Python argparse case inconsistency between optional and positional arguments

Viewed 159

I was wondering why the inconsistency between the case-transformation of optional and positional arguments in Python's argparse. Adding the '--optional-argument' to the parser will be named 'optional_argument', but positional argument would stay positional-argument.

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('positional-argument')
parser.add_argument('--optional-argument')
arg_dict = vars(parser.parse_args('Positional --optional-argument Optional'.split()))
print(arg_dict)
# {'positional-argument': 'Positional', 'optional_argument': 'Optional'}

I could change the way I add the positional argument to the parser, but the inconsistency would remain (though at a different place)

parser.add_argument('positional_argument')
parser.add_argument('--optional-argument')
# {'positional_argument': 'Positional', 'optional_argument': 'Optional'}
1 Answers
Related