, , sys.argv; , sys.argv[0] - script, - :
import sys
if len(sys.argv) > 1:
try:
age = int(sys.argv[1])
except ValueError:
print('Please give an age as an integer')
sys.exit(1)
else:
print('Please give an age as a command line argument')
sys.exit(1)
Python argparse, sys.argv:
import argparse
parser = argparse.ArgumentParser('Admission fee determination')
parser.add_argument('age', type=int, help='the age of the visitor')
args = parser.parse_args()
age = args.age
age . script :
$ python yourscript.py -h
usage: Admission fee determination [-h] age
positional arguments:
age the age of the visitor
optional arguments:
-h, --help show this help message and exit
, age .