예제 #1
0
    def main(self, signature=""):
        """
        A decorator that is used to register the main function with the given
        `signature`::

            @app.main()
            def main(context):
                # do something
                pass

        The main function is called, after any options and if no command has
        been called.
        """
        signature = Signature.from_string(signature, option=False)

        def decorator(function):
            if self.main_func is not None:
                raise RuntimeError("main is already defined")
            self.main_func = function
            self.main_signature = signature
            if function.__doc__:
                self.description = textwrap.dedent(function.__doc__).strip()
            return function

        return decorator
예제 #2
0
 def from_string(cls, string, function, overrideable=False):
     parts = string.split(" ", 1)
     if not parts or not parts[0]:
         raise InvalidSignature("option name missing")
     names = parts[0]
     if u"|" in names:
         names = names.split(u"|")
     else:
         names = [names]
     for name in names:
         if name.startswith("--"):
             if len(name) == 2:
                 raise InvalidSignature("option with long prefix is missing a name")
         elif name.startswith("-"):
             if len(name) == 1:
                 raise InvalidSignature("option with short prefix is missing a name")
             elif len(name) > 2:
                 raise InvalidSignature("short option with name longer than one character: %s" % name)
     if parts[1:]:
         signature = Signature.from_string(parts[1])
     else:
         signature = Signature([])
     return cls(names, function, signature, overrideable=overrideable)