hlyang1992
3/3/2017 - 1:18 AM

命令行参数解析

命令行参数解析

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('integer', help="positional argument")

parser.add_argument('bar', nargs='?', type=int, default=42,
                  help='the bar to %(prog)s (default: %(default)s)')

parser.add_argument('--foo', action='store_true',
                     help='foo the bars before frobbling')
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))