가, nargs
의 범위를 수용 할 argparse
의 수단이 아닌 단지 1
, 2
, '+'
, '*'
같은 값.
이 전달하는 값의 수를 확인하는 사용자 정의 Action
클래스를 작성할 수 있습니다. 그러나 파싱 후에도 테스트를 수행하는 것이 쉬울 것이라고 생각합니다.
정말 들어가고 싶다면 nargs
옵션을 추가하는 버그/이슈를 찾을 수 있습니다.
다른 포지션이 없다면, 3 포지션을 정의 할 수 있습니다. 그 중 2 개는 ?
입니다. 일반 이름과 '추가'작업을 통해 하나의 목록으로 결합 할 수도 있습니다. 나는 그것을 탐험 할 것이다.
In [573]: p=argparse.ArgumentParser()
In [574]: p.add_argument('str',action='append')
In [575]: p.add_argument('str',nargs='?',action='append')
In [576]: p.add_argument('str',nargs='?',action='append')
In [577]: p.print_help()
usage: ipython3 [-h] str [str] [str]
positional arguments:
str
str
str
optional arguments:
-h, --help show this help message and exit
In [578]: p.parse_args('1 2 3'.split())
Out[578]: Namespace(str=['1', '2', '3'])
In [579]: p.parse_args('1 3'.split())
Out[579]: Namespace(str=['1', '3', None])
In [580]: p.parse_args('1'.split())
Out[580]: Namespace(str=['1', None, None])
Nots가 있습니다. 나는 교정 할 수
p.add_argument('str', nargs='?', action='append', default=argparse.SUPPRESS)
In [586]: p.parse_args('1 2'.split())
Out[586]: Namespace(str=['1', '2'])
In [588]: p.parse_args('1 2 3 4'.split())
usage: ipython3 [-h] str [str] [str]
ipython3: error: unrecognized arguments: 4
이 (죄송합니다,이 1-3 인수에 대해 코딩,하지만 당신은 아이디어를 얻을)와 함께.
그 복제본의 대답은 내가 제안한 것과 같은 사용자 지정 Action 클래스를 제공합니다. 그러나 포스트 파싱 테스트보다 개선 된 것인지 확신 할 수 없습니다. – hpaulj