hug
hug copied to clipboard
Command-line interfaces don't handle keywords
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.
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.
@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
--name1and--name2are not among allowed keywords
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.