| 23 | |
| 24 | |
| 25 | class DshellArgumentParser(argparse.ArgumentParser): |
| 26 | |
| 27 | def add_plugin_arguments(self, plugin_name, plugin_obj): |
| 28 | """ |
| 29 | add_plugin_arguments(self, plugin_name, plugin_obj) |
| 30 | |
| 31 | Give it the name of the plugin and an instance of the plugin, and |
| 32 | it will automatically create argument entries. |
| 33 | """ |
| 34 | if plugin_obj.optiondict: |
| 35 | group = '{} plugin options'.format(plugin_obj.name) |
| 36 | group = self.add_argument_group(group) |
| 37 | for argname, optargs in plugin_obj.optiondict.items(): |
| 38 | optname = "{}_{}".format(plugin_name, argname) |
| 39 | data_type = optargs.get("type", None) |
| 40 | if data_type and data_type == bytes: |
| 41 | optargs["type"] = custom_bytes |
| 42 | default = optargs.get("default", None) |
| 43 | if default is not None: |
| 44 | optargs["default"] = custom_bytes(default) |
| 45 | group.add_argument("--" + optname, dest=optname, **optargs) |
| 46 | |
| 47 | def get_plugin_arguments(self, plugin_name, plugin_obj): |
| 48 | """ |
| 49 | get_plugin_arguments(self, plugin_name, plugin_obj) |
| 50 | |
| 51 | Returns a list of argument names and the attributes they're associated |
| 52 | with. |
| 53 | |
| 54 | e.g. --country_code for the "country" plugin ties to the "code" attr |
| 55 | in the plugin object. Thus, the return would be |
| 56 | [("country_code", "code"), ...] |
| 57 | """ |
| 58 | args_and_attrs = [] |
| 59 | if plugin_obj.optiondict: |
| 60 | for argname in plugin_obj.optiondict.keys(): |
| 61 | optname = "{}_{}".format(plugin_name, argname) |
| 62 | args_and_attrs.append((optname, argname)) |
| 63 | return args_and_attrs |