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

前端 未结 2 771
梦谈多话
梦谈多话 2021-02-18 19:01

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

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argum         


        
2条回答
  •  面向向阳花
    2021-02-18 19:51

    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
    

提交回复
热议问题