Python argparse

This is a template of argparse.

1
2
3
4
5
6
7
import argparse
parser = argparse.ArgumentParser(description="description of this script")
parser.add_argument('--parameter','-p',type=int, default='1', help='epoch')
args = parser.parse_args()

parameter = args.parameter
print(parameter)

or

1
2
3
4
5
parser = argparse.ArgumentParser(description="description of this script")
parser.add_argument('--querID','-id',type=str, help='Query ID')

args = parser.parse_args().__dict__
print(args)

Pass boolean flag via

1
2
parser.add_argument('--flag', action="store_true", help="this is your boolean value")
# enable this flag via: python test.py --flag

python script.py -h to show the help of parameters.