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
- Host: GitHub
- URL: https://github.com/netlah/commandlineutils
- Owner: NetLah
- License: mit
- Created: 2022-10-07T09:03:28.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-10T02:29:08.000Z (over 2 years ago)
- Last Synced: 2024-12-13T03:14:50.695Z (over 1 year ago)
- Language: C#
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
[](https://www.nuget.org/packages/NetLah.Extensions.CommandLineUtils/)
## Build Status
[](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.