0

This is a very basic example of setting up argparse:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--input_a", type=int, default=0,
                    help="first_input")
parser.add_argument("--input_b", type=int,
                    help="second_input")
args = parser.parse_args()

The default value for an argument is the value that it takes if it's not provided explicitly when I call the script.

I want to modify the code above in order to have for input_b more than one default value. To be more clear, I want that the default value of input_b depends on the value of input_a. Is this feature covered by argparse library or should I make a workaround after the parsing?


Bonus Question

To further extend the question, could I set the default value of one argument on the base of more than one other argument?

gvgramazio
  • 1,115
  • 3
  • 13
  • 30
  • Default values are put in the `args` Namespace at the start of parsing, and over written when relevant flag is parsed. Interactions between arguments are possible with custom `Action` classes, but get messy. It's better to do that kind of thing after parsing, when all arguments have been processed. – hpaulj Nov 03 '18 at 17:41
  • @hpaulj Have you any link that even remotely addresses this capability except for `argparse` documentation. I'm interested even if maybe is prone to mess. – gvgramazio Nov 03 '18 at 17:56
  • You can read the `argparse.py` code for yourself. Other SO questions have explored making parameters of argument depend on the values of others. Most recently there have been several questions about changing the `required` attribute based on the presence or absence of another argument. – hpaulj Nov 03 '18 at 18:06
  • @hpaulj I didn't think about that but it's also an interesting possible feature. – gvgramazio Nov 03 '18 at 21:13

0 Answers0