https://github.com/bbepis/nargs
Simple single-file argument parser for C#
https://github.com/bbepis/nargs
Last synced: about 1 year ago
JSON representation
Simple single-file argument parser for C#
- Host: GitHub
- URL: https://github.com/bbepis/nargs
- Owner: bbepis
- License: mit
- Created: 2021-07-31T08:00:30.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-31T12:50:40.000Z (almost 5 years ago)
- Last Synced: 2025-02-13T20:52:55.283Z (over 1 year ago)
- Language: C#
- Size: 20.5 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NArgs
Simple single-file argument parser for C#
Just copy NArgs.cs into your project and you're ready to go.
### Usage
```cs
using NArgs;
class MyArguments : IArgumentCollection
{
public IList Values { get; set; }
[CommandDefinition("r", "required", Description = "This is required", Required = true)]
public string RequiredArg { get; set; }
[CommandDefinition("collection", Description = "A collection of strings", Required = true)]
public IList StringCollection { get; set; }
[CommandDefinition("h", "help", Description = "Prints help text")]
public bool Help { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyArguments arguments = Arguments.Parse(args);
if (arguments.Values.Count == 0 || arguments.Help)
{
Console.WriteLine(Arguments.PrintLongHelp(
"MyApp v1.0.0, by me",
"Usage: MyApp [options] "));
return;
}
...
}
}
```
`PrintLongHelp` in the example writes this to console:
```
MyApp v1.0.0, by me
Usage: MyApp [options]
-h, --help Prints help text
-r , --required This is required
--collection A collection of strings
```
An argument class must inherit from `IArgumentCollection`. There are four property types supported:
- `bool`: Is `true` if the argument was provided, `false` if not. `Required` is not applicable to this argument type.
- `string`: Is set to the value of `--stringArg `, otherwise `null` if not provided.
- `IList`: Similar to the above, however collects multiple values into a single list. Does *not* split the value on comma or anything else, instead appends multiple usages (i.e. `--listArg value1 --listArg value2`) into a single list. List is empty if not provided.
- Any enum. Will parse a case-insensitive string value into an enum. Will throw if unable to parse the enum correctly.