2014-07-19 2 views
1

파이썬을 사용하고 argparse 라이브러리는 나는 문자 -을 선도하지 않고 옵션을 통과 할 수있는 방법을 스크립트파이썬은 문자없이 argparse -

parser.add_argument('-s','--status', action='store_true') 

python script.py -s 

에 대한 옵션을 잡을 수 있습니까?

python script.py status 

답변

1

설정 nargs='?' :

$ python test.py --help 
usage: test.py [-h] [status] 

positional arguments: 
    status 

optional arguments: 
    -h, --help show this help message and exit 

$ python test.py my_status 
my_status 
: 여기

import argparse 

parser = argparse.ArgumentParser() 
parser.add_argument('status', nargs='?') 
args = parser.parse_args() 
print args.status 

명령 줄에 무엇을의