Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a parameter of type Array into a Django Command?

It is straight forward creating a string parameter such as --test_email_address below.

   class Command(BaseCommand):
        option_list = BaseCommand.option_list + (
            make_option('--test_email_address',
                        action='store',
                        type="string",
                        dest='test_email_address',
                        help="Specifies test email address."),
            make_option('--vpds',
                        action='store',
                        type='list',           /// ??? Throws exception
                        dest='vpds',
                        help="vpds list [,]"),
        )

But how can I define a list to be passed in? such as [1, 3, 5]

like image 882
Houman Avatar asked Nov 03 '14 14:11

Houman


People also ask

How do you pass an array as a command line argument in Python?

sys. argv is used in python to retrieve command line arguments at runtime. For a program to be able to use it, it has to be imported from the “sys” module. The arguments so obtained are in the form of an array named sys.

What is Django-admin commands?

django-admin is Django's command-line utility for administrative tasks. This document outlines all it can do. In addition, manage.py is automatically created in each Django project.


1 Answers

You should add a default value and change the action to 'append':

make_option('--vpds',
            action='append',
            default=[],
            dest='vpds',
            help="vpds list [,]"),

The usage is as follows:

python manage.py my_command --vpds arg1 --vpds arg2
like image 135
Yossi Avatar answered Sep 28 '22 06:09

Yossi