8
|
1 """
|
|
2 methods and properties supporting
|
|
3 command line interface
|
|
4 """
|
|
5
|
|
6
|
|
7 import argparse
|
|
8 import sys
|
|
9 from .cast import keyvalue
|
|
10
|
|
11
|
|
12 class ConfigurationParser(argparse.ArgumentParser):
|
|
13 """parse arguments with validation"""
|
|
14
|
|
15 def __init__(self, **kwargs):
|
|
16 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
|
|
17 argparse.ArgumentParser.__init__(self, **kwargs)
|
|
18 self.add_arguments()
|
|
19 self.options = None
|
|
20
|
|
21 def add_arguments(self):
|
|
22 """add arguments"""
|
|
23 # Abstract Base Class
|
|
24
|
|
25 def parse_args(self, args=sys.argv[1:]):
|
|
26 options = argparse.ArgumentParser.parse_args(self, args)
|
|
27 self.options = self.validate(options)
|
|
28 return self.options
|
|
29
|
|
30 def validate(self, options):
|
|
31 """validate options"""
|
|
32 return options
|
|
33
|
|
34 def keyvalue(self, arg):
|
|
35
|
|
36 try:
|
|
37 return keyvalue(arg)
|
|
38 except AssertionError as e:
|
|
39 self.error(str(e))
|