질문이 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). file-to-index 만 선택적 매개 변수입니다. add_argument에 some optional = 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/)를 사용하십시오 –