diff --git a/moulinette/__init__.py b/moulinette/__init__.py index 4b45a877..c107dc6d 100755 --- a/moulinette/__init__.py +++ b/moulinette/__init__.py @@ -94,8 +94,7 @@ def api(namespaces, host='localhost', port=80, routes={}, 'use_cache': use_cache }) moulinette.run(host, port) -def cli(namespaces, args, print_json=False, print_plain=False, use_cache=True, - parser_kwargs={}): +def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}): """Command line interface Execute an action with the moulinette from the CLI and print its @@ -104,10 +103,10 @@ def cli(namespaces, args, print_json=False, print_plain=False, use_cache=True, Keyword arguments: - namespaces -- The list of namespaces to use - args -- A list of argument strings - - print_json -- True to print result as a JSON encoded string - - print_plain -- True to print result as a script-usable string - use_cache -- False if it should parse the actions map file instead of using the cached one + - output_as -- Output result in another format, see + moulinette.interfaces.cli.Interface for possible values - parser_kwargs -- A dict of arguments to pass to the parser class at construction @@ -120,7 +119,7 @@ def cli(namespaces, args, print_json=False, print_plain=False, use_cache=True, 'parser_kwargs': parser_kwargs, }, ) - moulinette.run(args, print_json, print_plain) + moulinette.run(args, output_as=output_as) except MoulinetteError as e: import logging logging.getLogger('yunohost').error(e.strerror) diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 01650095..a4aeab4f 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -278,7 +278,7 @@ class Interface(BaseInterface): self.actionsmap = actionsmap - def run(self, args, print_json=False, print_plain=False): + def run(self, args, output_as=None): """Run the moulinette Process the action corresponding to the given arguments 'args' @@ -286,11 +286,12 @@ class Interface(BaseInterface): Keyword arguments: - args -- A list of argument strings - - print_json -- True to print result as a JSON encoded string - - print_plain -- True to print result as a script-usable string + - output_as -- Output result in another format. Possible values: + - json: return a JSON encoded string + - plain: return a script-readable output """ - if print_json and print_plain: + if output_as and output_as not in ['json', 'plain']: raise MoulinetteError(errno.EINVAL, m18n.g('invalid_usage')) try: @@ -302,12 +303,13 @@ class Interface(BaseInterface): return # Format and print result - if print_json: - import json - from moulinette.utils.serialize import JSONExtendedEncoder - print(json.dumps(ret, cls=JSONExtendedEncoder)) - elif print_plain: - plain_print_dict(ret) + if output_as: + if output_as == 'json': + import json + from moulinette.utils.serialize import JSONExtendedEncoder + print(json.dumps(ret, cls=JSONExtendedEncoder)) + else: + plain_print_dict(ret) elif isinstance(ret, dict): pretty_print_dict(ret) else: