Option with input
If you want to use the input function, then the following solution is possible (weird):
import argparse
class StopAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
parser.exit('Exit')
parser = argparse.ArgumentParser(description='SImple Example')
parser.add_argument('-x', '--height', type=int, help='Height of the box')
parser.add_argument('-l', '--length', type=int, help='Length of the box')
parser.add_argument('-b', '--breadth', type=int, help='Breadth of the box')
parser.add_argument('--stop', nargs=0, required=False, action=StopAction)
values = []
for i in ['-x', '-l', '-b']:
i_value = input(f'enter {i} value (integer): ').strip()
if i_value == '--stop':
parser.parse_args([i_value])
elif i_value in ['-h', '--help']:
parser.parse_args([i_value])
else:
# check for int
parser.parse_args([i, i_value])
values.append(i)
values.append(i_value)
args = parser.parse_args(values)
# do something
print(args)
Output:
>test.py
enter -x value (integer): --stop
Exit
>test.py
enter -x value (integer): 1
enter -l value (integer): 2
enter -b value (integer): 3
Namespace(breadth=3, height=1, length=2, stop=None)
>test.py
enter -x value (integer): 1
enter -l value (integer): q
usage: test.py [-h] [-x HEIGHT] [-l LENGTH] [-b BREADTH] [--stop]
test.py: error: argument -l/--length: invalid int value: 'q'
Option with sys.argv
Typically the command line interface is used as follows (no input):
test.py -x 1 -l 2 -b 3
You can use sys.argv to get a list of command line arguments.
sys.argv
The list of command line arguments passed to a Python script. argv[0]
is the script name (it is operating system dependent whether this is a
full pathname or not). If the command was executed using the -c
command line option to the interpreter, argv[0] is set to the string
'-c'. If no script name was passed to the Python interpreter, argv[0]
is the empty string.
import argparse
import sys
# parser code here
# enable -h
args = parser.parse_args()
print(sys.argv)
if len(sys.argv) == 1:
parser.error('First, set the height of the box.')
else:
# throws an error if the arguments are out of order
if sys.argv[1] not in ['-x', '--height']:
parser.error('First, set the height of the box.')
else:
# do something
print(args)
Output:
>test.py -l 7 -b 4 -x 3
['test.py', '-l', '7', '-b', '4', '-x', '3']
usage: test.py [-h] [-x HEIGHT] [-l LENGTH] [-b BREADTH] [--stop]
test.py: error: First, set the height of the box.
Important
It doesn't matter what order the arguments are in, their values are available through args.argumentname
height = args.height
length = args.length
breadth = args.breadth
import argparse
# parser code here
# Namespace object
args = parser.parse_args()
if args.height is None: # not set
parser.error('Set the height of the box.')
else:
# do something
print(args.height + 1)