diff options
-rw-r--r-- | ipalib/errors.py | 13 | ||||
-rw-r--r-- | ipalib/public.py | 29 | ||||
-rw-r--r-- | ipalib/tests/test_public.py | 21 |
3 files changed, 63 insertions, 0 deletions
diff --git a/ipalib/errors.py b/ipalib/errors.py index 5e8af9d4..a961ecb6 100644 --- a/ipalib/errors.py +++ b/ipalib/errors.py @@ -107,6 +107,19 @@ class IPAError(Exception): return self.format % self.args +class ArgumentError(IPAError): + """ + Raised when a command is called with wrong number of arguments. + """ + + format = '%s %s' + + def __init__(self, command, error): + self.command = command + self.error = error + IPAError.__init__(self, command.name, error) + + class ValidationError(IPAError): """ Base class for all types of validation errors. diff --git a/ipalib/public.py b/ipalib/public.py index c44d039d..8ca97a36 100644 --- a/ipalib/public.py +++ b/ipalib/public.py @@ -236,6 +236,9 @@ class Command(plugable.Plugin): def __init__(self): self.args = plugable.NameSpace(self.__check_args(), sort=False) self.options = plugable.NameSpace(self.__check_options(), sort=False) + self.params = plugable.NameSpace( + tuple(self.args()) + tuple(self.options()), sort=False + ) def get_args(self): return self.takes_args @@ -347,6 +350,32 @@ class Command(plugable.Plugin): for option in sorted(self.options(), key=get_key): yield option + def args_to_kw(self, *args): + Args = tuple(self.args()) + if len(args) > len(Args): + if len(Args) > 0 and not Args[-1].multivalue: + if len(Args) == 1: + error = 'takes at most 1 argument' + else: + error = 'takes at most %d arguments' % len(Args) + raise errors.ArgumentError(self, error) + else: + raise errors.ArgumentError(self, 'takes no arguments') + MinArgs = sum(int(A.required) for A in Args) + if len(args) < MinArgs: + if MinArgs == 1: + error = 'takes at least 1 argument' + else: + error = 'takes at least %d arguments' % MinArgs + raise errors.ArgumentError(self, error) + for (i, Arg) in enumerate(Args): + pass + + + + + + class Object(plugable.Plugin): diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py index 5dcbd84c..6662b9d0 100644 --- a/ipalib/tests/test_public.py +++ b/ipalib/tests/test_public.py @@ -550,6 +550,27 @@ class test_Command(ClassChecker): """ assert 'execute' in self.cls.__public__ # Public + def test_args_to_kw(self): + o = self.__get_instance() + e = raises(errors.ArgumentError, o.args_to_kw, 1) + assert str(e) == 'example takes no arguments' + + o = self.__get_instance(args=('one?',)) + e = raises(errors.ArgumentError, o.args_to_kw, 1, 2) + assert str(e) == 'example takes at most 1 argument' + + o = self.__get_instance(args=('one', 'two?')) + e = raises(errors.ArgumentError, o.args_to_kw, 1, 2, 3) + assert str(e) == 'example takes at most 2 arguments' + + o = self.__get_instance(args=('one', 'two?')) + e = raises(errors.ArgumentError, o.args_to_kw) + assert str(e) == 'example takes at least 1 argument' + + o = self.__get_instance(args=('one', 'two', 'three?')) + e = raises(errors.ArgumentError, o.args_to_kw, 1) + assert str(e) == 'example takes at least 2 arguments' + class test_Object(ClassChecker): """ |