See All Titles |
![]() ![]() Command-line ArgumentsThe sys module also provides access to any command-line arguments via the sys.argv. Command-line arguments are those arguments given to the program in addition to the script name on invocation. Historically, of course, these arguments are so named because they are given on the command-line along with the program name in a text-based environment like a Unix- or DOS-shell. However, in an IDE or GUI environment, this would not be the case. Most IDEs provide a separate window with which to enter your "command-line arguments." These, in turn, will be passed into the program as if you started your application from the command-line. Those of you familiar with C programming may ask, "Where is argc?" The strings "argv" and "argv" stand for "argument count" and "argument vector," respectively. The argv variable contains an array of strings consisting of each argument from the command-line while the argc variable contains the number of arguments entered. In Python, the value for argc is simply the number of items in the sys.argv list, and the first element of the list, sys.argv[0], is always the program name. Summary:
Let us create a small test program called argv.py with the following lines: import sys print 'you entered', len(sys.argv), 'arguments…' print 'they were:', str(sys.argv) Here is an example invocation and output of this script: % argv.py 76 tales 85 hawk you entered 5 arguments… they were: ['argv.py', '76', 'tales', '85', 'hawk'] Are command-line arguments useful? Unix commands are typically programs which take input, perform some function, and send output as a stream of data. This data is usually sent as input directly to the next program, which does some other type of function or calculation and sends the new output to another program, and so on. Rather than saving the output of each program and potentially taking up a good amount of disk space, the output is usually "piped" in to the next program as its input. This is accomplished by providing data on the command-line or through standard input. When a program displays or sends output to the standard output file, the result would be displayed on the screen—unless that program is also "piped" to another program, in which case that standard output file is really the standard input file of the next program. I assume you get the drift by now! Command-line arguments allow a programmer or administrator to start a program perhaps with different behavioral characteristics. Much of the time, this execution takes place in the middle of the night and run as a batch job without human interaction. Command-line arguments and program options enable this type of functionality. As long as there are computers sitting idle at night and plenty of work to be done, there will always be a need to run programs in the background on our very expensive "calculators." Python features a getopt module that helps you parse command-line options and arguments.
|
© 2002, O'Reilly & Associates, Inc. |