Skip to content

NetLah/CommandLineUtils

Repository files navigation

NetLah.Extensions.CommandLineUtils - .NET Library

NetLah.Extensions.CommandLineUtils Command line parsing for .NET.

Nuget package

NuGet

Build Status

.NET

Getting started

1. Add/Update PackageReference to web .csproj

<ItemGroup>
  <PackageReference Include="NetLah.Extensions.CommandLineUtils" />
</ItemGroup>

2. Sample Command Line Application

CommandLineApplication commandLineApplication =
    new(throwOnUnexpectedArg: true)
    {
        Name = "SampleCommandLine.exe",
        FullName = "SampleCommandLine: greeting a fullname"
    };

CommandArgument? names = null;

commandLineApplication.Command("hello",
    (target) =>
    {
        target.FullName = "Greeting a fullname";
        target.Description = "The hello command";
        target.ShortVersionGetter = GetShortVersion;
        target.LongVersionGetter = GetLongVersion;

        names = target.Argument(
            "fullname",
            "Enter the full name of the person to be greeted.",
            multipleValues: true);

        target.HelpOption("-? | -h | --help");

        target.OnExecute(() =>
        {
            if (names?.Values.Any() == true)
            {
                Greet(names.Values);
            }
            else
            {
                // show help if the required argument is missing
                commandLineApplication.ShowHelp();
            }
            return 0;
        });
    });

commandLineApplication.HelpOption("-? | -h | --help");
commandLineApplication.VersionOption("--version", GetShortVersion, GetLongVersion);

commandLineApplication.OnExecute(() =>
{
    // show help if root command
    commandLineApplication.ShowHelp();
    return 0;
});

commandLineApplication.Execute(args);

static void Greet(IEnumerable<string> values) => Console.WriteLine($"Hello {string.Join(" ", values)}!");

static string GetShortVersion() => "1.2.3";

static string GetLongVersion() => "v1.2.3+456abcd";

Play around

1. Get help on root

To know the available commands and options SampleCommandLine.exe --help

SampleCommandLine: greeting a fullname 1.2.3

Usage: SampleCommandLine.exe [options] [command]

Options:
  -? | -h | --help  Show help information
  --version         Show version information

Commands:
  hello  The hello command

Use "SampleCommandLine.exe [command] --help" for more information about a command.

2. Get help on hello command

To know the available arguments and options of a command SampleCommandLine.exe hello --help

Greeting a fullname 1.2.3

Usage: SampleCommandLine.exe hello [arguments] [options]

Arguments:
  fullname  Enter the full name of the person to be greeted.

Options:
  -? | -h | --help  Show help information

3. Greeting a fullname

Command line SampleCommandLine.exe hello John Doe

Hello John Doe!

4. Check version

Command line SampleCommandLine.exe --version

SampleCommandLine: greeting a fullname
v1.2.3+456abcd

Project origin and status

This repos a fork of Microsoft.Extensions.CommandLineUtils. Microsoft announces discontinue support the library. You may check this repos if prefer the more enrich features.

About

Command line parsing for .NET

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages