0

e.g., to parse

./test.py --species electron proton --mass_electron 1

Here, the second argument name mass_electron is valid because electron is one of the values we passed to the argument species.

Edit 1:

This is different from another question Arguments that are dependent on other arguments with Argparse since I would like the argument name to depend on the value of another argument, i.e., that value is arbitrary.

Liang
  • 1,015
  • 1
  • 10
  • 13
  • Most cross checking is best done after parsing - when all of the inputs are known, regardless of their order in the commandline. The only mechanisms that `argparse` provides are the subparsers and mutually exclusive groups (xor). (Some people have tried custom `Action` classes, or multistage parsing, but the resulting code is usually more complicated.) – hpaulj Dec 17 '18 at 17:42

1 Answers1

1

I think the proper way is not to perform dynamic feeding of argument types to argparse, but to declare all possible arguments, parse command line and then raise exception if their combination is incorrect.

grapes
  • 8,185
  • 1
  • 19
  • 31
  • Yes, I ended up specifying all possible arguments, though some are fake placeholders if I don't truly need them. Dynamic feeding does cause confusion. The problem comes from the fact that some of the information is not contained in the data file, i.e., I made the assumptions depending on additional information. – Liang Dec 17 '18 at 16:19