31

I am trying to write a custom management command in django like below-

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('delay', type=int)

    def handle(self, *args, **options):
        delay = options.get('delay', None)
        print delay

Now when I am running python manage.py mycommand 12 it is printing 12 on console. Which is fine.

Now if I try to run python manage.py mycommand then I want that, the command prints 21 on console by default. But it is giving me something like this-

usage: manage.py mycommand [-h] [--version]
                           [-v {0,1,2,3}]
                           [--settings SETTINGS]
                           [--pythonpath PYTHONPATH]
                           [--traceback]
                           [--no-color]
                           delay

So now, how should I make the command argument "not required" and take a default value if value is not given?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 2
    Wouldn't you want to change this `delay = options.get('delay', None)` to this `delay = options.get('delay', 21)` to handle defaults? Just a guess. – idjaw Feb 25 '16 at 18:21

2 Answers2

62

One of the recipes from the documentation suggests:

For positional arguments with nargs equal to ? or *, the default value is used when no command-line argument was present.

So following should do the trick (it will return value if provided or default value otherwise):

parser.add_argument('delay', type=int, nargs='?', default=21)

Usage:

$ ./manage.py mycommand
21
$ ./manage.py mycommand 4
4
Yaroslav Admin
  • 13,880
  • 6
  • 63
  • 83
10

You can use the dash syntax for optional keyword arguments:

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument("-d", "--delay", type=int)

    def handle(self, *args, **options):
        delay = options["delay"] if options["delay"] else 21
        print(delay)

Use:

$ python manage.py mycommand -d 4
4
$ python manage.py mycommand --delay 4
4
$ python manage.py mycommand
21

Docs:

https://docs.djangoproject.com/en/2.2/howto/custom-management-commands/#s-accepting-optional-arguments

Simple explanation:

https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html#handling-arguments

ICW
  • 4,875
  • 5
  • 27
  • 33
  • That doesn't work because you don't know if the user put in 21 or it defaulted to 21. – noone392 Nov 10 '19 at 15:34
  • @noone392 Thats not true, you can easily check if it defaulted by checking `if options["delay"]`. My example was specific simply setting a value to 21 when `options["delay"]` is falsey, which corresponds to the parameter not being passed and thus is the case where we should use the default value. – ICW Nov 13 '19 at 20:38