|
| 1 | +# -*- coding:utf-8 -*- |
| 2 | + |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def function_argparse(): |
| 9 | + ''' |
| 10 | + function_argparse |
| 11 | +
|
| 12 | + reference link: |
| 13 | +
|
| 14 | + 1. https://docs.python.org/zh-cn/2.7/library/argparse.html |
| 15 | + 2. https://docs.python.org/zh-cn/3.8/library/argparse.html |
| 16 | +
|
| 17 | + usage: python -m build [-h] [--version] [--sdist] [--wheel] [--outdir OUTDIR] [--skip-dependency-check] [--no-isolation] |
| 18 | + [--config-setting CONFIG_SETTING] |
| 19 | + [srcdir] |
| 20 | + ''' |
| 21 | + # |
| 22 | + # the command line srcipt: |
| 23 | + # |
| 24 | + # python -m build -h |
| 25 | + # python test_argparse_2.py --help |
| 26 | + # python test_argparse_2.py -h |
| 27 | + # python test_argparse_2.py ./a ./b |
| 28 | + # python test_argparse_2.py ./a ./b -vs |
| 29 | + # python test_argparse_2.py ./a ./b -v1 0 |
| 30 | + # python test_argparse_2.py ./a ./b -v1 1 |
| 31 | + # python test_argparse_2.py ./a ./b -v1 2 |
| 32 | + # python test_argparse_2.py ./a ./b -v2 |
| 33 | + # python test_argparse_2.py ./a ./b -v2 -v2 |
| 34 | + # python test_argparse_2.py ./a ./b -v |
| 35 | + # python test_argparse_2.py ./a ./b -q |
| 36 | + # python test_argparse_2.py ./a ./b -vs -v1 0 |
| 37 | + # python test_argparse_2.py ./a ./b -vs -v1 1 |
| 38 | + # python test_argparse_2.py ./a ./b -vs -v1 2 |
| 39 | + # python test_argparse_2.py ./a ./b -vs -v1 0 -v2 |
| 40 | + # python test_argparse_2.py ./a ./b -vs -v1 1 -v2 |
| 41 | + # python test_argparse_2.py ./a ./b -vs -v1 2 -v2 |
| 42 | + # python test_argparse_2.py ./a ./b -vs -v1 0 -v2 -v2 |
| 43 | + # python test_argparse_2.py ./a ./b -vs -v1 1 -v2 -v2 |
| 44 | + # python test_argparse_2.py ./a ./b -vs -v1 2 -v2 -v2 |
| 45 | + # python test_argparse_2.py ./a ./b -vs -v1 0 -v2 -v |
| 46 | + # python test_argparse_2.py ./a ./b -vs -v1 1 -v2 -v |
| 47 | + # python test_argparse_2.py ./a ./b -vs -v1 2 -v2 -v |
| 48 | + # python test_argparse_2.py ./a ./b -vs -v1 0 -v2 -q |
| 49 | + # python test_argparse_2.py ./a ./b -vs -v1 1 -v2 -q |
| 50 | + # python test_argparse_2.py ./a ./b -vs -v1 2 -v2 -q |
| 51 | + # |
| 52 | + parser = argparse.ArgumentParser(description='这个是命令行总体多行文本描述信息.') |
| 53 | + # 位置参数 |
| 54 | + parser.add_argument('srcdir', metavar='srcdir', type=str, help='这是一个 srcdir 位置参数') |
| 55 | + parser.add_argument('destdir', metavar='destdir', type=str, help='这是一个 destdir 位置参数') |
| 56 | + # 可选参数 |
| 57 | + parser.add_argument('-vs', '--version', action="store_true", help='可选参数:显示版本号,并退出') |
| 58 | + parser.add_argument("-v1", "--verbosity_1", type=int, choices=[0, 1, 2], default=0, help="可选参数: 默认值形式") |
| 59 | + parser.add_argument("-v2", "--verbosity_2", action="count", default=0, help="可选参数: 默认值count 类型形式") |
| 60 | + # parser.add_argument('-l', |
| 61 | + # '--log', |
| 62 | + # default=sys.stdout, |
| 63 | + # type=argparse.FileType('w'), |
| 64 | + # help='the file where the sum should be written') |
| 65 | + # 可选参数,互斥选项 |
| 66 | + group = parser.add_mutually_exclusive_group() |
| 67 | + group.add_argument("-v", "--verbose", action="store_true", help='可选参数,互斥参数: the verbose info') |
| 68 | + group.add_argument("-q", "--quiet", action="store_true", help='可选参数,互斥参数: the quiet info') |
| 69 | + args = parser.parse_args() |
| 70 | + |
| 71 | + if args.srcdir: |
| 72 | + print('the args.srcdir: {}'.format(args.srcdir)) |
| 73 | + logging.debug('the args.srcdir: {}'.format(args.srcdir)) |
| 74 | + if args.destdir: |
| 75 | + print('the args.destdir: {}'.format(args.destdir)) |
| 76 | + logging.debug('the args.destdir: {}'.format(args.destdir)) |
| 77 | + if args.version: |
| 78 | + print('the args.version: {}'.format(args.version)) |
| 79 | + logging.debug('the args.version: {}'.format(args.version)) |
| 80 | + if args.verbosity_1 >= 0: |
| 81 | + print('the args.verbosity_1: {}'.format(args.verbosity_1)) |
| 82 | + logging.debug('the args.verbosity_1: {}'.format(args.verbosity_1)) |
| 83 | + if args.verbosity_2: |
| 84 | + print('the args.verbosity_2: {}'.format(args.verbosity_2)) |
| 85 | + logging.debug('the args.verbosity_2: {}'.format(args.verbosity_2)) |
| 86 | + if args.verbose: |
| 87 | + print('the args.verbose: {}'.format(args.verbose)) |
| 88 | + logging.debug('the args.verbose: {}'.format(args.verbose)) |
| 89 | + if args.quiet: |
| 90 | + print('the args.quiet: {}'.format(args.quiet)) |
| 91 | + logging.debug('the args.quiet: {}'.format(args.quiet)) |
| 92 | + # if args.log: |
| 93 | + # args.log.write('%s' % sum(args.integers)) |
| 94 | + # args.log.close() |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + ''' the main point ''' |
| 99 | + function_argparse() |
0 commit comments