1

ブール値のフラグと、コマンドの n 引数のオプションが必要です。望ましい使用例:

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_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

これは --ids オプションで機能しますが、 --all オプションでは次のようになりますerror: argument --all: expected one argument

TLDR: オプションとコマンドの両方を使用するにはどうすればよいですか?

4

1 に答える 1

5

試してみてください action='store_true'

例:

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()
于 2015-09-11T14:44:49.867 に答える