10/7/2016 - 1:30 PM

Argparse example. #python #argparse #command-line

Argparse example. #python #argparse #command-line


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 '
    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 ',
    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:])