1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 Command-line interface to yardstick
17 from pkg_resources import get_distribution
18 from argparse import RawDescriptionHelpFormatter
20 from yardstick.cmd.commands import task
24 '''Command-line interface to yardstick'''
28 'task': task.TaskCommands
32 self._version = 'yardstick version %s ' % \
33 get_distribution('yardstick').version
35 def _get_base_parser(self):
36 '''get base (top level) parser'''
38 parser = argparse.ArgumentParser(
40 formatter_class=RawDescriptionHelpFormatter,
41 description=YardstickCLI.__doc__ or ''
48 help="display version",
49 version=self._version,
55 help="increase output verbosity to debug",
61 help="increase output verbosity to info",
67 def _find_actions(self, subparsers, actions_module):
68 '''find action methods'''
69 # Find action methods inside actions_module and
70 # add them to the command parser.
71 # The 'actions_module' argument may be a class
72 # or module. Action methods start with 'do_'
73 for attr in (a for a in dir(actions_module) if a.startswith('do_')):
74 command = attr[3:].replace('_', '-')
75 callback = getattr(actions_module, attr)
76 desc = callback.__doc__ or ''
77 arguments = getattr(callback, 'arguments', [])
78 subparser = subparsers.add_parser(
82 for (args, kwargs) in arguments:
83 subparser.add_argument(*args, **kwargs)
84 subparser.set_defaults(func=callback)
86 def _get_parser(self):
87 '''get a command-line parser'''
88 parser = self._get_base_parser()
90 subparsers = parser.add_subparsers(
95 for category in YardstickCLI.categories:
96 command_object = YardstickCLI.categories[category]()
97 desc = command_object.__doc__ or ''
98 subparser = subparsers.add_parser(
99 category, description=desc,
100 formatter_class=RawDescriptionHelpFormatter
102 subparser.set_defaults(command_object=command_object)
103 cmd_subparsers = subparser.add_subparsers(title='subcommands')
104 self._find_actions(cmd_subparsers, command_object)
108 def main(self, argv):
109 '''run the command line interface'''
111 # get argument parser
112 parser = self._get_parser()
115 args = parser.parse_args(argv)
118 logger = logging.getLogger('yardstick')
119 logger.setLevel(logging.WARNING)
122 logger.getLogger('yardstick').setLevel(logging.INFO)
125 logger.setLevel(logging.DEBUG)
127 # dispatch to category parser