Tyrrrz / CliFx

Class-first framework for building command-line interfaces
MIT License
1.5k stars 61 forks source link

Command examples #115

Open Tyrrrz opened 2 years ago

Tyrrrz commented 2 years ago

Discussed in https://github.com/Tyrrrz/CliFx/discussions/114

Originally posted by **wayneyaoo** December 4, 2021 Can we have concept of "Example" for each command that can be specified by the CLI author and a new section is rendered as "EXAMPLE". I was imaging something like ```C# [Command("verb")] public class MyCommand : ICommand { // options and parameters go here as property [Example] public (static) Example[] Examples = new [] { new Example("verb --option value"), new Example("verb --anotherPositionalOption") }; } ``` and the examples could be rendered to another section like "EXAMPLE" when `./myapp verb -h` is called. I'm happy to contribute if this looks good. Or we can discuss.
Tyrrrz commented 2 years ago

My reply: https://github.com/Tyrrrz/CliFx/discussions/114#discussioncomment-1752827

Tyrrrz commented 2 years ago

Note: this is better implemented as another string property on the CommandAttribute, not as a type member as visualized in the original post.

Tyrrrz commented 2 years ago

Example usage:

[Command("foo", Examples = new[]
    {
        // Note: no need to add the command name here
        "--bar 42",
        "-b 42"
    }
]
public class MyCommand : ICommand
{
    // ...
}

The examples would then be shown in the "usage" section of the help text:

USAGE
  dotnet myapp.dll foo [options]
  dotnet myapp.dll foo --bar 42
  dotnet myapp.dll foo -b 42

OPTIONS
  -b|--bar          Something.
  -h|--help         Shows help text.