Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command-line interfaces don't handle keywords #873

Open
arogozhnikov opened this issue Oct 4, 2020 · 3 comments
Open

Command-line interfaces don't handle keywords #873

arogozhnikov opened this issue Oct 4, 2020 · 3 comments

Comments

@arogozhnikov
Copy link

Contents for testhug.py

import hug

def hello_world(name1: str, name2: str):
    return f"Hello {name1} and {name2}"

if __name__ == '__main__':
    api = hug.API(__name__)
    hug.cli('hello_world', api=api)(hello_world)
    api.cli()
> python testhug.py hello_world --name1 1 --name2 2
Hello 1 and 2
> python testhug.py hello_world --name2 1 --name1 2
Hello 1 and 2
> python testhug.py hello_world --name2 1 --name1123 2
Hello 1 and 2

Keywords are ignored. I see CLI is not in priority, but if that's expected behavior, this should be documented well (one wouldn't expect CLI to behave like this).

Liked concept a lot though.

@Peter200lx
Copy link
Contributor

Currently you have no default values for the parameters, making them positional instead of keyword arguments:

$ python testhug.py hello_world --help
usage: testhug.py [-h] name1 name2

positional arguments:
  name1       String
  name2       String

optional arguments:
  -h, --help  show this help message and exit

If you modify the code to have default values then you can address them by keywords:

import hug

def hello_world(name1: str = "First", name2: str = "Second"):
    return f"Hello {name1} and {name2}"

if __name__ == '__main__':
    api = hug.API(__name__)
    hello_world = hug.cli('hello_world', api=api)(hello_world)
    api.cli()
$ python testhug.py hello_world --help
usage: testhug.py [-h] [-n NAME1] [-na NAME2]

optional arguments:
  -h, --help            show this help message and exit
  -n NAME1, --name1 NAME1
                        String
  -na NAME2, --name2 NAME2
                        String
$ python testhug.py hello_world --name1 2 --name2 1
Hello 2 and 1
$ python testhug.py hello_world --name1 2
Hello 2 and Second

In this case I think the primary confusion for this issue is that hug is ignoring keywords it does not recognize instead of warning the user about unexpected arguments.

@arogozhnikov
Copy link
Author

arogozhnikov commented Oct 4, 2020

@Peter200lx sure, I've noticed that.

However that's not expected behavior. Possible options are

  • allow passing positional arguments by name (just as in python). Python has / to mark arguments that should not be called by name
  • raise an error and inform that --name1 and --name2 are not among allowed keywords

@Peter200lx
Copy link
Contributor

General CLI patterns doesn't really agree with the first option, positional arguments and keyword arguments are two very different things. In the last line of my previous comment I agree that it is bad behavior that HUG ignores keyword arguments that it doesn't recognize. You can see this as you can pass any keyword argument:

$ python testhug.py hello_world --asdf --doesnt-exist
Hello First and Second

I believe the bug here is that hug is ignoring CLI options it doesn't specifically recognize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants