@songying
2018-09-04T09:47:00.000000Z
字数 1115
阅读 1125
python库
参考: https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser
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()
解析器类为ArgumentParser
import argparse
parser = argparse.ArgumentParser(description='This is a PyMOTW sample program')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
默认情况下,参数是从 sys.argv[1:] 中获取,但你也可以传递自己的参数列表。
parser.parse_args(['--sum', '7', '-1', '42'])
class argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True)
action='store_true'
store_true 是指带触发action时为真,不触发则为假.
python test.py -c => c是true(触发)
python test.py => c是false(无触发)