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

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#

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.