쉼표로 구분 된 정수 목록을 포함하는 필수 위치 인수를 구문 분석하고 싶습니다. 나는 사람들이 플래그 문자로 -
외에 다른 문자를 사용하는 것이 좋습니다 본 적이argparse를 사용하여 마이너스 기호 (음수)로 위치 인수를 구문 분석하는 방법
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('positional')
parser.add_argument('-t', '--test', action='store_true')
opts = parser.parse_args()
print opts
$ python example.py --test 1,2,3,4
Namespace(positional='1,2,3,4', test=True)
$ python example.py --test -1,2,3,4
usage: example.py [-h] [-t] positional
example.py: error: too few arguments
$ python example.py --test "-1,2,3,4"
usage: example.py [-h] [-t] positional
example.py: error: too few arguments
,하지만 난 오히려 그렇게하지 않는 게 좋을 : - 첫번째 정수 선도적 마이너스가 포함되어있는 경우 ('')는 기호, argparse는 불평 . --test
과 -1,2,3,4
을 모두 유효한 인수로 허용하는 argparse를 구성하는 또 다른 방법이 있습니까?
- 누군가가 이것을 필요로하는 경우에 --test가 인수를하면 다음을 할 수 있습니다 : 'python example.py - test = -1,2,3,4' – lababidi