user1447941
user1447941

Reputation: 3895

How do I make an argument optional if a condition is met?

I'm just starting out with docopt, and with it I am trying to write an usage command that will accept one or more optional arguments if the command matches something. Here it is at its current state:

Usage:
  script.py voucher (add|del) <code> <credits> [<points>]

Here, first the voucher command is used, and then either add or del. I want to change the line so that if add is used, both the code and credits arguments are required, but points is optional.

However, if del is used instead, then only the code argument is needed.

How would I be able to do this?

Upvotes: 2

Views: 171

Answers (1)

user180100
user180100

Reputation:

"""S.O. 12766628

Usage:
    script.py voucher add <code> <credits> [<points>]
    script.py voucher del <code>
"""
from docopt import docopt


if __name__ == '__main__':
    arguments = docopt(__doc__, version='S.O. 12766628')
    print(arguments)

Does what you need.

Upvotes: 3

Related Questions