[030aeb]: / dosma / utils / cmd_line_utils.py

Download this file

94 lines (72 with data), 3.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
__all__ = ["ActionWrapper"]
class ActionWrapper(object):
"""Wrapper for actions (methods) that can be executed via command-line.
Examples include `segment` scans, `interregister` scans, etc.
Actions are instance methods of classes that can be executed via the command line.
They are typically associated with different scans.
To expose these methods to the command-line interface, we wrap these actions as subparsers.
Parameters for the method are arguments of the subparser.
"""
def __init__(self, name, **kwargs):
self._name = name
self._help = ""
self._param_help = None
self._alternative_param_names = None
self._aliases = []
if "help" in kwargs:
self._help = kwargs.get("help")
if "aliases" in kwargs:
aliases = kwargs.get("aliases")
assert type(aliases) is list, "aliases must be a list"
self._aliases = aliases
if "param_help" in kwargs:
param_help_in = kwargs.get("param_help")
assert type(param_help_in) is dict, "`param_help` must be a dictionary of str->str"
for param_name in param_help_in:
assert type(param_name) is str, "Keys must be of string type"
assert type(param_help_in[param_name]) is str, "Values must be of string type"
self._param_help = dict(param_help_in)
if "alternative_param_names" in kwargs:
alternative_param_names_in = kwargs.get("alternative_param_names")
assert (
type(alternative_param_names_in) is dict
), "`alternative_param_names` must be a dictionary of str->str"
for param_name in alternative_param_names_in:
assert type(param_name) is str, "Keys must be of string type"
assert type(alternative_param_names_in[param_name]) in [
list,
tuple,
], "Values must be of string type"
self._alternative_param_names = alternative_param_names_in
def get_alternative_param_names(self, param: str):
"""Get aliases (alternate names) for a parameter.
Args:
param (str): Action parameter name.
Returns:
Optional[list[str]]: If aliases exist for parameter. `None`, otherwise.
"""
if not self._alternative_param_names or param not in self._alternative_param_names:
return None
return self._alternative_param_names[param]
def get_param_help(self, param: str):
"""Get help menu for a parameter.
Args:
param (str): Action parameter name.
Returns:
str: Help menu for parameter, if initialized. `""`, otherwise.
"""
if not self._param_help or param not in self._param_help:
return ""
return self._param_help[param]
@property
def aliases(self):
"""list[str]: Aliases (other names) for this action."""
return self._aliases
@property
def help(self):
"""str: Help menu for this action."""
return self._help
@property
def name(self):
"""str: Action name."""
return self._name