summaryrefslogtreecommitdiffstats
path: root/qtip/cli
diff options
context:
space:
mode:
authorTaseer Ahmed <taseer94@gmail.com>2017-01-06 13:31:36 +0500
committerTaseer Ahmed <taseer94@gmail.com>2017-01-06 13:35:25 +0500
commitb1f9a775036c3bb2823ebe881540c480ec05355e (patch)
tree11be1ad35453ff73910b38220b946d614f18e5c4 /qtip/cli
parent0dbc16c41646cbec79b65ec2ac60b8965cd22db9 (diff)
Rework Cli.
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>
Diffstat (limited to 'qtip/cli')
-rw-r--r--qtip/cli/entry.py69
1 files changed, 64 insertions, 5 deletions
diff --git a/qtip/cli/entry.py b/qtip/cli/entry.py
index c3ebf96a..0ecb06cd 100644
--- a/qtip/cli/entry.py
+++ b/qtip/cli/entry.py
@@ -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