forked from USArmyResearchLab/Dshell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdshellargparse.py
More file actions
40 lines (33 loc) · 1.51 KB
/
Copy pathdshellargparse.py
File metadata and controls
40 lines (33 loc) · 1.51 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
"""
This argument parser is almost identical to the Python standard argparse.
This one adds a function to automatically add plugin-specific arguments.
"""
import argparse
class DshellArgumentParser(argparse.ArgumentParser):
def add_plugin_arguments(self, plugin_name, plugin_obj):
"""
add_plugin_arguments(self, plugin_name, plugin_obj)
Give it the name of the plugin and an instance of the plugin, and
it will automatically create argument entries.
"""
if plugin_obj.optiondict:
group = '{} plugin options'.format(plugin_obj.name)
group = self.add_argument_group(group)
for argname, optargs in plugin_obj.optiondict.items():
optname = "{}_{}".format(plugin_name, argname)
group.add_argument("--" + optname, dest=optname, **optargs)
def get_plugin_arguments(self, plugin_name, plugin_obj):
"""
get_plugin_arguments(self, plugin_name, plugin_obj)
Returns a list of argument names and the attributes they're associated
with.
e.g. --country_code for the "country" plugin ties to the "code" attr
in the plugin object. Thus, the return would be
[("country_code", "code"), ...]
"""
args_and_attrs = []
if plugin_obj.optiondict:
for argname in plugin_obj.optiondict.keys():
optname = "{}_{}".format(plugin_name, argname)
args_and_attrs.append((optname, argname))
return args_and_attrs