파이썬 argparse 작동 방식을 이해하는 데 어려움을 겪고 있습니다.argparse - 코드가 호출되지 않고 실행되는 이유는 무엇입니까?
내 스크립트에는 매개 변수를 취하지 않는 세 가지 위치 인수가 있습니다 (make, 컴파일 및 정리). 설명서와 같이 서브 파서를 통해 해봤지만 내 코드는 인수없이 실행되거나 올바른 함수를 호출하지 않는 인수를 사용하여 실행됩니다 (모든 경우에 Language(). make()가 실행 됨).
내 argparse 코드 :
lang = Language()
parser = argparse.ArgumentParser(description='e-cidadania language catalog generator.')
subparser = parser.add_subparsers()
parser_make = subparser.add_parser('make', help='Create all the language' \
' catalogs for translation,'\
' including JavaScript.')
parser_make.set_defaults(func=lang.make())
parser_compile = subparser.add_parser('compile', help='Compile all the language' \
' catalogs for use.')
parser_compile.set_defaults(func=lang.compile())
parser_clean = subparser.add_parser('clean', help='Delete all the language catalogs.' \
' After this you will'\
' have to rebuild the catalogs' \
' and translate them.')
parser_clean.set_defaults(func=lang.clean())
args = parser.parse_args()
첫 번째 줄은 파일의 유일한 클래스를 호출은 세 가지 주요 방법, make
, clean
및 compile
및 기타 개인 방법, _iterator
및 __init__
와 Language
을했다.
업데이트 전체 스크립트는 여기에 있습니다 : 나는이 문서에서 누락하고 무엇 http://dpaste.com/hold/681317/
이 실행? particullar에서
parser_make.set_defaults(func=lang.make())
,이 부분 : 여기, 자신을 make
를 호출하고 있기 때문이다
목표가 간단한 위치 인수를 처리하는 경우 http://parac.googlecode.com/hg/doc/plac.html http : //packages.python에서 이러한 argparse 래퍼 중 하나를 살펴 보는 것이 좋습니다. org/argh/reference.html –
+1, 그들에 대해 알지 못했습니다 :) –
우리는 서로 서로 돕기 위해 여기에 있습니다. –