Rework Cli. 59/26759/1
authorTaseer Ahmed <taseer94@gmail.com>
Fri, 6 Jan 2017 08:31:36 +0000 (13:31 +0500)
committerTaseer Ahmed <taseer94@gmail.com>
Fri, 6 Jan 2017 08:35:25 +0000 (13:35 +0500)
Allows global arguments to be passed to each group.

References:
https://github.com/pallets/click/blob/master/examples/complex/complex/cli.py

Change-Id: Ifed191add867b35ddfff92de0c7db4a03d67d091
Signed-off-by: Taseer Ahmed <taseer94@gmail.com>
qtip/cli/entry.py
tests/unit/cli/test_module.py [deleted file]
tests/unit/cli/test_options.py [new file with mode: 0644]

index c3ebf96..0ecb06c 100644 (file)
@@ -7,13 +7,72 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+import os
+import sys
 import click
-from qtip.cli.commands import cmd_perftest
-from qtip.cli.commands import cmd_suite
+
 
 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
 
-cli = click.CommandCollection(sources=[cmd_perftest.cli, cmd_suite.cli])
 
-if __name__ == '__main__':
-    cli()
+class Context(object):
+
+    ''' TODO(taseer) implementation'''
+
+    def __init__(self):
+        self.verbose = False
+        self.debug = False
+        self.version = 'qtip-1.0.0'
+
+    def log(self, msg, *args):
+        ''' Log message to stderr '''
+        pass
+
+    def verbose(self, msg, *args):
+        ''' Log message to stderr when verbose '''
+        pass
+
+    def version(self):
+        ''' Display programs version '''
+        pass
+
+    def debug(self, msg, *args):
+        ''' Log message to debug '''
+        pass
+
+
+pass_context = click.make_pass_decorator(Context, ensure=True)
+cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__),
+                                          'commands'))
+
+
+class QtipCli(click.MultiCommand):
+
+    def list_commands(self, ctx):
+        rv = []
+        for filename in os.listdir(cmd_folder):
+            if filename.endswith('.py') and \
+               filename.startswith('cmd_'):
+                rv.append(filename[4:-3])
+        rv.sort()
+        return rv
+
+    def get_command(self, ctx, name):
+        try:
+            if sys.version_info[0] == 2:
+                name = name.encode('ascii', 'replace')
+            mod = __import__('qtip.cli.commands.cmd_' + name,
+                             None, None, ['cli'])
+        except ImportError:
+            return
+        return mod.cli
+
+
+@click.command(cls=QtipCli, context_settings=CONTEXT_SETTINGS,
+               invoke_without_command=True)
+@click.option('-v', '--verbose', is_flag=True, help='Enable verbose mode.')
+@click.option('-d', '--debug', is_flag=True, help='Enable debug mode.')
+@click.option('--version', is_flag=True, help='Shows program current version.')
+@pass_context
+def cli(ctx, verbose, version, debug):
+    pass
diff --git a/tests/unit/cli/test_module.py b/tests/unit/cli/test_module.py
deleted file mode 100644 (file)
index a890068..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-import os
-import pytest
-import sys
-import yaml
-
-from click.testing import CliRunner
-from qtip.cli.entry import cli
-
-
-class TestClass(object):
-    '''TODO(taseer) Remove hardcoded behaviour '''
-
-    @pytest.fixture()
-    def runner(self):
-        return CliRunner()
-
-    def test(self, runner):
-        unit = 'perftest'
-        test_file = 'data/helper/' + unit + '.yaml'
-        path = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, test_file)
-
-        with open(path) as trial:
-            content = trial.read()
-            data = yaml.safe_load(content)['tests']
-            if data is None:
-                print("Unit Test does not exist")
-                sys.exit(1)
-            else:
-                for i in range(0, len(data)):
-                    result = runner.invoke(cli, data[i]['command'])
-                    assert result.output == data[i]['output']
diff --git a/tests/unit/cli/test_options.py b/tests/unit/cli/test_options.py
new file mode 100644 (file)
index 0000000..5129b5c
--- /dev/null
@@ -0,0 +1,32 @@
+###############################################################
+# Copyright (c) 2016 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import pytest
+from click.testing import CliRunner
+
+from qtip.cli.entry import cli
+
+
+class TestClass(object):
+
+    @pytest.fixture()
+    def runner(self):
+        return CliRunner()
+
+    def test_verbose(self, runner):
+        result = runner.invoke(cli, ['-v'])
+        assert result.output == ''
+
+    def test_version(self, runner):
+        result = runner.invoke(cli, ['--version'])
+        assert '' in result.output
+
+    def test_debug(self, runner):
+        result = runner.invoke(cli, ['-d'])
+        assert '' in result.output