Xiang Wang @ 2019-02-27 18:57:23
- command:
ls
- positional argument:
ls DESC
- optional argument:
ls -l
- help text:
ls --help
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
- 添加一个参数
parser.add_argument("echo", help="echo the string you use here")
- 添加一个数字参数
parser.add_argument("square", help="展示一个数字", type=int)
- example
parser.add_argument("--verbosity", help="是否查看")
if args.verbosity:
print("verbosity turned on")
./command.py --verbosity 1
./command.py # 这样就不会verbosity了
- 使用
store_true
parser.add_argument("--verbose", action="store_true", help="increase output verbosity")
args = parser.parse_args()
if args.verbos:
print("verbose")
- 使用short options
parser.add_arguments("-v", ""--verbose", help="increate output verbosity", action="store_true")
args = parser.parse_args()
if args.verbose:
print("verbosity on")
import argparse
parser = argparse.ArgumentParser(description="我是一个用来处理几个数字的脚本")
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="找到最大的数字 (default: find the max)")
args = parser.parse_args()
print(args.accumulate(args.integers))