我正在使用带有子解析器的 argparse 来执行不同的操作。每个动作都有略微不同的论点。
我已经按照文档说明进行了设置,当我帮助使用每个子解析器( parser_2
)和另一个子解析器( )时,它会显示正确的参数parser_3
这是为了cdf
:
positional arguments:
repo name the repo to perform tasks on
optional arguments:
-h, --help show this help message and exit
--state {open,closed}
Print issues in a repository having status of
all(default), open, or closed
这是为了clsiss
:
usage: subparsprob.py clsiss [-h] repo issnums [issnums ...]
positional arguments:
repo name the repo to perform tasks on
issnums put the issue number(s) separated by blank
optional arguments:
-h, --help show this help message and exit
但是,当我实际运行命令时,我会遇到使用错误:对于从命令行执行的 clsiss:
PS C:\xxx> python subparsprob.py clsiss repo 1
usage: subparsprob.py clsiss [-h] repo issnums [issnums ...]
subparsprob.py clsiss: error: argument issnums: invalid int value: 'repo'
对于 cdf(从命令行执行):
PS C:\xxx> python subparsprob.py cdf repo
usage: subparsprob.py clsiss [-h] repo issnums [issnums ...]
subparsprob.py clsiss: error: argument issnums: invalid int value: 'repo'
请帮助,我使用了正确的参数和参数数量,但是当我实际尝试运行它时无法弄清楚为什么使用错误
我仍然遇到同样的错误,这是整个代码,我无法弄清楚。请帮忙
#!/usr/bin/python3
import argparse
import sys
import os
argv = sys.argv[1:]
# from issueGithub import IssueGithub, Taskname
def main():
parser=argparse.ArgumentParser(description='Invoke various github
actions')
subparsers = parser.add_subparsers(help='sub-commands for Github
options',dest='action_name')
parser_2 =subparsers.add_parser('clsiss',help='close issues')
parser_2.add_argument('repo',type=str,help="name the repo to perform
tasks
on")
parser_2.add_argument('issnums',type=int,nargs='+',help="put the issue
number(s) separated by blank")
parser_3 = subparsers.add_parser('cdf',help='create default
tasks/issues')
parser_3.add_argument('repo',type=str,help="name the repo to perform
tasks
on")
parser_3.add_argument('--state',choices=['open','closed'], default='all',
help='Print issues in a repository
args = parser.parse_args()
args2 = parser_2.parse_args()
args3 = parser_3.parse_args()
print("Args are")
print(args)
print(args2)
print(args3)
if __name__ =="__main__":
main()