0

I am using argparse with subparsers to do different actions. Each action has slightly different arguments.

I have set it up as the documentations instructs, with one action in subparser (parser_2) and the other subparser (parser_3) when i do help for usage of each it says the correct parameters

This is for 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

this is for 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

however when i actually run the commands i get usage errors: for clsiss executing from command line:

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'

for cdf (executing from command line):

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'

please help, I am using the correct arguments and number of argument but cannot figure out why the usage is wrong when i actually try to run it

i am still getting the same error , here is the entire code, I cannot figure it out. Please help

#!/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()
G J
  • 33
  • 6
  • 2
    I can reproduce this error only after switching position of `repo` and `issnums` parser options. Code at the bottom works without errors. Maybe you have typo in code? – mx0 Nov 03 '19 at 20:18
  • Hi thanks for replying switching the position of repo and issnums on command line ? I am not understanding where you are talking about. Can you show me please? – G J Nov 03 '19 at 20:43
  • Code you have pasted at the bottom works OK. Maybe you are running different code? – mx0 Nov 03 '19 at 21:06
  • 1
    Your code has syntax errors and never runs `main`. Please [edit] the question to fix it. Also make sure it's a [mre]. See also [ask] for more tips. – wjandrea Nov 03 '19 at 21:16
  • Yeah, I can't reproduce the problem either (after fixing the problems I mentioned above). – wjandrea Nov 03 '19 at 21:19
  • 1
    The fact that the `cdf` example shows a `clsiss` help message suggests that you are actually running an earlier version of the code, not the one that you show. – hpaulj Nov 03 '19 at 21:46

1 Answers1

0

With a copy-n-paste of your code:

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

I don't get your errors:

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

Your errors - sort of:

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'
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Why the `args2` and `args3` calls? It's the `args2` one that's producing the error. – hpaulj Nov 04 '19 at 03:25
  • Thanks! Darn all i was doing was printing out the different parser arguments (parser_2 and 3). It never dawned on me that is the issue to just print those variables args2 and args3 would cause an issue in usage! thanks everyone for looking with me it has been driving me up the wall – G J Nov 04 '19 at 12:42
  • `parser_2.parse_args([])` gives `parser_2` the same list that `parser` expects 'clsiss repo 1'. But in normal parsing is only gets the 'repo 1' part. The 'clsiss' part is already handled by the main parser. – hpaulj Nov 04 '19 at 17:00