How to restrict argument values using choice options in Python?


Introduction..

Assume you are asked to code a program to accept the number of tennis grandslam titles from the user and process them. We already know, Federer and Nadal share the maximum grandslam titles in Tennis which is 20 (As of 2020) while the minimum is 0, lot of players are still fighting to get their first grandslam title.

Let us create a program to accept the titles.

Note - Execute the program from terminal.

Example

import argparse

def get_args():
""" Function : get_args
parameters used in .add_argument
1. metavar - Provide a hint to the user about the data type.
- By default, all arguments are strings.

2. type - The actual Python data type
- (note the lack of quotes around str)

3. help - A brief description of the parameter for the usage

"""

parser = argparse.ArgumentParser(
description='Example for one positional arguments',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

# Adding our first argument player titles of type int
parser.add_argument('titles',
metavar='titles',
type=int,
help='GrandSlam Titles')

return parser.parse_args()

# define main
def main(titles):
print(f" *** Player had won {titles} GrandSlam titles.")

if __name__ == '__main__':
args = get_args()
main(args.titles)

Output

Our program is now ready to accept the titles. So let us pass any number(not float) as argument.

<<< python test.py 20
*** Player had won 20 GrandSlam titles.
<<< python test.py 50
*** Player had won 50 GrandSlam titles.
<<< python test.py -1
*** Player had won -1 GrandSlam titles.
<<< python test.py 30
*** Player had won 30 GrandSlam titles.

While there is no technical issue with the code, there is definetly a functional issue as our program is accepting any number of GrandSlam titles including negative titles.

In such cases where we want to restrict the choices of GrandSlam titles, we can use the choices option.

In the following example, we restrict the titles to a range (0, 20).

Example

import argparse
def get_args():
""" Function : get_args
parameters used in .add_argument
1. metavar - Provide a hint to the user about the data type.
- By default, all arguments are strings.

2. type - The actual Python data type
- (note the lack of quotes around str)

3. help - A brief description of the parameter for the usage

4. choices - pre defined range of choices a user can enter to this program

"""

parser = argparse.ArgumentParser(
description='Example for one positional arguments',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

# Adding our first argument player titles of type int
parser.add_argument('titles',
metavar='titles',
type=int,
choices=range(0, 20),
help='GrandSlam Titles')

return parser.parse_args()

# define main
def main(titles):
print(f" *** Player had won {titles} GrandSlam titles.")

if __name__ == '__main__':
args = get_args()
main(args.titles)

Output

>>> python test.py 30
usage: test.py [-h] titles
test.py: error: argument titles: invalid choice: 30 (choose from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
<<< python test.py 10
*** Player had won 10 GrandSlam titles.
<<< python test.py -1
usage: test.py [-h] titles
test.py: error: argument titles: invalid choice: -1 (choose from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
<<< python test.py 0
*** Player had won 0 GrandSlam titles.
<<< python test.py 20
usage: test.py [-h] titles
test.py: error: argument titles: invalid choice: 20 (choose from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)

Conclusion :

  • The choices option takes a list of values. argparse stops the program if the user fails to supply one of these.

  • The user must choose from the numbers 0-19 or argparse will stop with an error.

Finally, You can also have a program that accepts string choices.

Example

import argparse

def get_args():
""" Function : get_args
parameters used in .add_argument
1. metavar - Provide a hint to the user about the data type.
- By default, all arguments are strings.

2. type - The actual Python data type
- (note the lack of quotes around str)

3. help - A brief description of the parameter for the usage

4. choices - pre defined range of choices a user can enter to this program

"""

parser = argparse.ArgumentParser(
description='Example for one positional arguments',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

# Adding our first argument player names of type str.
parser.add_argument('player',
metavar='player',
type=str,
choices=['federer', 'nadal', 'djokovic'],
help='Tennis Players')

# Adding our second argument player titles of type int
parser.add_argument('titles',
metavar='titles',
type=int,
choices=range(0, 20),
help='GrandSlam Titles')

return parser.parse_args()

# define main
def main(player,titles):
print(f" *** {player} had won {titles} GrandSlam titles.")

if __name__ == '__main__':
args = get_args()
main(args.player,args.titles)

Output

<<< python test.py
usage: test.py [-h] player titles
test.py: error: the following arguments are required: player, titles
<<< python test.py "federer" 30
usage: test.py [-h] player titles
test.py: error: argument titles: invalid choice: 30 (choose from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
<<< python test.py "murray" 5
usage: test.py [-h] player titles
test.py: error: argument player: invalid choice: 'murray' (choose from 'federer', 'nadal', 'djokovic')
<<< python test.py "djokovic" 17
*** djokovic had won 17 GrandSlam titles.

Updated on: 10-Nov-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements