Fixed #32153 -- Fixed management commands when using required list options.

Thanks Mark Gajdosik for the report and initial patch.
This commit is contained in:
Hasan Ramezani 2020-10-29 19:30:43 +01:00 committed by Mariusz Felisiak
parent 966b5b49b6
commit f06beea929
4 changed files with 47 additions and 10 deletions

View File

@ -120,7 +120,12 @@ def call_command(command_name, *args, **options):
for s_opt in parser._actions if s_opt.option_strings
}
arg_options = {opt_mapping.get(key, key): value for key, value in options.items()}
parse_args = [str(a) for a in args]
parse_args = []
for arg in args:
if isinstance(arg, (list, tuple)):
parse_args += map(str, arg)
else:
parse_args.append(str(arg))
def get_actions(parser):
# Parser actions and actions from sub-parser choices.
@ -139,15 +144,19 @@ def call_command(command_name, *args, **options):
}
# Any required arguments which are passed in via **options must be passed
# to parse_args().
parse_args += [
min(opt.option_strings)
if isinstance(opt, (_AppendConstAction, _CountAction, _StoreConstAction))
else '{}={}'.format(min(opt.option_strings), arg_options[opt.dest])
for opt in parser_actions if (
for opt in parser_actions:
if (
opt.dest in options and
(opt.required or opt in mutually_exclusive_required_options)
)
]
):
parse_args.append(min(opt.option_strings))
if isinstance(opt, (_AppendConstAction, _CountAction, _StoreConstAction)):
continue
value = arg_options[opt.dest]
if isinstance(value, (list, tuple)):
parse_args += map(str, value)
else:
parse_args.append(str(value))
defaults = parser.parse_args(args=parse_args)
defaults = dict(defaults._get_kwargs(), **arg_options)
# Raise an error if any unknown options were passed.

View File

@ -7,6 +7,7 @@ class Command(BaseCommand):
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--foo-id', type=int, nargs='?', default=None)
group.add_argument('--foo-name', type=str, nargs='?', default=None)
group.add_argument('--foo-list', type=int, nargs='+')
group.add_argument('--append_const', action='append_const', const=42)
group.add_argument('--const', action='store_const', const=31)
group.add_argument('--count', action='count')

View File

@ -0,0 +1,10 @@
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--foo-list', nargs='+', type=int, required=True)
def handle(self, *args, **options):
for option, value in options.items():
self.stdout.write('%s=%s' % (option, value))

View File

@ -244,8 +244,9 @@ class CommandTests(SimpleTestCase):
management.call_command('mutually_exclusive_required', foo_name='foo', stdout=out)
self.assertIn('foo_name', out.getvalue())
msg = (
'Error: one of the arguments --foo-id --foo-name --append_const '
'--const --count --flag_false --flag_true is required'
'Error: one of the arguments --foo-id --foo-name --foo-list '
'--append_const --const --count --flag_false --flag_true is '
'required'
)
with self.assertRaisesMessage(CommandError, msg):
management.call_command('mutually_exclusive_required', stdout=out)
@ -275,6 +276,22 @@ class CommandTests(SimpleTestCase):
)
self.assertIn(expected_output, out.getvalue())
def test_required_list_option(self):
tests = [
(('--foo-list', [1, 2]), {}),
((), {'foo_list': [1, 2]}),
]
for command in ['mutually_exclusive_required', 'required_list_option']:
for args, kwargs in tests:
with self.subTest(command=command, args=args, kwargs=kwargs):
out = StringIO()
management.call_command(
command,
*args,
**{**kwargs, 'stdout': out},
)
self.assertIn('foo_list=[1, 2]', out.getvalue())
def test_required_const_options(self):
args = {
'append_const': [42],