LISTINg 10-2
(continued)
548592c10.indd 198
2/24/10 12:48:28 PM
www.finebook.ir
Understanding the Command Line
❘
199
The list of short options is
‘Dh?g:s‘
. Notice that you don’t include a dash between each of the
options — Python includes them for you automatically. Each of the entries is a different command
line switch, except for the colon. So, this application accepts
–D
,
–h
,
–?
,
–g:
, and
–s
as command line
switches. The command line switches are case sensitive. The colon after
–g
signifies that the user must
also provide a value as part of the command line switch.
The list of long options includes
[‘help‘, ‘Greet=‘, ‘Hello‘]
. Notice that you don’t include
the double dash at the beginning of each long option. As with the short versions of the command
line switch, these command line switches are case sensitive. The command line switches for this
example are:
–D
➤
➤
: Debug mode
–h
➤
➤
,
–?
, and
––help
: Help
➤
➤
–g:Username
and
––Greet:
Username
: Greeting that includes the user’s name
–s
➤
➤
and
––Hello
: Says hello to the user without using a name
At this point, the code can begin processing
opts
and
args
. In both cases, the code relies on a
for
loop to perform the task. However, notice that
opts
relies on two arguments,
opt
and
arg
, while
args
relies on a single argument
arg
. That’s because
opts
and
args
are stored differently. The
opts
version
of
-g:John
appears as
[(‘–g‘, ‘:John‘)]
, while the
args
version appears as
[‘/g:John‘]
. Notice
that
opts
automatically separates the command line switch from the value for you.
Processing
opts
takes the same course in every case. The code uses an
if
statement such as
if opt
in (‘–h‘, ‘–?‘, ‘––help‘)
to determine whether the string appears in
opt
. In most cases, the
code simply prints out a value for this example. The help routine calls on
usage()
, which is explained
in the “Providing Command Line Help” section of the chapter. Calling
sys.exit()
automatically
ends the application. If the application detects any command line options that don’t appear in your
list of command line options to process, it raises the
getopt.GetoptError()
exception. Standard
practice for Python applications is to display usage information using
usage()
and then exit with an
error code (of 2 in this case by calling
sys.exit(2)
).
Now look at the
args
processing and you see something different. Python doesn’t provide nearly as
much automation in this case. In addition, your user will likely expect / command line switches to
behave like those for most Windows applications (case insensitive). The example handles this issue
by using a different
if
statement, such as
if arg.upper() in (‘/?‘, ‘/HELP‘)
. Notice that the
options use a slash, not a dash.
Argument processing relies on a single
if
statement, rather than individual
if
statements.
Consequently, the second through the last command line switches actually rely on an
elif
clause. Python won’t automatically detect errors in / command line switches. Therefore, your
code also requires an else clause that raises the
getopt.GetoptError()
event manually.
Remember that arguments are single strings, not command line switch and value pairs. You need
some method to split the command line switch from the value. The code handles this case using
elif ‘/GREET‘ in arg.upper()
or
‘/G‘ in arg.upper()
where it compares each command line
switch individually. In addition, it relies on
arg.split(‘:‘)[1]
to display the value. The argument
processing routine shows that you can accommodate both Linux and Windows users quite easily
with your application.
548592c10.indd 199
2/24/10 12:48:29 PM
www.finebook.ir
Do'stlaringiz bilan baham: |