Easy argument parsing for .Net Core applications.
Available as a nuget package.
var parser = new ArgsParser.ArgsParser(args[0])
.SupportsOption<int>("port", "Port to start the dev server on", 1337)
.RequiresOption<string>("read", "Folder to read the site from", "site")
.RequiresOption<string>("write", "Folder to write the result to")
.HasFlag("serve", "Start the site going in a dev server")
.HasFlag("force", "Overwrite any destination content")
.Help();
parser.Parse();
Console.Log($"Errors: {parser.Errors.Count}");
Console.Log($" Port: {parser.GetOption<int>("port")}"); // method 1 returns as the type
Console.Log($" Port: {parser.ParsedOptions["port"]}") // method 2 returns as an object
Required options come first, then optional options, then flags. Each of those three blocks is further sorted alphabetically.
-read <value> Folder to read the site from (required)
-write <value> Folder to write the result to (required)
-port <value> Port to start the dev server on
-force Overwrite any destination content
-serve Start the site going in a dev server
mycommand -run data "Site Title" --serve -ignore -port 3000
Errors are keyed by either the name of the option/flag if there is an issue with that specific one, or by "N/A" for things like unexpected (and therefore orphaned) values.
if (parser.HasErrors)
foreach (var key in parser.Errors.Keys)
parser.Errors[key].ForEach(x => Console.WriteLine($"{key} => {x}"));
This gives:
ignore => Unknown flag: ignore
N/A => Unexpected value: Site Title
read => Option missing: read
run => Unknown option: run
write => Option missing: write
In addition:
parser.ParsedFlags
will contain two entries,"serve"
and"ignore"
parser.ParsedOptions["run"]
will contain"data"
(string)parser.ParsedOptions["port"]
will contain3000
(int)- As will
parser.GetOption<int>("port")
- As will
- Display help showing supported flags/options
- Required named option/values
- Optional named option/values
- Optional named flags
- Parsing generates simple collections
- A collection of all provided options (keys and their typed values)
- A collection of all provided flags (array of named)
- Provides a collection of error messages
- Missing required options
- Unknown flags or options
- Values not attached to options
- Option types support any
IConvertable
, includingint
,bool
,DateTime
- Accepts either
-
or--
prefixes
Copyright K Cartlidge 2020. Licensed as GNU AGPLv3.
See the CHANGELOG for current status.