pythondjangodjango-manage.pydjango-commands

Django custom command not working with args


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

Solution

  • 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.