반응형
내가 필요한 것은 :
pro [-a xxx | [-b yyy -c zzz]]
나는 이것을 시도했지만 작동하지 않습니다. 누군가 나를 도울 수 있습니까?
group= parser.add_argument_group('Model 2')
group_ex = group.add_mutually_exclusive_group()
group_ex.add_argument("-a", type=str, action = "store", default = "", help="test")
group_ex_2 = group_ex.add_argument_group("option 2")
group_ex_2.add_argument("-b", type=str, action = "store", default = "", help="test")
group_ex_2.add_argument("-c", type=str, action = "store", default = "", help="test")
감사!
해결 방법
add_mutually_exclusive_group
은 전체 그룹을 상호 배타적으로 만들지 않습니다. 그룹 내의 옵션을 상호 배타적으로 만듭니다.
prog
command 1
-a: ...
command 2
-b: ...
-c: ...
첫 번째 인수 세트로 호출하려면 다음을 수행하십시오.
prog command_1 -a xxxx
두 번째 인수 세트를 사용하여 호출하려면 다음을 수행하십시오.
prog command_2 -b yyyy -c zzzz
하위 명령 인수를 위치로 설정할 수도 있습니다.
prog command_1 xxxx
git 또는 svn과 같은 종류 :
git commit -am
git merge develop
# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', action='store_true', help='help for foo arg.')
subparsers = parser.add_subparsers(help='help for subcommand')
# create the parser for the "command_1" command
parser_a = subparsers.add_parser('command_1', help='command_1 help')
parser_a.add_argument('a', type=str, help='help for bar, positional')
# create the parser for the "command_2" command
parser_b = subparsers.add_parser('command_2', help='help for command_2')
parser_b.add_argument('-b', type=str, help='help for b')
parser_b.add_argument('-c', type=str, action='store', default='', help='test')
>>> parser.print_help()
usage: PROG [-h] [--foo] {command_1,command_2} ...
positional arguments:
{command_1,command_2}
help for subcommand
command_1 command_1 help
command_2 help for command_2
optional arguments:
-h, --help show this help message and exit
--foo help for foo arg.
>>>
>>> parser.parse_args(['command_1', 'working'])
Namespace(a='working', foo=False)
>>> parser.parse_args(['command_1', 'wellness', '-b x'])
usage: PROG [-h] [--foo] {command_1,command_2} ...
PROG: error: unrecognized arguments: -b x
행운을 빕니다.
참조 페이지 https://stackoverflow.com/questions/17909294
반응형
'파이썬' 카테고리의 다른 글
파이썬 목록의 모든 요소에 논리 연산자를 적용하는 방법 (0) | 2021.01.12 |
---|---|
파이썬 Python 3 앱을 .exe로 어떻게 컴파일합니까? (0) | 2021.01.12 |
파이썬 Python에서 목록 시작 부분에 정수 추가 (0) | 2021.01.12 |
파이썬 튜플에 Python의 요소가 포함되어 있는지 확인하는 방법은 무엇입니까? (0) | 2021.01.12 |
파이썬 What is actually assertEquals in Python? (0) | 2021.01.12 |
댓글