def _convert_commands(self, container): commands = [] for subcommand_name in iter(sorted(container.get_command_names())): subcmd = container.get_command(subcommand_name) #if not isinstance(subcmd, CommandContainer): # continue cmd = Command(subcommand_name, subcmd.description) if isinstance(subcmd, CommandContainer): subcommands = self._convert_commands(subcmd) for subcommand in subcommands: cmd.add_subcommand(subcommand) # get options parser = FakeParser() subcmd.setup_parser(parser) for op in parser.options: description = op[1].get('help', '') required = '(required)' in description description = description.replace(' (required)', '') cmd.add_option(Option([op[0]], description, required=required)) commands.append(cmd) return commands
def test_command_converts_to_json(self): command = Command('command', 'description',) option = Option(['--name', '-n'], 'description', True) command.add_option(option) subcommand = Command('subcommand', 'description',) command.add_subcommand(subcommand) out = json.dumps(command, sort_keys=True, cls=CustomJSONEncoder) assert_equals(out, '{"description": "description", "name": "command", "options": [{"description": "description", "names": ["--name", "-n"], "required": true}], "subcommands": [{"description": "description", "name": "subcommand", "options": [], "subcommands": []}]}')
def _convert_commands(self, container): commands = [] for subcommand_name in iter(sorted(container.get_command_names())): subcmd = container.get_command(subcommand_name) cmd = Command(subcommand_name, subcmd.description) if isinstance(subcmd, CommandContainer): subcommands = self._convert_commands(subcmd) for subcommand in subcommands: cmd.add_subcommand(subcommand) # get options parser = FakeParser() subcmd.setup_parser(parser) for op in parser.options: description = op[1].get('help', '') required = '(required)' in description cmd.add_option(Option(list(op[0]), description, required=required, dest=op[1].get('dest', None))) commands.append(cmd) return commands