add new command line handler
[yardstick.git] / yardstick / cmd / cli.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
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 ##############################################################################
9
10 '''
11 Command-line interface to yardstick
12 '''
13
14 import argparse
15 import logging
16
17 from pkg_resources import get_distribution
18 from argparse import RawDescriptionHelpFormatter
19
20 from yardstick.cmd.commands import task
21
22
23 class YardstickCLI():
24     '''Command-line interface to yardstick'''
25
26     # Command categories
27     categories = {
28         'task': task.TaskCommands
29     }
30
31     def __init__(self):
32         self._version = 'yardstick version %s ' % \
33             get_distribution('yardstick').version
34
35     def _get_base_parser(self):
36         '''get base (top level) parser'''
37
38         parser = argparse.ArgumentParser(
39             prog='yardstick',
40             formatter_class=RawDescriptionHelpFormatter,
41             description=YardstickCLI.__doc__ or ''
42         )
43
44         # Global options
45
46         parser.add_argument(
47             "-V", "--version",
48             help="display version",
49             version=self._version,
50             action="version"
51         )
52
53         parser.add_argument(
54             "-d", "--debug",
55             help="increase output verbosity to debug",
56             action="store_true"
57         )
58
59         parser.add_argument(
60             "-v", "--verbose",
61             help="increase output verbosity to info",
62             action="store_true"
63         )
64
65         return parser
66
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(
79                 command,
80                 description=desc
81             )
82             for (args, kwargs) in arguments:
83                 subparser.add_argument(*args, **kwargs)
84             subparser.set_defaults(func=callback)
85
86     def _get_parser(self):
87         '''get a command-line parser'''
88         parser = self._get_base_parser()
89
90         subparsers = parser.add_subparsers(
91             title='subcommands',
92         )
93
94         # add subcommands
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
101             )
102             subparser.set_defaults(command_object=command_object)
103             cmd_subparsers = subparser.add_subparsers(title='subcommands')
104             self._find_actions(cmd_subparsers, command_object)
105
106         return parser
107
108     def main(self, argv):
109         '''run the command line interface'''
110
111         # get argument parser
112         parser = self._get_parser()
113
114         # parse command-line
115         args = parser.parse_args(argv)
116
117         # handle global opts
118         logger = logging.getLogger('yardstick')
119         logger.setLevel(logging.WARNING)
120
121         if args.verbose:
122             logger.getLogger('yardstick').setLevel(logging.INFO)
123
124         if args.debug:
125             logger.setLevel(logging.DEBUG)
126
127         # dispatch to category parser
128         args.func(args)