I'm learning to include command line parameters in my code. I've read the docs for argparse
and I tried running this script from there.
#argparse_trial.py
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))
If I run
>python argparse_trial.py 1
in the command line, I get the expected result 1
But if I run
>argparse_trial.py 1
I get
usage: arg_parse_trial.py [-h] [--sum] N [N ...]
argparse_trial.py: error: the following arguments are required: N
I checked and the only argument the code seems to receive in the second case is the filename itself, no matter how many arguments are given.
I'm on a Windows machine and python is in my path.
Why is the second case failing in this script? How can I get it to work?