Initial commit
[ta/lockcli.git] / src / lockcli / processors.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
15 from __future__ import print_function
16 import sys
17 import argparse
18 from lockcli import handlers as clihandlers
19
20
21 class CLIProcessor(object):
22     def __init__(self, prog):
23         self.prog = prog
24
25     def __call__(self, args):
26         parser = argparse.ArgumentParser(description='Lock CLI', prog=self.prog)
27         parser.add_argument('--server',
28                             dest='server',
29                             metavar='SERVER',
30                             required=False,
31                             default='none',
32                             type=str,
33                             action='store')
34
35         parser.add_argument('--port',
36                             dest='port',
37                             metavar='PORT',
38                             required=False,
39                             default=2379,
40                             type=int,
41                             action='store')
42
43         parser.add_argument('--verbose',
44                             required=False,
45                             default=False,
46                             action='store_true')
47
48         subparsers = parser.add_subparsers()
49         handlers = clihandlers.get_handlers_list()
50         for handler in handlers:
51             handler.init_subparser(subparsers)
52
53         parse_result = parser.parse_args(args)
54
55         parse_result.handler(parse_result)
56
57
58 def main():
59     processor = CLIProcessor('lockcli')
60     args = sys.argv[1:]
61     try:
62         processor(args)
63     except Exception as error:  # pylint: disable=broad-except
64         print('Got error %s' % str(error))
65         sys.exit(1)
66
67
68 if __name__ == '__main__':
69     main()