#30584: call_command raises ValueError when subparser dest is passed in options.
-------------------------------------+-------------------------------------
Reporter: bill parquet | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):
* owner: nobody => Hasan Ramezani
* status: new => assigned
* has_patch: 0 => 1
Comment:
I think `call_command('mycommand', 'foo', bar=True)` is a valid call. and
as we can see in the question the problem is because the
`_SubParsersAction.option_strings` is an empty array.
so we can fix it by adding a check for `option_strings`.
If we change the question command `foo` argument to `foo.add_argument("--
bar", required=True)` and call it again with `call_command('mycommand',
'foo', bar=True)` , we will face with problem because in the below section
of code
{{{
parse_args += [
'{}={}'.format(min(opt.option_strings), arg_options[opt.dest])
for opt in parser._actions if opt.required and opt.dest in options
]
}}}
we just loop over the `parser._actions`. but `bar` is not in the parser
actions. it is in the choices of foo:
`parser._actions[action_number].choices['foo']._actions`
Also, I think the `call_command('mycommand', subcommand='foo', bar=True)`
is invalid. because it is also invalid in python shell:
{{{
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers(title="subcommands",
dest="subcommand", required=True)
>>> foo = subparsers.add_parser("foo")
>>> foo.add_argument("--bar")
_StoreAction(option_strings=['--bar'], dest='bar', nargs=None, const=None,
default=None, type=None, choices=None, help=None, metavar=None)
>>> print(parser.parse_args(['subcommand=foo', '--bar=True']))
usage: [-h] {foo} ...
: error: argument subcommand: invalid choice: 'subcommand=foo' (choose
from 'foo')
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30584#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.2825b067ebfd421308be8df6a1ef8079%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.