1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| https://pymotw.com/2/optparse/ 下面是一个简单的例子. import optparse
parser = optparse.OptionParser()
parser.add_option('-q', action='store_const', const='query', dest='mode', help='Query') parser.add_option('-i', action='store_const', const='install', dest='mode', help='Install')
query_opts = optparse.OptionGroup(parser, 'Query Options', 'These options control the query mode.',) query_opts.add_option('-l', action='store_const', const='list', dest='query_mode', help='List contents') query_opts.add_option('-f', action='store_const', const='file', dest='query_mode', help='Show owner of file') query_opts.add_option('-a', action='store_const', const='all', dest='query_mode', help='Show all packages') parser.add_option_group(query_opts)
install_opts = optparse.OptionGroup(parser, 'Installation Options','These options control installation.',) install_opts.add_option('--hash', action='store_true', default=False, help='Show hash marks as progress indication') install_opts.add_option('--force', dest='install_force', action='store_true', default=False, help='Install, regardless of depdencies or existing version') parser.add_option_group(install_opts)
print parser.parse_args()
$ python optparse_groups.py -h
Usage: optparse_groups.py [options]
Options: -h, --help show this help message and exit -q Query -i Install
Query Options: These options control the query mode.
-l List contents -f Show owner of file -a Show all packages
Installation Options: These options control installation.
--hash Show hash marks as progress indication --force Install, regardless of depdencies or existing version
|