Commits

Anonymous committed 63133da

added best management of exclude groups

Comments (0)

Files changed (1)

     parser = caipyrinha.Caipyrinha(prog="Your Program")
     parser.add_argument("--version", action='version', version="%(prog)s 0.1")
 
-    @parser.callback(exit=0, group="group1")
+    @parser.callback(exit=0, exclusive="group1")
     def first(flags, returns):
         '''Execute this option and exit'''
         print "bye bye"
         '''set his own return value with his parameter'''
         return flags.second
 
-    @parser.callback("--third", group="group1")
+    @parser.callback("--third", exclusive="group1")
     def no_used_name(flags, returns):
         '''you cant use this argument with first'''
         print returns.second
 # META
 #===============================================================================
 
-__version__ = ("0", "1")
+__version__ = ("0", "2")
 __license__ = "WISKEY License"
 __author__ = "JBC"
 __email__ = "jbc dot develop at gmail dot com"
         """
         super(Caipyrinha, self).__init__(*args, **kwargs)
         self._callbacks = collections.OrderedDict()
-        self._groups = {}
+        self._exgroups = {}
 
     def __call__(self, args):
         """self.__call__(args) <=> self(args) <=> self.parse_wc(args)"""
         declared it.
 
         Support the same ``*args`` and ``**kwargs`` of ``add_argument`` method.
-        Also support the ``group`` parameter if you want to set the callback in
-        a mutually exclusive group, and ``exit`` (must be an int and the default
-        is ``None``) if you want to exit after execute this callback using
-        this value as exit code.
+        Also support the ``exclude`` parameter if you want to set the callback
+        in a mutually exclusive group, and ``exit`` (must be an int and the
+        default is ``None``) if you want to exit after execute this callback
+        using this value as exit code.
 
         The function to be decorated must accept 2 arguments ``flags`` (the
         flag status of the parser) and ``returns`` (the return values of the
             kwargs["action"] = kwargs.get("action", "store_true")
             if not len(args):
                 args.append("--{}".format(func.__name__))
-            group = kwargs.pop("group", None)
+            exgroup = kwargs.pop("exclusive", None)
             exit = kwargs.pop("exit", None)
-            if group and group not in self._groups:
-                self._groups[group] = self.add_mutually_exclusive_group()
-            to_add = self._groups[group] if group else self
+            if exgroup and exgroup not in self._exgroups:
+                self.add_mutually_exclusive_group(exgroup)
+            to_add = self._exgroups[exgroup] if exgroup else self
             added = to_add.add_argument(*args, **kwargs)
             self._callbacks[added.dest] = (func, exit)
             return func
 
         return add_argument
 
+    def add_mutually_exclusive_group(self, name, required=False):
+        if name not in self._exgroups:
+            exgroup = super(Caipyrinha, self).add_mutually_exclusive_group()
+            self._exgroups[name] = exgroup
+        group = self._exgroups[name]
+        group.required = required
+        return group
+
     def parse_wc(self, args):
         """Parse the arguments and execute the callbacks.
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.