1

The python program has multiple options such as download data, build model or both. Example:

parser.add_argument('-m', '--mode', required=True, choices=['dload', 'predict', 'both'],
                    help='dload = download data\n '
                         'predict = build prediction model\n,'
                         'both = download and predict')

but I want to make sure some of the other arguments are only need to appear when its download. I can easily set required=False but that doesn't look like a good solution.

parser.add_argument('-s', '--start-year-month', required=False,
                    help="start year to download data, separate year and month by '- ' "\
                    "ex: 2010-01")

parser.add_argument('-e', '--end-year-month', required=False,
                    help="ending year of data set separate year and month by '- ' " \
                    "ex: 2010-01")

above two arguments should only be required when the choice for -m is dload or both

add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
  • 2
    Possible duplicate of [Argparse: Required argument 'y' if 'x' is present](https://stackoverflow.com/questions/19414060/argparse-required-argument-y-if-x-is-present) – Hampus Larsson Nov 13 '18 at 16:57
  • The default for `optionals` is `required=False`. If you specify the right defaults for these arguments, it might not matter whether they are provided or not. You can also test for values after parsing. The `parser` doesn't have to do all the checking. – hpaulj Nov 13 '18 at 17:06
  • Are 's' and 'e' allowed with 'predict'? Just not required? I was thinking of elaborating on the deleted 'sub-commands' answer, but if the only difference is in the 'required' parameter, it may not be worth it. Stick with your own post-parsing testing. – hpaulj Nov 14 '18 at 04:52

0 Answers0