Skip to content

Choices/parsers should reuse the formatter_class defined on the ArgumentParser #238

@petergaultney

Description

@petergaultney
from rich_argparse import ArgumentDefaultsRichHelpFormatter

parser = argparse.ArgumentParser(formatter_class=ArgumentDefaultsRichHelpFormatter)
argh.add_commands(parser, [foo, bar, baz])

Running the above code means that my-app --help prints colorfully-formatted help text - but running my-app foo --help does not. Inspecting the parser._actions[1].choices shows me that the parsers that have been added by argh are using the CustomFormatter defined within Argh rather than ArgumentDefaultsRichHelpFormatter.

My instinct as a user is that if I've defined a formatter_class on the top level parser object, I'd like for argh, as it performs its magic, to reuse that formatter class on all calls to add_parser.

I think this is probably where the 'bug' comes in, and it seems like a very easy fix; just call subparsers_action.add_parser(cmd_name, **{**{'formatter_class': parser.formatter_class}, **func_parser_kwargs}).

I don't know the codebase very well yet, so I may be missing something, but I'd love to see a change to support this 'naturally' (right now i have to go set this after the fact by iterating through the parser's private object attributes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions