Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / common / vstfcli.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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 import argparse
11 import sys
12
13
14 class VstfHelpFormatter(argparse.HelpFormatter):
15
16     def start_section(self, heading):
17         # Title-case the headings
18         heading = '%s%s' % (heading[0].upper(), heading[1:])
19         super(VstfHelpFormatter, self).start_section(heading)
20
21
22 class VstfParser(argparse.ArgumentParser):
23
24     def __init__(self,
25                  prog='vstf',
26                  description="",
27                  epilog='',
28                  add_help=True,
29                  formatter_class=VstfHelpFormatter):
30
31         super(VstfParser, self).__init__(
32             prog=prog,
33             description=description,
34             epilog=epilog,
35             add_help=add_help,
36             formatter_class=formatter_class)
37         self.subcommands = {}
38
39     def _find_actions(self, subparsers, actions_module):
40         for attr in (a for a in dir(actions_module) if a.startswith('do_')):
41             command = attr[3:].replace('_', '-')
42             callback = getattr(actions_module, attr)
43             desc = callback.__doc__ or ''
44             action_help = desc.strip()
45             arguments = getattr(callback, 'arguments', [])
46             subparser = subparsers.add_parser(
47                 command,
48                 help=action_help,
49                 description=desc,
50                 add_help=False,
51                 formatter_class=VstfHelpFormatter)
52             subparser.add_argument('-h', '--help',
53                                    action='help',
54                                    help=argparse.SUPPRESS)
55             self.subcommands[command] = subparser
56             for (args, kwargs) in arguments:
57                 subparser.add_argument(*args, **kwargs)
58             subparser.set_defaults(func=callback)
59
60     def set_subcommand_parser(self, target, metavar="<subcommand>"):
61         subparsers = self.add_subparsers(metavar=metavar)
62         self._find_actions(subparsers, target)
63         return subparsers
64
65     def set_parser_to_subcommand(self, subparser, target):
66         self._find_actions(subparser, target)
67
68
69 if __name__ == "__main__":
70     from vstf.common import test_func
71     parser = VstfParser(prog="vstf", description="test parser")
72     parser.set_subcommand_parser(test_func)
73     args = parser.parse_args(sys.argv[1:])
74     args.func(args)