I have noticed that argparse uses a rather "mystic" way of creating the variables in the parser. I know that the names of the variables are usually easy to infer:
it's the stripped version of long or short option (without the -- or - respectively).
Also all hyphens (-) becomes underscores (_) to become legal variable names.
But that leaves me with a question about conflicting cases (I know it's an extreme case but the inferring part it's a bit mysterious to me). For example the program:
import argparse
parser = argparse.ArgumentParser(description="A simple test about var names")
parser.add_argument("--max-value", type=int, help="the maximum value", metavar="Maximum-value")
parser.add_argument("-m", "--max_value", action="store_true", help="Whether to the use maximum value", dest="max1")
args = parser.parse_args()
print("max_value {}".format(args.max1))
print("max-value {}".format(args.max_value))
uses apparently two very similar options (--max-value and --max_value) which lead to the same inferred variable max_value. If either of the options was missing the variable would be max_value without ambiguity.
But when both are present, apparently --max_value gets the trophy variable max_value and the second one (--max-value) gets what? I haven't been able to find what's the second variable.
So, to access it I must define a variable explicitly with dest option? How do I get list of the names of variables available? The funny thing is that if I use dest= for the --max_value option then --max-value gets the expected variable max_value while --max_value gets the non inferred one (in my case max1)!
I know also that metavar has nothing to do with the actual variable name but only affects the display in help.
Edit:
Adding some information from the @Martijn Pieters answer:
If I am getting it write if no dest is applied the parser follows the general rule which states that an implicit dest is applied. The same in my case dest="max_value".
So,
parser.add_argument("--max-value", type=int, help="the maximum value")
is exactly the same as:
parser.add_argument("--max-value", type=int, help="the maximum value", dest="max_value")
internally.
But, then the following code snippets should produce different results which it does not:
# parserv1.py
import argparse
parser = argparse.ArgumentParser(description="A simple test about var names")
parser.add_argument("-m", "--max_value", action="store_true", help="Whether to the use maximum value", dest="max_value")
parser.add_argument("--max-value", type=int, help="the maximum value", metavar="Maximum-value", dest="max_value")
args = parser.parse_args()
print("max-value {}".format(args.max_value))
>>>python parserv1.py -m --max-value 3
max-value 3
# parserv2.py
import argparse
parser = argparse.ArgumentParser(description="A simple test about var names")
parser.add_argument("--max-value", type=int, help="the maximum value", metavar="Maximum-value", dest="max_value")
parser.add_argument("-m", "--max_value", action="store_true", help="Whether to the use maximum value", dest="max_value")
args = parser.parse_args()
print("max-value {}".format(args.max_value))
>>>python parserv1.py -m --max-value 3
max-value 3
Both just print the value of max_value as int independently of the order they are declared.
So, int option have higher precedence than binary (i.e. flags)? Are option types important in these cases?
P.S. I am using python 3.6.3 and since it might be a version issue I wanted to mention it.