1

I want to have a boolean flag and an option that n arguments for my command. Desired example usage:

python manage.py my_command --all # Execute my_command with all id's

python manage.py my_command --ids id1 id2 id3 ... # Execute my_command with n ids

python manage.py my_command --all --ids id1 id2 id3 ... # Throw an error

My function looks like this right now (The body of the function also has the logic to throw an error if both are provided):

@my_command_manager.option("--all", dest="all_ids", default=False, help="Execute for all ids.")
@my_command_manager.option("--ids", dest="ids", nargs="*", help="The ids to execute.")
def my_command(ids, all_ids=False): #do stuff

This works for the --ids option, but the --all option says: error: argument --all: expected one argument.

TLDR: How can I have both an option and a command?

goodcow
  • 4,495
  • 6
  • 33
  • 52
  • Please provide the shortest **complete** program that demonstrates the problem. See http://stackoverflow.com/help/mcve – Robᵩ Sep 11 '15 at 14:19

1 Answers1

5

Try action='store_true'.

Example:

from flask import Flask
from flask.ext.script import Manager

app = Flask(__name__)
my_command_manager = Manager(app)

@my_command_manager.option(
    "--all",
    dest="all_ids",
    action="store_true",
    help="Execute for all ids.")
@my_command_manager.option(
    "--ids",
    dest="ids",
    nargs="*",
    help="The ids to execute.")
def my_command(ids, all_ids):
    print(ids, all_ids)


if __name__ == "__main__":
    my_command_manager.run()
Robᵩ
  • 163,533
  • 20
  • 239
  • 308