Skip to content

Commit d05c7d0

Browse files
Improve CLI multiple behaviour and builtin type output
1 parent 0704dcd commit d05c7d0

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Ideally, within a virtual environment.
1111

1212
Changelog
1313
=========
14+
### 2.6.0 - August 28, 2019
15+
- Improved CLI multiple behaviour with empty defaults
16+
- Improved CLI type output for built-in types
17+
1418
### 2.5.6 - June 20, 2019
1519
- Fixed issue #815: map_params() causes api documentation to lose param type information
1620
- Improved project testing: restoring 100% coverage

examples/cli_multiple.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import hug
2+
3+
@hug.cli()
4+
def add(numbers: list=None):
5+
return sum([int(number) for number in numbers])
6+
7+
8+
if __name__ == "__main__":
9+
add.interface.cli()

hug/interface.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
text,
4949
)
5050

51+
DOC_TYPE_MAP = {str: "String", bool: "Boolean", list: "Multiple", int: "Integer", float: "Float"}
52+
53+
54+
def _doc(kind):
55+
return DOC_TYPE_MAP.get(kind, kind.__doc__)
56+
5157

5258
def asyncio_call(function, *args, **kwargs):
5359
loop = asyncio.get_event_loop()
@@ -227,7 +233,7 @@ def __init__(self, route, function):
227233
self.output_doc = self.transform.__doc__
228234
elif self.transform or self.interface.transform:
229235
output_doc = self.transform or self.interface.transform
230-
self.output_doc = output_doc if type(output_doc) is str else output_doc.__doc__
236+
self.output_doc = output_doc if type(output_doc) is str else _doc(output_doc)
231237

232238
self.raise_on_invalid = route.get("raise_on_invalid", False)
233239
if "on_invalid" in route:
@@ -310,7 +316,7 @@ def documentation(self, add_to=None):
310316
for requirement in self.requires
311317
]
312318
doc["outputs"] = OrderedDict()
313-
doc["outputs"]["format"] = self.outputs.__doc__
319+
doc["outputs"]["format"] = _doc(self.outputs)
314320
doc["outputs"]["content_type"] = self.outputs.content_type
315321
parameters = [
316322
param
@@ -329,7 +335,7 @@ def documentation(self, add_to=None):
329335
continue
330336

331337
input_definition = inputs.setdefault(argument, OrderedDict())
332-
input_definition["type"] = kind if isinstance(kind, str) else kind.__doc__
338+
input_definition["type"] = kind if isinstance(kind, str) else _doc(kind)
333339
default = self.defaults.get(argument, None)
334340
if default is not None:
335341
input_definition["default"] = default
@@ -505,7 +511,7 @@ def exit(self, status=0, message=None):
505511
if option in self.interface.input_transformations:
506512
transform = self.interface.input_transformations[option]
507513
kwargs["type"] = transform
508-
kwargs["help"] = transform.__doc__
514+
kwargs["help"] = _doc(transform)
509515
if transform in (list, tuple) or isinstance(transform, types.Multiple):
510516
kwargs["action"] = "append"
511517
kwargs["type"] = Text()
@@ -588,9 +594,12 @@ def exit_callback(message):
588594

589595
for field, type_handler in self.reaffirm_types.items():
590596
if field in pass_to_function:
591-
pass_to_function[field] = self.initialize_handler(
592-
type_handler, pass_to_function[field], context=context
593-
)
597+
if not pass_to_function[field] and type_handler in (list, tuple, hug.types.Multiple):
598+
pass_to_function[field] = type_handler(())
599+
else:
600+
pass_to_function[field] = self.initialize_handler(
601+
type_handler, pass_to_function[field], context=context
602+
)
594603

595604
if getattr(self, "validate_function", False):
596605
errors = self.validate_function(pass_to_function)

tests/test_decorators.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,3 +1936,11 @@ def pull_record(record_id: hug.types.number = 1):
19361936

19371937
assert hug.test.get(hug_api, "pull_record").data == 1
19381938
assert hug.test.get(hug_api, "pull_record", id=10).data == 10
1939+
1940+
1941+
def test_multiple_cli(hug_api):
1942+
@hug.cli(api=hug_api)
1943+
def multiple(items: list=None):
1944+
return items
1945+
1946+
hug_api.cli([None, "multiple", "-i", "one", "-i", "two"])

0 commit comments

Comments
 (0)