How to make a Django custom management command argument not required?

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)