Argparse
Simple Examples
import argparse
parser = argparse.ArgumentParser(description='Short sample app')
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print parser.parse_args(['-a', '-bval', '-c', '3'])
import argparse
parser = argparse.ArgumentParser(description='Example with long option names')
parser.add_argument('--noarg', action="store_true", default=False)
parser.add_argument('--witharg', action="store", dest="witharg")
parser.add_argument('--witharg2', action="store", dest="witharg2", type=int)
print parser.parse_args([ '--noarg', '--witharg', 'val', '--witharg2=3' ])
Automatically Generated Options
import argparse
parser = argparse.ArgumentParser(version='1.0')
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print parser.parse_args()
print 'This is not printed'
Sources of Arguments
In the examples so far, the list of arguments given to the parser have come from a list passed in explicitly, or were taken implicitly from sys.argv. Passing the list explicitly is useful when you are using argparse to process command line-like instructions that do not come from the command line (such as in a configuration file).
import argparse
from ConfigParser import ConfigParser
import shlex
parser = argparse.ArgumentParser(description='Short sample app')
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
config = ConfigParser()
config.read('argparse_witH_shlex.ini')
config_value = config.get('cli', 'options')
print 'Config :', config_value
argument_list = shlex.split(config_value)
print 'Arg List:', argument_list
print 'Results :', parser.parse_args(argument_list)