我試着學習如何argparse.ArgumentParser
作品,我已經寫了這幾行:使用Python argparse.ArgumentParser方法
global firstProduct
global secondProduct
myparser=argparse.ArgumentParser(description='parser test')
myparser.add_argument("product1",help="enter product1",dest='product_1')
myparser.add_argument("product2",help="enter product2",dest='product_2')
args=myparser.parse_args()
firstProduct=args.product_1
secondProduct=args.product_2
我只是想,當用戶運行此腳本有兩個參數的我代碼分別將它們分配給firstProduct
和secondProduct
。但它不起作用。有沒有人告訴我爲什麼?在此先感謝
你應該描述錯誤,而不只是說'它不工作'。錯誤消息:「ValueError:dest爲位置參數提供兩次」非常重要。 unutbu的回答解決了這個問題。 – hpaulj