cli icon indicating copy to clipboard operation
cli copied to clipboard

Custom SubcommandHelpTemplate

Open kristofferahl opened this issue 3 years ago • 3 comments

Is there a way to set a custom help template for subcommands?

I know it's totally possible to set custom templates for the app (CustomAppHelpTemplate) and for commands (CustomHelpTemplate) but I can't find how to override the SubcommandHelpTemplate. What am I missing?

kristofferahl avatar Sep 10 '21 18:09 kristofferahl

Jesus Christ, I just spent an hour in the VS Code Debugger trying to figure out why my CustomHelpTemplate Template is not being printed correctly. The issue is here

https://github.com/urfave/cli/blob/master/app.go#L391

a is a new *cli.App that contains all configs for the command being executed, but it is not passed to checkSubcommandHelp, so when this if logic: https://github.com/urfave/cli/blob/12b7dfd08cb0cf2853ce3aa0767046e1c4edaf23/help.go#L330

returns true indicating that the help text should be returned, and ShowSubcommandHelp is called, c *cli.Context is empty. Well not empty, not nil, but all of the properties are initialized with their default values.

https://github.com/urfave/cli/blob/12b7dfd08cb0cf2853ce3aa0767046e1c4edaf23/help.go#L224

So it doesn't matter what happens inside of ShowSubcommandHelp, when the subsequent ShowCommandHelp is called https://github.com/urfave/cli/blob/12b7dfd08cb0cf2853ce3aa0767046e1c4edaf23/help.go#L189 command == "" will always return true, and this is where our CustomHelpTemplate value that is set on the *cli.Command struct is ignored.

I am still digging to see if I can fix this, i've never put a PR into an Open Source project fixing a bug like this before. It's always been to fix typo's, etc.....

Code_BGgJAZjpgS Code_TmPlCE2BCz

ddouglas avatar Nov 06 '21 19:11 ddouglas

Yeah I have no clue what to do here, well "properly". I can see the data in the *cli.App that is in the *cli.Context here:

Code_n22ZIOEnJB

The issue is that the Name that is being passed to ShowCommandHelp is bare because when RunAsSubcommand is called, that calls NewContext, this func initializes the Command as a &cli.Command instead of the command that is housing the Subcommands.

https://github.com/urfave/cli/blob/12b7dfd08cb0cf2853ce3aa0767046e1c4edaf23/context.go#L33

ddouglas avatar Nov 06 '21 19:11 ddouglas

Okay, I've put a PR in for this. I do not consider this an ideal fix, but it works.

ddouglas avatar Nov 06 '21 19:11 ddouglas

@ddouglas Can you check if latest release solves this issue for you ?

dearchap avatar Oct 27 '22 12:10 dearchap