Ejemplo n.º 1
0
 def execute(self):
     """Executes the specified command.
     """
     parser = ArgumentParser(add_help=False)
     help_args = ('-h', '--help')
     help = False
     command = None
     try:
         for help_arg in help_args:
             if help_arg in self._argv:
                 help = self._argv.pop(self._argv.index(help_arg))
         if self._argv:
             command = self.get_command(self._argv.pop(0))
             if command:
                 parser.add_arguments(command.arguments)
                 parser.description = command.help
                 parser.usage = self.get_command_usage(command)
     except:
         raise
     if not command:
         parser.usage = self.available_commands_usage
         help = True
     if help:
         parser.print_help()
     if command and not help:
         try:
             command.parsed_args = parser.parse_args(self._argv)
             return command()
         except ConsoleError as exc:
             sys.stderr.write(
                 colors.fail('Error: {0}\n'.format(str(exc).strip("'"))))
Ejemplo n.º 2
0
 def execute(self):
     """Executes the specified command.
     """
     parser = ArgumentParser(add_help=False)
     help_args = ('-h', '--help')
     help = False
     command = None
     try:
         for help_arg in help_args:
             if help_arg in self._argv:
                 help = self._argv.pop(self._argv.index(help_arg))
         if self._argv:
             command = self.get_command(self._argv.pop(0))
             if command:
                 parser.add_arguments(command.arguments)
                 parser.description = command.help
                 parser.usage = self.get_command_usage(command)
     except:
         raise
     if not command:
         parser.usage = self.available_commands_usage
         help = True
     if help:
         parser.print_help()
     if command and not help:
         try:
             command.parsed_args = parser.parse_args(self._argv)
             return command()
         except ConsoleError as exc:
             sys.stderr.write(
                 colors.fail('Error: {0}\n'.format(str(exc).strip("'"))))
Ejemplo n.º 3
0
 def _handle_exc(self, exc):
     exc_msg = str(exc).strip("'")
     sys.stderr.write(colors.fail('Error: {0}\n'.format(exc_msg)))
     sys.exit(1)
Ejemplo n.º 4
0
    def attach_commands(self, parser, namespace):
        """Register the commands against the parser.

        Args:
            parser: The parser to add commands to
            namespace: The namespace the commands should sit within
        """
        subparsers = parser.add_subparsers()
        namespaces = {}
        for command_class in self.commands:
            command_class = self.get_command(command_class)
            command_namespace = command_class.cased_name()
            if command_namespace not in namespaces:
                namespaces[command_namespace] = []
            command_help = command_class.help()
            # if namespace in self.commands:
            methods = [cmethod for cmethod in dir(command_class)
                       if hasattr(getattr(command_class, cmethod), 'is_cli_command')]
            for a_command in methods:
                command = getattr(command_class, a_command)
                namespaces[command_namespace].append((a_command, command.__func_doc__))
                if command_namespace == namespace:
                    subparser = subparsers.add_parser(
                        a_command,
                        help=command.__func_doc__,
                        description=command.__desc__)
                    for arg, kwargs in command.__args__:
                        subparser.add_argument(arg, **kwargs)
                    subparser.set_defaults(
                        command=(
                            command_class,
                            a_command,
                            command.__args_mapping__))
                    self.update_usage(
                        subparser, namespace, is_subparser=True)
            if command_namespace == namespace:
                # when viewing a command
                parser.description = command_help
                self.update_usage(parser, namespace)
                return
            if not namespace:
                # when viewing the namespaces
                subparsers.add_parser(
                    command_namespace,
                    description=command_help,
                    help=command_help)
        self.update_usage(parser, namespace)
        if not namespace or namespace not in namespaces:
            # Override the default output from argparse to display all the
            # namespaces and their commands.
            parser.print_usage()
            self.write()
            longest_namespace = max(namespaces, key=len)
            length = len(longest_namespace)
            for namespace in sorted(namespaces):
                commands = namespaces[namespace]
                self.write(colors.fail(styles.bold(namespace.ljust(length))))
                longest_command = max([command[0] for command in commands], key=len)
                longest_command_length = len(longest_command)
                for command, help in sorted(commands):
                    command = styles.bold(command.ljust(longest_command_length))
                    self.write('    {}\t{}'.format(command, help))
                self.write()
            sys.exit(0)
Ejemplo n.º 5
0
 def test_fail(self):
     assert fail('test') == '\033[91mtest\033[0m'
     assert fail('test', terminate=False) == '\033[91mtest'