commandlineparser / commandlineparser/commandline

Support for custom dynamic help option for a single subcommand?

Open
#704 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.8k
Forks
478
PR merge metrics
No merged PRs in 30d

Description

For all my current subcommands I am using the default help option (which is very nice btw!). I have 1 subcommand that I want the default help options to be turned off for so that I can display custom help code inside of my subcommand's OnExecute method:

```
protected bool HandleHelp(){
//TO DO: Check to see if --help was passed in and display custom help text to the console based on a Dictionary of known commands on current machine.
}
protected override async Task OnExecute(CommandLineApplication app)
{
try
{
//Load a list of all the available commands
BuildListOfKnownCommands();

//Check for help option and display help message for specific tool
if(HandleHelp()){
return 0;
}

//TO DO: Run command
return 0;
}
catch (Exception ex)
{
OnException(ex);
return 1;
}
```

Is there an easy way to do this?

I have tried adding a member to my subcommand like so:

```
[HelpOption("--help")]
public bool HelpRequested {get;set;} = false;
```

but this gives me the error message **"Multiple HelpOptionAttributes found. HelpOptionAttribute should only be used one per type, either on one property or type."**

I tried using the attribute `[SuppressDefaultHelpOption]` to disable the default --help behavior but that didn't seem to take.

My subcommand has the following options:

```
[Command(Name = "tools", OptionsComparison = StringComparison.InvariantCultureIgnoreCase, Description = "Run helpful developer tools and scripts")]
[SuppressDefaultHelpOption]
```

Can the default --help behavior be turned off for just a single subcommand so that I can do my own custom dynamic --help function?

Thanks!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.