diff --git a/IPython/core/magic_arguments.py b/IPython/core/magic_arguments.py index ec539f8..15e0ebb 100644 --- a/IPython/core/magic_arguments.py +++ b/IPython/core/magic_arguments.py @@ -173,11 +173,17 @@ class magic_arguments(ArgDecorator): return func -class argument(ArgDecorator): - """ Store arguments and keywords to pass to add_argument(). +class ArgMethodWrapper(ArgDecorator): - Instances also serve to decorate command methods. """ + Base class to define a wrapper for ArgumentParser method. + + Child class must define either `_method_name` or `add_to_parser`. + + """ + + _method_name = None + def __init__(self, *args, **kwds): self.args = args self.kwds = kwds @@ -187,36 +193,31 @@ class argument(ArgDecorator): """ if group is not None: parser = group - parser.add_argument(*self.args, **self.kwds) + getattr(parser, self._method_name)(*self.args, **self.kwds) return None -class defaults(ArgDecorator): - """ Store arguments and keywords to pass to set_defaults(). +class argument(ArgMethodWrapper): + """ Store arguments and keywords to pass to add_argument(). Instances also serve to decorate command methods. """ - def __init__(self, *args, **kwds): - self.args = args - self.kwds = kwds + _method_name = 'add_argument' - def add_to_parser(self, parser, group): - """ Add this object's information to the parser. - """ - if group is not None: - parser = group - parser.set_defaults(*self.args, **self.kwds) - return None +class defaults(ArgMethodWrapper): + """ Store arguments and keywords to pass to set_defaults(). -class argument_group(ArgDecorator): + Instances also serve to decorate command methods. + """ + _method_name = 'set_defaults' + + +class argument_group(ArgMethodWrapper): """ Store arguments and keywords to pass to add_argument_group(). Instances also serve to decorate command methods. """ - def __init__(self, *args, **kwds): - self.args = args - self.kwds = kwds def add_to_parser(self, parser, group): """ Add this object's information to the parser.