#30584 closed Bug (fixed)

call_command raises ValueError when subparser dest is passed in options.

Reported by: bill parquet Owned by: Hasan Ramezani
Component: Core (Management commands) Version: master
Severity: Normal Keywords:
Cc: Hasan Ramezani Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If a management command contains subparsers:

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

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.

    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:

call_command('mycommand', subcommand='foo', bar=True)

Change History (3)

comment:1 Changed 14 months ago by felixxm

Cc: Hasan Ramezani added
Summary: call_command doesn't recognize subparsers for parameter validationcall_command raises ValueError when subparser dest is passed in options.
Triage Stage: UnreviewedAccepted
Version: 2.2master

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

comment:2 Changed 13 months ago by Hasan Ramezani

Has patch: set
Owner: changed from nobody to Hasan Ramezani
Status: newassigned

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

PR

Last edited 13 months ago by felixxm (previous) (diff)

comment:3 Changed 13 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 2b03e8e9:

Fixed #30584 -- Fixed management command when using subparsers with dest parameter.

Note: See TracTickets for help on using tickets.
Back to Top