Created
October 7, 2016 13:30
-
-
Save Makistos/cea9c10639a7704c0dd6997608207a87 to your computer and use it in GitHub Desktop.
Argparse example. #python #argparse #command-line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment