Skip to content

Instantly share code, notes, and snippets.

@liuyix
Last active December 18, 2015 22:39
Show Gist options
  • Select an option

  • Save liuyix/5856067 to your computer and use it in GitHub Desktop.

Select an option

Save liuyix/5856067 to your computer and use it in GitHub Desktop.
Python Tips

Python 命令行参数

Documentation: http://docs.python.org/2.7/library/argparse.html

  • 指定--foo 时内部提供数值或者其他参数: store_const
    • 特殊情况 store_true/store_falsefoo 赋予True/False布尔值
  • 可以参数可以出现多次时:append,类似的还有append_const

Example

python提供的argparse可以实现很复杂的逻辑。在文档开始的地方就给出了一个例子。

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment