{{{#!python
class Command(BaseCommand):
def add_arguments(self, parser):
subparsers = parser.add_subparsers(title="subcommands",
dest="subcommand",
required=True)
foo = subparsers.add_parser("foo")
foo.set_defaults(method=self.on_foo_command)
foo.add_argument("--bar")
}}}
In Django, 1.11, this could be called using
{{{#!python
call_command('mycommand', 'foo', bar=True)
}}}
With the additional argument validation in call_command, this generates
`ValueError: min() arg is an empty sequence` at line 124 in
`django/core/management/__init__.py` because the
`_SubParsersAction.option_strings` is an empty array.
{{{#!python
parse_args += [
'{}={}'.format(min(opt.option_strings), arg_options[opt.dest])
for opt in parser._actions if opt.required and opt.dest in options
]
}}}
If the subcommand parser is not tagged as required, `TypeError: Unknown
option(s) for mycommand command: bar` occurs downstream.
The same occurs if the subcommand is passed as an option:
{{{#!python
call_command('mycommand', subcommand='foo', bar=True)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30584>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: Hasan Ramezani (added)
* stage: Unreviewed => Accepted
* version: 2.2 => master
Comment:
Thanks for the report.
In Django 1.11 your custom command raises `TypeError: __init__() missing 1
required positional argument: 'cmd'` which has been fixed in
dd68b51e1da54267bde4799fa0d9fbd4290eb8b5. I wasn't able to run your
example in Django < 2.2.
`call_command('mycommand', 'foo', bar=True)` raises `TypeError: Unknown
option(s) for mycommand command: bar.` which seems fine for me because you
added only `--bar` argument, i.e.
{{{
>>> management.call_command('mycommand', 'foo', '--bar', '1')
>>> management.call_command('mycommand', 'foo', '--bar', True)
}}}
work as expected.
IMO there is only issue with validation of `call_command('mycommand',
subcommand='foo', bar=True)` because it raises
{{{
File "django/core/management/__init__.py", line 130, in <listcomp>
for opt in parser._actions if opt.required and opt.dest in options
ValueError: min() arg is an empty sequence
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30584#comment:1>
* 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>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"2b03e8e9e8205ae3a3aa128764277e70b7c30803" 2b03e8e9]:
{{{
#!CommitTicketReference repository=""
revision="2b03e8e9e8205ae3a3aa128764277e70b7c30803"
Fixed #30584 -- Fixed management command when using subparsers with dest
parameter.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30584#comment:3>