~fabrixxm/climatik

bc0393ec4cdde6ce768c0690e21e748714ef3bfc — fabrixxm a month ago 2101629
Fix default to print help for groups
2 files changed, 18 insertions(+), 3 deletions(-)

M climatik/__init__.py
M tests/test_command_group.py
M climatik/__init__.py => climatik/__init__.py +2 -2
@@ 47,7 47,7 @@ def get_parser(*args, **kwargs) -> argparse.ArgumentParser:
    Arguments are passed to `argparse.ArgumentParser` constructor
    """
    parser = argparse.ArgumentParser(*args, **kwargs)
    parser.set_defaults(func=lambda *a,**k: parser.print_help())
    parser.set_defaults(func=parser.print_help)
    subparsers = parser.add_subparsers(title="Subcommands")

    for groupname, group in commands.items():


@@ 56,7 56,7 @@ def get_parser(*args, **kwargs) -> argparse.ArgumentParser:
        else:
            grouparser = subparsers.add_parser(groupname,  help=group['help'], description=group['description'])
            groupparsers = grouparser.add_subparsers(title="Subcommands")
            grouparser.set_defaults(func=lambda *a,**k: grouparser.print_help())
            grouparser.set_defaults(func=grouparser.print_help)

        for name, command in group['commands'].items():
            s_parser = groupparsers.add_parser(name, help=command['help'], description=command['description'])

M tests/test_command_group.py => tests/test_command_group.py +16 -1
@@ 100,4 100,19 @@ class TestCommandGroup(ClimatikTest):
        self.assertIn("group     Group help", help)

        help = parser._subparsers._group_actions[0]._name_parser_map['group'].format_help()
        self.assertIn("group description", help)
\ No newline at end of file
        self.assertIn("group description", help)

    def test_group_multiple_default_help(self):
        @command(group_name="a")
        def cmda():
            pass

        @command(group_name="b")
        def cmdb():
            pass

        parser = climatik.get_parser()
        for g in ['', ' a', ' b']:
            nsargs = parser.parse_args(args=g.split())
            r = repr(nsargs.func)
            self.assertIn(f"print_help of ArgumentParser(prog='nose2{g}'", r)