企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 问题 You want to write a program that parses options supplied on the command line (foundin sys.argv). ## 解决方案 The argparse module can be used to parse command-line options. A simple examplewill help to illustrate the essential features: # search.py‘''Hypothetical command-line tool for searching a collection offiles for one or more text patterns.‘''import argparseparser = argparse.ArgumentParser(description='Search some files') parser.add_argument(dest='filenames',metavar='filename', nargs='*') parser.add_argument(‘-p', ‘–pat',metavar='pattern', required=True,dest='patterns', action='append',help='text pattern to search for')parser.add_argument(‘-v', dest='verbose', action='store_true',help='verbose mode')parser.add_argument(‘-o', dest='outfile', action='store',help='output file')parser.add_argument(‘–speed', dest='speed', action='store',choices={‘slow','fast'}, default='slow',help='search speed') args = parser.parse_args() # Output the collected argumentsprint(args.filenames)print(args.patterns)print(args.verbose)print(args.outfile)print(args.speed) This program defines a command-line parser with the following usage: bash % python3 search.py -husage: search.py [-h] [-p pattern] [-v] [-o OUTFILE] [–speed {slow,fast}] > [filename [filename ...]] Search some files positional arguments:filenameoptional arguments: <table class="first docutils option-list" frame="void" rules="none"><col class="option"/><col class="description"/><tbody valign="top"><tr><td class="option-group"><kbd><span class="option">-h</span>, <span class="option">--help</span></kbd></td><td>show this help message and exit</td></tr><tr><td class="option-group" colspan="2"><kbd><span class="option">-p <var>pattern</var></span>, <span class="option">--pat <var>pattern</var></span></kbd></td></tr><tr><td> </td><td>text pattern to search for</td></tr><tr><td class="option-group"><kbd><span class="option">-v</span></kbd></td><td>verbose mode</td></tr><tr><td class="option-group"><kbd><span class="option">-o <var>OUTFILE</var></span></kbd></td><td>output file</td></tr></tbody></table> –speed {slow,fast} search speed The following session shows how data shows up in the program. Carefully observe theoutput of the print() statements. bash % python3 search.py foo.txt bar.txtusage: search.py [-h] -p pattern [-v] [-o OUTFILE] [–speed {fast,slow}] > [filename [filename ...]] search.py: error: the following arguments are required: -p/–pat bash % python3 search.py -v -p spam –pat=eggs foo.txt bar.txtfilenames = [‘foo.txt', ‘bar.txt']patterns = [‘spam', ‘eggs']verbose = Trueoutfile = Nonespeed = slow bash % python3 search.py -v -p spam –pat=eggs foo.txt bar.txt -o resultsfilenames = [‘foo.txt', ‘bar.txt']patterns = [‘spam', ‘eggs']verbose = Trueoutfile = resultsspeed = slow bash % python3 search.py -v -p spam –pat=eggs foo.txt bar.txt -o results –speed=fast filenames = [‘foo.txt', ‘bar.txt']patterns = [‘spam', ‘eggs']verbose = Trueoutfile = resultsspeed = fast Further processing of the options is up to the program. Replace the print() functionswith something more interesting. ## 讨论 The argparse module is one of the largest modules in the standard library, and has ahuge number of configuration options. This recipe shows an essential subset that canbe used and extended to get started.To parse options, you first create an ArgumentParser instance and add declarations forthe options you want to support it using the add_argument() method. In each add_argument() call, the dest argument specifies the name of an attribute where the result ofparsing will be placed. The metavar argument is used when generating help messages.The action argument specifies the processing associated with the argument and is oftenstore for storing a value or append for collecting multiple argument values into a list.The following argument collects all of the extra command-line arguments into a list. It’sbeing used to make a list of filenames in the example: parser.add_argument(dest='filenames',metavar='filename', nargs='*') The following argument sets a Boolean flag depending on whether or not the argumentwas provided: parser.add_argument(‘-v', dest='verbose', action='store_true',help='verbose mode') The following argument takes a single value and stores it as a string: parser.add_argument(‘-o', dest='outfile', action='store',help='output file') The following argument specification allows an argument to be repeated multiple timesand all of the values append into a list. The required flag means that the argument mustbe supplied at least once. The use of -p and –pat mean that either argument name isacceptable. parser.add_argument(‘-p', ‘–pat',metavar='pattern', required=True,dest='patterns', action='append',help='text pattern to search for') Finally, the following argument specification takes a value, but checks it against a set ofpossible choices. parser.add_argument(‘–speed', dest='speed', action='store',choices={‘slow','fast'}, default='slow',help='search speed') Once the options have been given, you simply execute the parser.parse() method.This will process the sys.argv value and return an instance with the results. The results for each argument are placed into an attribute with the name given in the dest parameterto add_argument().There are several other approaches for parsing command-line options. For example,you might be inclined to manually process sys.argv yourself or use the getopt module(which is modeled after a similarly named C library). However, if you take this approach,you’ll simply end up replicating much of the code that argparse already provides. Youmay also encounter code that uses the optparse library to parse options. Althoughoptparse is very similar to argparse, the latter is more modern and should be preferredin new projects.