แนะนำตัว..
สมมติว่าคุณถูกขอให้เขียนโค้ดโปรแกรมเพื่อยอมรับจำนวนชื่อเทนนิสแกรนด์สแลมจากผู้ใช้และดำเนินการตามนั้น เรารู้แล้วว่าเฟเดอเรอร์และนาดาลแบ่งปันตำแหน่งแกรนด์สแลมสูงสุดในเทนนิสซึ่งก็คือ 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 เราสามารถใช้ตัวเลือกตัวเลือกได้
ในตัวอย่างต่อไปนี้ เราจำกัดชื่อให้อยู่ในช่วง (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.