Skip to content

Instantly share code, notes, and snippets.

@mmckechney
Created March 29, 2022 14:14
Show Gist options
  • Save mmckechney/7bd92a4417458fb8b919b44bac5c5b89 to your computer and use it in GitHub Desktop.
Save mmckechney/7bd92a4417458fb8b919b44bac5c5b89 to your computer and use it in GitHub Desktop.
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Help;
using System.CommandLine.NamingConventionBinder;
namespace SysCmdLine
{
class Program
{
public static void Main(string[] args)
{
RootCommand rootCommand = new RootCommand(description: "Sample code to show Issue #1691");
var testCommand = new Command("test", "Simple test command")
{
new Option<string>(new string[]{"--opt1", "--o1"}, "Option 1"),
new Option<string>(new string[]{"--opt2", "--o2"}, "Option 1"),
};
testCommand.Handler = CommandHandler.Create<string, string>(Worker.RunTest);
rootCommand.Add(testCommand);
var parser = new CommandLineBuilder(rootCommand)
.UseDefaults()
.UseHelp(ctx =>
{
ctx.HelpBuilder.CustomizeLayout(_ => System.CommandLine.Help.HelpBuilder.Default
.GetLayout()
.Prepend(
_ => _.Output.WriteLine("**Adding extra help here**")
));
}).Build();
rootCommand.InvokeAsync(args).Wait();
}
public class Worker
{
public static void RunTest(string opt1, string opt2)
{
Console.WriteLine(opt1);
Console.WriteLine(opt2);
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta3.22114.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta3.22114.1" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment