You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using click for an application with nested subcommands. I would like to change the appearance of click's help text; in particularly, I would like a different name for the Commands section heading. The original output of rpc -h is
Usage: rpc [OPTIONS] service function [arguments]
blah blah
Options:
-h Show this message and exit.
--version Show the version and exit.
Commands:
service1
service2
I am able to use subcommand_metavar to change the text in the usage line after [OPTIONS]. However, in my opinion, the Commands section heading is misleading because command does not appear in the usage line; Services is better, as shown below:
Usage: rpc [OPTIONS] service function [argument...]
blah blah
Options:
-h Show this message and exit.
--version Show the version and exit.
Services:
service1
service2
for help at the top-level and when I run rpc service1 -h, I would like to see Functions as the section heading:
Usage: rpc service1 [OPTIONS] function [argument...]
blah blah
Options:
-h Show this message and exit.
Functions:
function1
function2
I have the following workaround:
def Subcommand_context(section_name: str) -> click.Context:
"""
Return a Context subclass that uses a subclass of HelpFormatter
that writes section_name for the Commands section.
>>> main_group.context_class = Subcommand_context("Services")
>>> sub_command = click.Grroup("service1")
>>> sub_command.context_class = Subcommand_context("Functions")
>>> main_group.add_command(sub_command)
"""
class Formatter(click.formatting.HelpFormatter):
def write_heading(self, heading: str) -> None:
if heading == "Commands":
heading = section_name
super().write_heading(heading)
class Context_class(click.Context):
formatter_class = Formatter
return Context_class
This workaround is needed because it is not possible to set the formatter_class via the @group decorator nor the Group constructor. The format_commands method in the Multicommand class cannot be overridden in a subclass to change the section heading.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am using
clickfor an application with nested subcommands. I would like to change the appearance of click's help text; in particularly, I would like a different name for theCommandssection heading. The original output ofrpc -hisI am able to use
subcommand_metavarto change the text in the usage line after[OPTIONS]. However, in my opinion, theCommandssection heading is misleading becausecommanddoes not appear in the usage line;Servicesis better, as shown below:for help at the top-level and when I run
rpc service1 -h, I would like to seeFunctionsas the section heading:I have the following workaround:
This workaround is needed because it is not possible to set the
formatter_classvia the@groupdecorator nor theGroupconstructor. Theformat_commandsmethod in theMulticommandclass cannot be overridden in a subclass to change the section heading.Any suggestion for a better way?
Beta Was this translation helpful? Give feedback.
All reactions