假设要求您编写程序以接受用户对网球大满贯冠军的称号并进行处理。我们已经知道,费德勒和纳达尔在网球比赛中拥有最多的20个大满贯冠军(截至2020年),而最小的则为0,许多球员仍在努力争取自己的第一个大满贯冠军。
让我们创建一个接受标题的程序。
注-从终端执行程序。
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)
输出结果
我们的程序现在可以接受标题了。因此,让我们传递任何数字(非浮点数)作为参数。
<<< 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.
尽管代码没有技术问题,但由于我们的程序接受任何数量的GrandSlam标题(包括否定标题),因此肯定存在功能问题。
在这种情况下,我们想限制GrandSlam标题的选择,我们可以使用choices选项。
在下面的示例中,我们将标题限制为范围(0,20)。
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)
输出结果
>>> 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)
结论:
选项选项采用值列表。如果用户未能提供其中之一,则argparse会停止程序。
用户必须从数字0-19中选择,否则argparse会因错误而停止。
最后,您还可以拥有一个接受字符串选择的程序。
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)
输出结果
<<< 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.