[英]How do I pass the argument correctly in Python
我的一個小腳本有問題。 我想用一個參數( --color BLUE
)打開程序。
顏色定義如下:
BLUE = bytearray(b'\x00\x00\xff')
解析器如下所示:
common_parser.add_argument('--color', dest='color', action='store', required=False, default=BLUE, help='set the color')
現在,我使用參數--color YELLOW
啟動腳本時,它僅讀取“ Y”並對其進行處理。 它不指向字節數組。 如何正確通過?
定義顏色及其相應對象的字典:
COLORS = {
'BLUE': bytearray(b'\x00\x00\xff'),
'GREEN': bytearray(b'\x00\xff\x00'),
'RED': bytearray(b'\xff\x00\x00')
}
將add_argument
調用更改為:
common_parser.add_argument('--color', dest='color', required=False, default="BLUE", help='set the color')
現在,您可以使用參數通過鍵(都是字符串)來查找顏色:
color = COLORS[args.color]
其中args
是argparse
解析的命令行參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.