The command line arguments from the operating system are put into
the sys.argv
variable as a sequence of strings.
Looking at the syntax rules for command line options and operands in the
previous section we can see that it can be challenging to parse this
sequence of strings.
The getopt
module helps us parse the
options and operands that are provided to our program on the command
line. This module has one very important function, also named
getopt
.
-
getopt.getopt
(
args
,
options
, 〈
long_options
〉)
→ ( options, operands )
-
Decode the given sequence of arguments,
args
, using the given set of
options
and
long_options
. Returns a tuple with a
sequence of normalized (option,value) pairs plus a sequence of the
program's operand values.
The
args
value should not include
sys.argv[0]
, the program name. Therefore, the
argument value for
args
is almost always
sys.argv[1:]
.
The
options
value is a string of the
one-letter options. Any options which require argument values are
followed by a :
. For example, "ab:c"
means that the program will accept -a
,
-c
, -ac
, -b
value
as options.
The
long_options
value is optional,
if present it is a list of the long options. If a long option
requires a parameter value, it's name must end in =
.
For example, ("silent","debug","log=")
means that
options like --silent
, --debug
,
and --log=myfile.log
are accepted as
options.
There are two results of getopt
: the options
and the operands. The options is a list of
(
name
,
value
)
pairs. The operands is the list
of names which follow the last option. In most cases, this list is a
list of file names to be used as input.
There are several ways to handle the options list.
-
We can iterate through this list, setting global variables, or
configuring some processing object. This works well when we have
both short and long option names for the same configuration
setting.
options, operands = getopt.getopt( sys.argv[1:], ... )
for name, value in options:
if name == "-X" or name == "--long":
set some global variable
-
We can define our configuration as a dictionary. We can then
update this dictionary with the options. This forces the rest of our
program to handle the -X
or --long
names
for cofiguration parameters.
config = { "-X" : default, "--long": default }
options, operands = getopt.getopt( sys.argv[1:], ... )
config.update( dict(options) )
-
We can define our configuration as a dictionary. We can
initialize that configuration dictionary with the given options then
fold in default values. While pleasantly obvious, it still makes the
-X
and --long
options visible throughout
our program.
options, operands = getopt.getopt( sys.argv[1:], ... )
config= dict(options)
config.setdefault( "-X", value )
config.setdefault( "--long", value )
One very adaptable and reusable structure is the following.
class MyApplication( object ):
def __init__( self ):
self.someProperty=
default
def process( aFileName ):
""" The Real Work. """
This is the real work of this applications
def main():
theApp= MyApplication()
options, operands = getopt.getopt( sys.argv[1:], "..." )
for name, value in options:
if name == "-X" or name == "--long":
set properties in theApp
for fileName in operaands:
theApp.process( aFileName )
A Complete Example. Here's a more complete example of using
getopt
. Assume we have a program with the
following synopsis.
portfolio.py
〈-v〉 〈-h〉 〈-d
mm/dd/yy
〉 〈-s
symbol
...〉 file
This program has two single-letter options: -v
and
-h
. It has two options which take argument values,
-d
and -s
. Finally, it accepts an operand of a
file name.
These options can be processed as follows:
"""portfolio.py -- examines a portfolio file
"""
import getopt
class Portfolio( object ):
...
def main():
portfolio= Portfolio()
opts, operands = getopt( sys.argv[1:], "vhd:s:" )
for o,v in opts:
if o == "-v": portfolio.verbose= True
elif o == "-h":
print __doc__
return
elif o == "-d": portfolio.date= v
elif o == "-s": portfolio.symbol= v
for f in operands:
portfolio.process( f )
if __name__ == "__main__":
main()
The program's options are coded as "vhd:s:"
:
the single-letter options (-v
and -h
) and the
value options (-d
and -s
). The
getopt
function separates the the options from the
arguments, and returns the options as a sequence of option flags and
values. The process
function performs the real work
of the program, using the options and operands extracted from the
command line.