ArgsParser 3.0.0

Easy argument parsing for .Net Core applications.

Install-Package ArgsParser -Version 3.0.0
dotnet add package ArgsParser --version 3.0.0
<PackageReference Include="ArgsParser" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ArgsParser --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ArgsParser, 3.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ArgsParser as a Cake Addin
#addin nuget:?package=ArgsParser&version=3.0.0

// Install ArgsParser as a Cake Tool
#tool nuget:?package=ArgsParser&version=3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ArgsParser

Easy argument parsing for .Net Core applications.

Example usage

using ArgsParser;

// other code

var parser = new Parser(args)
    .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")
    .SupportsFlag("serve", "Start the site going in a dev server")
    .SupportsFlag("force", "Overwrite any destination content")
    .Help();

parser.Parse();

var hasError = parser.HasErrors;
var serve = parser.IsFlagProvided("serve");
var port = parser.GetOption<int>("port");
var read = parser.GetOption<string>("read");

Example autogenerated help info (.Help())

  • 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

Example input

mycommand -run data "Site Title" --serve -ignore -port 3000

Example errors from the above

Errors come in two collections. The property HasErrors will be true if either collection has entries.

  • ExpectationErrors are where expectations are not met by the arguments provided (eg missing required option)
  • ArgumentErrors are where something was provided but there were issues with it (eg a value without an option)

For example, with the configuration above and the example input shown the combined errors will be as follows.

write => Option missing: write
0 => Unknown option: run
2 = Unexpected value: Site Title
4 => Unknown flag: ignore
  • The first is an expectation error and is keyed by the name of the option
  • The remaining three are argument errors keyed by their 0-based offset into the args provided
  • The "missing" read option has no error; it was defined above with a default value

Supported

  • Display help showing supported flags/options
  • Required named option/values
  • Optional named option/values
  • Optional named flags
  • Default option values
  • Option types support any IConvertable, including int, bool, DateTime
  • Accepts either - or -- prefixes
  • Provides two collections of error messages
    • Expectation errors
      • Missing required options
    • Argument errors
      • Option values of incorrect type
      • Unexpected values (not with an option)
      • Unnamed flags or options
      • Unknown flags or options

The example in detail

The assertions, which are provided for clarity, are from NUnit.

var args = new string[] { "-run", "data", "Site Title", "--serve", "-ignore", "-port", "3000" };

var parser = new Parser(args)
    .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")
    .SupportsFlag("serve", "Start the site going in a dev server")
    .SupportsFlag("force", "Overwrite any destination content")
    .Help();

var result = parser.Parse();

Assert.AreEqual(4, result.ExpectationErrors.Count + result.ArgumentErrors.Count);
Assert.Contains("Option missing: write", result.ExpectationErrors.Values.ToList());
Assert.Contains("Unknown option: run", result.ArgumentErrors.Values.ToList());
Assert.Contains("Unexpected value: Site Title", result.ArgumentErrors.Values.ToList());
Assert.Contains("Unknown flag: ignore", result.ArgumentErrors.Values.ToList());

Assert.IsTrue(result.IsOptionProvided("port"));
Assert.AreEqual(3000, result.GetOption<int>("port"));

Assert.IsTrue(result.IsOptionProvided("read"));
Assert.AreEqual("site", result.GetOption<string>("read"));

Assert.IsFalse(result.IsOptionProvided("write"));
Assert.AreEqual(null, result.GetOption<string>("write"));

Assert.IsTrue(result.IsFlagProvided("serve"));
Assert.IsFalse(result.IsFlagProvided("force"));

Copyright K Cartlidge 2020.
Licensed as GNU AGPLv3.

ArgsParser

Easy argument parsing for .Net Core applications.

Example usage

using ArgsParser;

// other code

var parser = new Parser(args)
    .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")
    .SupportsFlag("serve", "Start the site going in a dev server")
    .SupportsFlag("force", "Overwrite any destination content")
    .Help();

parser.Parse();

var hasError = parser.HasErrors;
var serve = parser.IsFlagProvided("serve");
var port = parser.GetOption<int>("port");
var read = parser.GetOption<string>("read");

Example autogenerated help info (.Help())

  • 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

Example input

mycommand -run data "Site Title" --serve -ignore -port 3000

Example errors from the above

Errors come in two collections. The property HasErrors will be true if either collection has entries.

  • ExpectationErrors are where expectations are not met by the arguments provided (eg missing required option)
  • ArgumentErrors are where something was provided but there were issues with it (eg a value without an option)

For example, with the configuration above and the example input shown the combined errors will be as follows.

write => Option missing: write
0 => Unknown option: run
2 = Unexpected value: Site Title
4 => Unknown flag: ignore
  • The first is an expectation error and is keyed by the name of the option
  • The remaining three are argument errors keyed by their 0-based offset into the args provided
  • The "missing" read option has no error; it was defined above with a default value

Supported

  • Display help showing supported flags/options
  • Required named option/values
  • Optional named option/values
  • Optional named flags
  • Default option values
  • Option types support any IConvertable, including int, bool, DateTime
  • Accepts either - or -- prefixes
  • Provides two collections of error messages
    • Expectation errors
      • Missing required options
    • Argument errors
      • Option values of incorrect type
      • Unexpected values (not with an option)
      • Unnamed flags or options
      • Unknown flags or options

The example in detail

The assertions, which are provided for clarity, are from NUnit.

var args = new string[] { "-run", "data", "Site Title", "--serve", "-ignore", "-port", "3000" };

var parser = new Parser(args)
    .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")
    .SupportsFlag("serve", "Start the site going in a dev server")
    .SupportsFlag("force", "Overwrite any destination content")
    .Help();

var result = parser.Parse();

Assert.AreEqual(4, result.ExpectationErrors.Count + result.ArgumentErrors.Count);
Assert.Contains("Option missing: write", result.ExpectationErrors.Values.ToList());
Assert.Contains("Unknown option: run", result.ArgumentErrors.Values.ToList());
Assert.Contains("Unexpected value: Site Title", result.ArgumentErrors.Values.ToList());
Assert.Contains("Unknown flag: ignore", result.ArgumentErrors.Values.ToList());

Assert.IsTrue(result.IsOptionProvided("port"));
Assert.AreEqual(3000, result.GetOption<int>("port"));

Assert.IsTrue(result.IsOptionProvided("read"));
Assert.AreEqual("site", result.GetOption<string>("read"));

Assert.IsFalse(result.IsOptionProvided("write"));
Assert.AreEqual(null, result.GetOption<string>("write"));

Assert.IsTrue(result.IsFlagProvided("serve"));
Assert.IsFalse(result.IsFlagProvided("force"));

Copyright K Cartlidge 2020.
Licensed as GNU AGPLv3.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.0.0 227 11/7/2020