pythondocopt

docopt boolean arg python


I use following args for my script with doctopt

Usage:
GaussianMixture.py --snpList=File --callingRAC=File

Options:
-h --help     Show help.
snpList     list snp txt
callingRAC      results snp

I would like to add an argument that have a conditional consequence on my script : correct my datas or don't correct my datas. Something like :

Usage:
GaussianMixture.py --snpList=File --callingRAC=File  correction(--0 | --1)

Options:
-h --help     Show help.
snpList     list snp txt
callingRAC      results snp
correction      0 : without correction | 1 : with correction 

And I would like to add in my script an if in some functions

def func1():
  if args[correction] == 0:
      datas = non_corrected_datas
  if args[correction] == 1:
      datas = corrected_datas

But I don't know how to write it in the usage neither in my script.


Solution

  • EDIT: My original answer didn't take into account OP's requirements for --correction to be mandatory. Syntax was incorrect in my original answer. Here's a tested working example:

    #!/usr/bin/env python
    """Usage:
        GaussianMixture.py --snpList=File --callingRAC=File --correction=<BOOL>
    
    Options:
        -h, --help          Show this message and exit.
        -V, --version       Show the version and exit
        --snpList         list snp txt
        --callingRAC      results snp
        --correction=BOOL Perform correction?  True or False.  [default: True]
    
    """
    
    __version__ = '0.0.1'
    
    from docopt import docopt
    
    def main(args):
        args = docopt(__doc__, version=__version__)
        print(args)
    
        if args['--correction'] == 'True':
            print("True")
        else:
            print("False")
    
    if __name__ == '__main__':
        args = docopt(__doc__, version=__version__)
        main(args)
    

    Please let me know if this works for you.