An open API service indexing awesome lists of open source software.

https://github.com/netlah/commandlineutils

Command line parsing for .NET
https://github.com/netlah/commandlineutils

Last synced: 5 months ago
JSON representation

Command line parsing for .NET

Awesome Lists containing this project

README

          

# NetLah.Extensions.CommandLineUtils - .NET Library

[NetLah.Extensions.CommandLineUtils](https://www.nuget.org/packages/NetLah.Extensions.CommandLineUtils/) Command line parsing for .NET.

## Nuget package

[![NuGet](https://img.shields.io/nuget/v/NetLah.Extensions.CommandLineUtils.svg?style=flat-square&label=nuget&colorB=00b200)](https://www.nuget.org/packages/NetLah.Extensions.CommandLineUtils/)

## Build Status

[![.NET](https://github.com/NetLah/CommandLineUtils/actions/workflows/dotnet.yml/badge.svg)](https://github.com/NetLah/CommandLineUtils/actions/workflows/dotnet.yml)

## Getting started

### 1. Add/Update PackageReference to web .csproj

```xml

```

### 2. Sample Command Line Application

```cs
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 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`

```txt
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`

```txt
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`

```txt
Hello John Doe!
```

### 4. Check version

Command line `SampleCommandLine.exe --version`

```txt
SampleCommandLine: greeting a fullname
v1.2.3+456abcd
```

## Project origin and status

This repos a fork of [Microsoft.Extensions.CommandLineUtils](https://github.com/dotnet/extensions). [Microsoft announces](https://github.com/dotnet/extensions/issues/257) discontinue support the library. You may check this [repos](https://github.com/natemcmaster/CommandLineUtils) if prefer the more enrich features.