4

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.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Elysire
  • 693
  • 10
  • 23

2 Answers2

6

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.

  • Thank you for this solution, this works. But I really would like to have an obligatory argument with or without correction because users can forget the optional argument and have datas not corrected by mistake if they are not familiar to this where as if it's obligatory they will have an error. But I can add a warning print meanwhile I find a better solution :) thanks anyway ! – Elysire Dec 07 '16 at 17:30
  • You could try making correction manditory (remove brackets), and give it a true/false value eg: – Dustin Keib Dec 07 '16 at 22:12
  • 1
    Usage: GaussianMixture.py --snpList=File --callingRAC=File correction=(True | False) – Dustin Keib Dec 07 '16 at 22:13
  • Last comment didn't include the proper syntax of --correction=(True | False) - I've updated my answer to consider being a mandatory parameter – Dustin Keib Dec 07 '16 at 22:20
  • 1
    Remember to put single `-` and double dashes `--` in the options part. The options arguments should also be present in the options part. – J. P. Petersen Dec 08 '16 at 11:14
  • Thank you J. P., good point! I've updated my answer accordingly. – Dustin Keib Dec 08 '16 at 18:13
4

Not all options have to have arguments in docopt. In other words, you can use flag arguments instead. This is the most straightforward way to get boolean values from a user. That being said, you can simply do the following.

"""
Usage:
  GaussianMixture.py (--correction | --no-correction)

Options:
  --correction      With correction
  --no-correction   Without correction
  -h --help     Show help.
"""
import docopt


if __name__ == '__main__':
    args = docopt.docopt(__doc__)
    print(args)