Joseph
Joseph

Reputation: 47

How to create custom Flask CLI commands with 2 or more arguments?

I want to make a custom Flask CLI command. It works fine when I use only with 1 argument but it doesn't work for 2 or more arguments. I am using the below code and it fails for 2 or more arguments:

# Usage: flask power 2 3 
# Output: 6
@app.cli.command()
@click.argument('x','y')
def power(x, y):
  print(int(x)**int(y))

The following code works fine with just 1 parameter

# Usage: flask power 2
# Output: 6
@app.cli.command()
@click.argument('x')
def power(x):
  print(int(x)**3)

The error reproduced with 2 or more arguments code is:

flask power 2 3
Traceback (most recent call last):
  File "/home/user/.local/bin/flask", line 8, in <module>
    sys.exit(main())
  File "/home/user/.local/lib/python3.5/site-packages/flask/cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "/home/user/.local/lib/python3.5/site-packages/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/home/user/.local/lib/python3.5/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/user/.local/lib/python3.5/site-packages/click/core.py", line 1254, in invoke
    cmd_name, cmd, args = self.resolve_command(ctx, args)
  File "/home/user/.local/lib/python3.5/site-packages/click/core.py", line 1297, in resolve_command
    cmd = self.get_command(ctx, cmd_name)
  File "/home/user/.local/lib/python3.5/site-packages/flask/cli.py", line 515, in get_command
    rv = info.load_app().cli.get_command(ctx, name)
  File "/home/user/.local/lib/python3.5/site-packages/flask/cli.py", line 372, in load_app
    app = locate_app(self, import_name, name)
  File "/home/user/.local/lib/python3.5/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/projects/challenge/myflaskproj/__init__.py", line 6, in <module>
    from myflaskproj.commands import power, repeat
  File "/projects/challenge/myflaskproj/commands.py", line 6, in <module>
    @click.argument('x','y')
  File "/home/user/.local/lib/python3.5/site-packages/click/decorators.py", line 168, in decorator
    _param_memo(f, ArgumentClass(param_decls, **attrs))
  File "/home/user/.local/lib/python3.5/site-packages/click/core.py", line 1984, in __init__
    Parameter.__init__(self, param_decls, required=required, **attrs)
  File "/home/user/.local/lib/python3.5/site-packages/click/core.py", line 1483, in __init__
    param_decls or (), expose_value
  File "/home/user/.local/lib/python3.5/site-packages/click/core.py", line 2019, in _parse_decls
    " {}".format(len(decls))
TypeError: Arguments take exactly one parameter declaration, got 2

How do I make the command to accept 2 arguments in this method? Am I doing a mistake somewhere? Help would be highly appreciated.

I saw some solutions which use BluePrint and I don't want to use Blueprint to make my job run! The above code is simple and convenient to use. I couldn't figure out where and what I am doing wrong!! The extra help is highly appreciated!!

Upvotes: 2

Views: 3323

Answers (1)

Kusal Hettiarachchi
Kusal Hettiarachchi

Reputation: 603

@click.arguments does not accept multiple parameters this way.

Instead, use

@app.cli.command()
@click.argument('x', nargs=1)
@click.argument('y', nargs=1)
def power(x, y):
    print(int(x)**int(y))

Refer to the documentation here.

Upvotes: 3

Related Questions