我有3個問題。使用argparse需要說明如何正確使用它
1)。我希望能夠使用這個python命令行程序,而不用擔心參數的順序。我以前使用sys.argv,並讓我的用戶使用如下腳本: mypyscript.py create indexname http://localhost:9260 clientMap.json
這就要求我的用戶記住訂單。 我想要的是這樣的: mypyscript.py -i indexname -c create -f clientMap.json -u http://localhost:9260
請注意我是如何毀壞訂單的。 2)。我的代碼中將使用什麼命令行變量作爲條件邏輯 ?我需要通過args.command-type訪問它嗎?短跑是好的? 3)。只有文件到索引是可選參數。我可以傳遞給add_argument一些可選的= True參數或其他東西嗎?我該如何處理?
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-c","--command-type", help="The command to run against ElasticSearch are one of these: create|delete|status")
parser.add_argument("-i","--index_name", help="Name of ElasticSearch index to run the command against")
parser.add_argument("-u", "--elastic-search-url", help="Base URl of ElasticSearch")
parser.add_argument("-f", "--file_to_index", default = 'false', help="The file name of the index map")
args = parser.parse_args()
print args.elastic_search_url
只需使用[docopt](http://docopt.org/) –