CLI Program Arguments
Table of contents
Description
Programs can be written to accept command-line arguments when they are executed. This requires the use of the sys.argv
list.
- The first item in this list (index 0) is always the name of the file being executed.
- Any additional items in this list are arguments supplied on the command line.
- The module
sys
must be imported to access thesys.argv
list.
argv.py
The following program prints items in the sys.argv list. It is saved in the /Users/bianca/test
directory.
Navigate to directory and execute:
addition-cli.py
The following program accepts two integer arguments from the command line, then displays the sum. It is saved in the /Users/bianca/test
directory.