samba-tool: Fixed bugs to determine min and max # of allowed arguments
authorGiampaolo Lauria <lauria2@yahoo.com>
Mon, 18 Jul 2011 20:48:03 +0000 (16:48 -0400)
committerAndrew Tridgell <tridge@samba.org>
Thu, 21 Jul 2011 01:44:31 +0000 (11:44 +1000)
Fixed the bugs in the code to determine both the min and the max # of allowed arguments
Changed the argument suffix convention from "*" to "+" to represent one or more arguments as:
 1. It follows the Regular expression convention ("*" means 0 or more)
 2. It is what was missing in terms of functionality
NB Currently, no command is using the "*/+", but it is a good thing to have to help out the validation of the args if/when in the future
we have such need

Signed-off-by: Andrew Tridgell <tridge@samba.org>
source4/scripting/python/samba/netcmd/__init__.py

index 05ed321ef990085f0d52507977de2aa96da33bb7..e7ec1f78369c3f8247fd8654af88b418ee28d04d 100644 (file)
@@ -124,17 +124,24 @@ class Command(object):
                 if option.dest is not None:
                     del kwargs[option.dest]
         kwargs.update(optiongroups)
+
+        # Check for a min a max number of allowed arguments, whenever possible
+        # The suffix "?" means zero or one occurence
+        # The suffix "+" means at least one occurence
         min_args = 0
         max_args = 0
+        undetermined_max_args = False
         for i, arg in enumerate(self.takes_args):
-            if arg[-1] not in ("?", "*"):
-                min_args += 1
-            max_args += 1
-            if arg[-1] == "*":
-                max_args = -1
-        if len(args) < min_args or (max_args != -1 and len(args) > max_args):
-            self.usage(*args)
+            if arg[-1] != "?":
+               min_args += 1
+            if arg[-1] == "+":
+               undetermined_max_args = True
+            else:
+               max_args += 1
+        if (len(args) < min_args) or (undetermined_max_args == False and len(args) > max_args):
+            parser.print_usage()
             return -1
+
         try:
             return self.run(*args, **kwargs)
         except Exception, e: