If we need a program to be able to process text that may have been redirected in the console or that may be in files listed on the command line, we can use the fileinput module's fileinput.input() function. This function iterates over all the lines redirected from the console (if any) and over all the lines in the files listed on the command line, as one continuous sequence of lines. The module can report the current filename and line number at any time using fileinput.filename() and fileinput.lineno(), and can handle some kinds of compressed files.
Two separate modules are provided for handling command-line options, optparse and getopt. The getopt module is popular because it is simple to use and has been in the library for a long time. The optparse module is newer and more powerful.
Example: The optparse Module |
parser = optparse.OptionParser()
parser.add_option("-w", "—maxwidth", dest="maxwidth", type="int", help=("the maximum number of characters that can be " "output to string fields [default: %default]")) parser.add_option("-f", "--format", dest="format", help=("the format used for outputting numbers " "[default: %default]")) parser.set_defaults(maxwidth=100, format=".0f") opts, args = parser.parse_args()
Only nine lines of code are needed, plus the import optparse statement. Furthermore, we do not need to explicitly provide -h and --help options; these are handled by the optparse module to produce a suitable usage message using the texts from the help keyword arguments, and with any "%default" text replaced with the option's default value.
Notice also that the options now use the conventional Unix style of having both short and long option names that start with a hyphen. Short names are convenient for interactive use at the console; long names are more understandable when used in shell scripts. For example, to set the maximum width to 80 we can use any of -w80, -w 80, —maxwidth=80, or —maxwidth 80. After the command line is parsed, the options are available using the dest names, for example, opts.maxwidth and opts.format. Any command-line arguments that have not been processed (usually filenames) are in the args list.
If an error occurs when parsing the command line, the optparse parser will call sys.exit(2). This leads to a clean program termination and returns 2 to the operating system as the program's result value. Conventionally, a return value of 2 signifies a usage error, 1 signifies any other kind of error, and 0 means success. When sys.exit() is called with no arguments it returns 0 to the operating system.
Back in Chapter 2 we described the csv2html.py program. In that chapter s exercises we proposed extending the program to accept the command-line arguments, "maxwidth" taking an integer and "format" taking a string. The model solution (csv2html2_ans.py) has a 26-line function to process the arguments. Here is the start of the main() function for csv2html2_opt.py, a version of the program that uses the optparse module to handle the command-line arguments rather than a custom function:
Was this article helpful?