python test.py --arg -foo -bar
test.py: error: argument --arg: expected at least one argument
python test.py --arg -8
['-8']
How do I allow -non_number to work with argparse?
Is there a way to disable short arguments?
python test.py --arg -foo -bar
test.py: error: argument --arg: expected at least one argument
python test.py --arg -8
['-8']
How do I allow -non_number to work with argparse?
Is there a way to disable short arguments?
Call it like this:
python test.py --arg='-foo'
To allow specifying multiple:
parser.add_argument('--arg', action='append')
# call like python test.py --arg=-foo --arg=-bar
I think you're looking for the nargs
parameter to argparser.
parser.add_argument('--arg', nargs='?')
At the moment, --arg
is interpreting the value '-8'
as the input, whereas it thinks '-f'
(with parameters 'oo'
) is a new argument.
Alternatively, you could use action='store_true'
, which will represent the presence or absence of the argument with a boolean.
parser.add_argument('--arg', action='store_true')