python Argparse的例子。 #python #argparse #command-line

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Argparse的例子。 #python #argparse #command-line相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python

import sys
import argparse
import pprint

def read_params(args):
    p = argparse.ArgumentParser(epilog='TAG is required for db operations '
    'but can also be used without them. In that case files are named using '
    'it. It can also be omitted in which case results are only written to '
    'results.csv.')
    p.add_argument('TAG', help='Test (database) tag', nargs='?', default='')
    g1 = p.add_mutually_exclusive_group()
    g1.add_argument('-b', '--boot-only', help='Boot time only, no detailed '
            'timings.', action='store_true')
    g1.add_argument('-v', '--verbose', help='Verbose logging, no adb filter used.')
    p.add_argument('-c', '--count', help='Repeat count. Default .', default=10, type=int)
    p.add_argument('-i', '--image', help='Device to test. Default ',
            default='system2')
    p.add_argument('-p', '--pin', help='Pin code. Default ', default='0000')
    p.add_argument('-q', '--quiet', help='Quiet mode.', action='store_true')
    g2 = p.add_argument_group('database options')
    g2.add_argument('-r', '--read', help='Read from db (optionally only TAG '
            'rows). No boots performed. Empty tag value \'\' for summaries.',
            dest='db_mode', action='store_const', const='read')
    g2.add_argument('-w', '--write', help='Write results to db to TAG.',
            dest='db_mode', action='store_const', const='write')
    g2.add_argument('-u', '--update', help='Update summary row for TAG.',
            dest='db_mode', action='store_const', const='update')
    g2.add_argument('--delete', help='Delete TAG from database.',
            dest='db_mode', action='store_const', const='delete')
    g2.add_argument('--fields', help='Fields to show with -r.')
    g2.add_argument('--sort', help='Sort order for -r.')
    g2.add_argument('-x', '--extended-description', help='Extended '
            'description (written only to db).', metavar='DESC')


    return vars(p.parse_args(args))

d = read_params(sys.argv[1:])
pprint.pprint(d)

以上是关于python Argparse的例子。 #python #argparse #command-line的主要内容,如果未能解决你的问题,请参考以下文章

python内置库--argparse

argparse 仅在位置参数之前解析

python中的argparse

python argparse模块

python argparse模块

python argparse模块