Example #1
0
    def available_commands_usage(self):
        """Returns the usage text for all commands.

        This is used when no commands have been specified.
        """
        help = '\n'.join(['{usage}', '', 'Commands:\n    {commands}', ''])
        commands = []
        for name, command in self.commands.items():
            commands.append('{0}: {1}'.format(styles.bold(name), command.help))

        return help.format(commands="\n    ".join(commands), usage=self.usage)
Example #2
0
    def available_commands_usage(self):
        """Returns the usage text for all commands.

        This is used when no commands have been specified.
        """
        help = '\n'.join([
            '{usage}',
            '',
            'Commands:\n    {commands}',
            ''
        ])
        commands = []
        for name, command in self.commands.items():
            commands.append('{0}: {1}'.format(styles.bold(name), command.help))

        return help.format(commands="\n    ".join(commands),
                           usage=self.usage)
Example #3
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)
Example #4
0
 def test_bold(self):
     assert bold('test') == '\033[1mtest\033[0m'
     assert bold('test', terminate=False) == '\033[1mtest'
Example #5
0
 def test_bold(self):
     assert bold('test') == '\033[1mtest\033[0m'
     assert bold('test', terminate=False) == '\033[1mtest'