0

我正在使用带有子解析器的 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()
4

1 回答 1

0

复制并粘贴您的代码:

1301:~/mypy$ python3 stack58684096.py -h
usage: stack58684096.py [-h] {clsiss,cdf} ...

Invoke various actions

positional arguments:
  {clsiss,cdf}  sub-commands for options
    clsiss      close issues
    cdf         create default tasks/issues

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

我没有得到你的错误:

1302:~/mypy$ python3 stack58684096.py clsiss repo 1
Namespace(action_name='clsiss', issnums=[1], repo='repo')
1302:~/mypy$ python3 stack58684096.py cdf repo
Namespace(action_name='cdf', repo='repo', state='all')

1302:~/mypy$ python3 stack58684096.py clsiss -h
usage: stack58684096.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

你的错误 - 排序:

1302:~/mypy$ python3 stack58684096.py clsiss repo
usage: stack58684096.py clsiss [-h] repo issnums [issnums ...]
stack58684096.py clsiss: error: the following arguments are required: issnums
1304:~/mypy$ python3 stack58684096.py clsiss repo repo
usage: stack58684096.py clsiss [-h] repo issnums [issnums ...]
stack58684096.py clsiss: error: argument issnums: invalid int value: 'repo'
于 2019-11-03T21:03:43.810 回答