7

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]

2
  • did you try just list - without the quotes ? From the documentation, it looks like type can be any valid simple types. The other (hacky) way is to read the arguments as string, and parse it using ast.literal_eval or something. Commented Nov 3, 2014 at 14:23
  • yeah I tried without the quotes and its the same problem. Commented Nov 3, 2014 at 14:37

2 Answers 2

11

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
Sign up to request clarification or add additional context in comments.

4 Comments

sorry, thats an interesting idea, but it throws an exception: optparse.OptionError: option --vpds: invalid option type: 'list'
Removing type='list' fixes it. Updated the answer.
Can anyone point me out to better info on the action values? I'm looking here without finding much info on what the available do or what values I can use
@domdambrogia the docs actually say to check docs.python.org/3/library/argparse.html#action docs
3

You can also do it like this:

        parser.add_argument(
            "--vpds",
            nargs="*",
            help="vpds list",
            default=[],
            type=int,
        )

The usage is as follows:

python manage.py my_command --vpds 1 3 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.