Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / cli / cmcliprocessor.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from __future__ import print_function
15 import sys
16 import argparse
17
18 from cmframework.apis import cmerror
19 from cmframework.cli import cmclihandlers
20
21
22 class CMCLIProcessor(object):
23     def __init__(self, prog):
24         self.prog = prog
25
26     def __call__(self, args):
27         parser = argparse.ArgumentParser(description='Configuration Management CLI', prog=self.prog)
28         parser.add_argument('--ip',
29                             dest='ip',
30                             metavar='SERVER-IP',
31                             default='config-manager',
32                             type=str,
33                             action='store')
34
35         parser.add_argument('--port',
36                             dest='port',
37                             metavar='SERVER-PORT',
38                             default=61100,
39                             type=int,
40                             action='store')
41
42         parser.add_argument('--client-lib',
43                             dest='client_lib',
44                             metavar='CLIENT-LIB',
45                             default='cmframework.lib.CMClientImpl',
46                             type=str,
47                             action='store')
48
49         parser.add_argument('--verbose',
50                             required=False,
51                             default=False,
52                             action='store_true')
53
54         subparsers = parser.add_subparsers()
55         handlers = cmclihandlers.get_handlers_list()
56         for handler in handlers:
57             handler.init_subparser(subparsers)
58
59         parse_result = parser.parse_args(args)
60
61         try:
62             parse_result.handler(parse_result)
63         except cmerror.CMError:
64             raise
65         except Exception as exp:
66             raise cmerror.CMError(str(exp))
67
68
69 def main():
70     processor = CMCLIProcessor('cmcli')
71     args = sys.argv[1:]
72     try:
73         processor(args)
74     except cmerror.CMError as error:
75         print('Got error %s' % str(error))
76         sys.exit(1)
77
78
79 if __name__ == '__main__':
80     main()