New code structure
[deliverable/lttng-analyses.git] / lttnganalysescli / lttnganalysescli / command.py
CommitLineData
323b3fd6
PP
1import linuxautomaton.automaton
2#import lttnganalysescli
3import argparse
4import sys
5
6
7class Command:
8 def __init__(self, add_arguments_cb, enable_u_opt):
9 self._add_arguments_cb = add_arguments_cb
10 self._enable_u_opt = enable_u_opt
11 self._create_automaton()
12
13 def _error(self, msg, exit_code=1):
14 print(msg, file=sys.stderr)
15 sys.exit(exit_code)
16
17 def _gen_error(self, msg, exit_code=1):
18 self._error('Error: {}'.format(msg), exit_code)
19
20 def _cmdline_error(self, msg, exit_code=1):
21 self._error('Command line error: {}'.format(msg), exit_code)
22
23 def _parse_args(self):
24 ap = argparse.ArgumentParser(description=self._DESC)
25
26 # common arguments
27 ap.add_argument('-b', '--begin', help='begin timestamp')
28 ap.add_argument('-e', '--end', help='end timestamp')
29
30 # optional common argument
31 if self._enable_u_opt:
32 ap.add_argument('-u', '--uuu', help='famous U option')
33
34 # specific arguments
35 self._add_arguments_cb(ap)
36
37 # version of the specific command
38 #version = '%(prog)s v{}'.format(lttnganalysescli.__version__)
39 ap.add_argument('-V', '--version', action='version',
40 version=self._VERSION)
41
42 # parse arguments
43 args = ap.parse_args()
44
45 # common validation
46 if args.begin != 'begin':
47 self._cmdline_error('begin argument should be "begin"')
48
49 if args.end != 'end':
50 self._cmdline_error('end argument should be "end"')
51
52 if self._enable_u_opt:
53 if args.uuu != 'uuu':
54 self._cmdline_error('uuu argument should be "uuu"')
55
56 # transform and save arguments
57 self._arg_begin = len(args.begin)
58 self._arg_end = len(args.end)
59
60 if self._enable_u_opt:
61 self._arg_uuu = len(args.uuu)
62
63 # save all arguments
64 self._args = args
65
66 def _create_automaton(self):
67 self._automaton = linuxautomaton.automaton.Automaton()
This page took 0.024849 seconds and 5 git commands to generate.