แนะนำตัว..
Python มีโมดูล argparse ที่ทรงพลังมาก ซึ่งมีฟังก์ชันสำหรับการแยกวิเคราะห์อาร์กิวเมนต์บรรทัดคำสั่ง หากเราต้องการรับอินพุตของผู้ใช้จากบรรทัดคำสั่ง OS โดยไม่ต้องโต้ตอบหรือเขียนโค้ดโปรแกรมที่ยอมรับพารามิเตอร์จากบรรทัดคำสั่งเช่น ระบุ URL เพื่อแยกวิเคราะห์หรือยอมรับไฟล์เพื่ออัปโหลดไปยังบัคเก็ต S3 จากนั้นจึงใช้ argparse ได้อย่างง่ายดาย
การใช้งานพื้นฐาน
-
กำหนดอาร์กิวเมนต์ที่โค้ดของคุณจะยอมรับ
-
เรียกตัวแยกวิเคราะห์อาร์กิวเมนต์เพื่อส่งคืนวัตถุผลลัพธ์
-
ใช้อาร์กิวเมนต์
กล่าวโดยสรุป โครงสร้างของตัวแยกวิเคราะห์อาร์กิวเมนต์มีลักษณะดังนี้
def main( parameters): << Logic here >> if __name__ == '__main__': << 1. Define argument parser >> << 2. Parse the arguements >> << 3. Validation >> << 4. call main (parameters) >>
ฟังก์ชั่นหลักรู้ว่าจุดเริ่มต้นของรหัสของเราคืออะไร ส่วน __name__ =='__main__' จะดำเนินการก็ต่อเมื่อมีการเรียกโค้ดโดยตรงเท่านั้น
-
สร้างโปรแกรมที่จะยอมรับเพียงหนึ่งอาร์กิวเมนต์ - นักเทนนิสเป็นสตริง
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 Two positional arguments',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Adding our first argument player name of type string
parser.add_argument('player',
metavar='player',
type=str,
help='Tennis Player')
return parser.parse_args()
# define main
def main(player):
print(f" *** The {player} had won 20 grandslam titles.")
if __name__ == '__main__':
args = get_args()
main(args.player) ก) ตอนนี้เมื่อคุณรันโปรแกรมนี้จากบรรทัดคำสั่งโดยไม่ผ่านพารามิเตอร์ใด ๆ เช่น หากไม่ระบุอะไรเลย โปรแกรมจะพิมพ์คำสั่งการใช้งานสั้นๆ เกี่ยวกับวิธีการเรียกใช้โปรแกรมอย่างเหมาะสม
In [3]: run <>.ipynb usage: ipython [-h] player ipython: error: the following arguments are required: player An exception has occurred, use %tb to see the full traceback.
ข) ถ้าเราให้อาร์กิวเมนต์มากกว่าหนึ่งข้อ โปรแกรมจะบ่นว่าได้รับอาร์กิวเมนต์ที่สองที่ยังไม่ได้กำหนดไว้
c) เฉพาะเมื่อเราให้โปรแกรมอาร์กิวเมนต์เพียงหนึ่งอาร์กิวเมนต์เท่านั้น โปรแกรมจะทำงาน
2.สร้างโปรแกรมที่จะยอมรับเพียงสองอาร์กิวเมนต์ - ผู้เล่นเทนนิสเป็นชื่อสตริงและแกรนด์สแลมท์ชื่อที่ผู้เล่นชนะเป็นจำนวนเต็ม
ตัวอย่าง
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 Two positional arguments',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Adding our first argument player name of type string
parser.add_argument('player',
metavar='player',
type=str,
help='Tennis Player')
# Adding our second argument player titles of type integer/number.
parser.add_argument('titles',
metavar='titles',
type=int,
help='Tennis Player Grandslam Titles')
return parser.parse_args()
# define main
def main(player, titles):
print(f" *** The {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 20 *** The federer had won 20 grandslam titles.