2

I am trying to call the command using args in Django 2.0. When I pass the args it give this error message:

"TypeError: Unknown option(s) for dummy command: args. Valid options are: help, no_color, pythonpath, settings, skip_checks, stderr, stdout, traceback, url, verbosity, version."

The command works fine with options. It only cause this error when called using args.

My command code:

from django.core.management.base import BaseCommand


class Command(BaseCommand):
    help = 'A description of your command'

    def add_arguments(self, parser):
        parser.add_argument(
            '--url', required=False,
            type=str,
            help='the url to process',
        )

    def handle(self, *args, **options):
        for url in args:
            self.stdout.write(url)

Here I call the command

from django.core.management import call_command
from django.test import TestCase


class DummyTest(TestCase):

    def test_dummy_test_case(self):
        call_command("dummy", args=['google.com'])
Faizan
  • 268
  • 2
  • 9

1 Answers1

2

The command argument is set as url, not args; do:

call_command("dummy", url='google.com')

Django management commands use argparse for argument parsing; go through the doc to get more ideas on how this works.

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • That's problematic because the whole reason I want to use `call_command` instead of e.g. `module.management.commands.Command().handle()` is that I want to do an end-to-end test of the command, including arg parsing. – Ben Quigley Mar 11 '21 at 20:37