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()
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'