1

I have a file named test.py. There are 3 ways to use it:

  1. You can load the contents of a json file, and it will generate graphs using that data (eg: python test.py -l './data.json')
  2. You can give it a YAML file, and it will use some information in it to generate a json file and then generate graphs (eg: python test.py -y './user_input.yaml')
  3. You can give it the information mentioned in point 2 through command line arguments (eg: python test.py -s 50 -ftr '.pkl')

If -l is used, it won't need any of the arguments used in 2 or 3. If -y is used, it won't need any of the arguments from 1 and 2, and if the command-line arguments are given, it won't need any of the other arguments (but it will need the s and ftr arguments)

How would I set that up with argparse? One way would be to make if statements, and if args.yaml is not None: <load from the yaml file>, but the problem with that is I would prefer an argument required error to pop up if option 3 is chosen but not all the arguments are given. Is there a way to use argparse that would satisfy my need?

Kadhir
  • 143
  • 1
  • 2
  • 12
  • `mutually_exclusive_group` should handle the '-l' vs '-y' case. '-s' could be part of the group if it didn't also expect '-ftr'. For more complicated things, I'm recommending testing after parsing. The key purpose of `argparse` is understand what your user wants; don't design a parser that's hard to explain. – hpaulj Jul 16 '20 at 00:41

0 Answers0