ArgsParser 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ArgsParser --version 3.0.0
NuGet\Install-Package ArgsParser -Version 3.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#r "nuget: ArgsParser, 3.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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

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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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 Downloads Last updated
5.0.1 76 2/23/2024
5.0.0 158 8/27/2023
4.0.6 114 8/24/2023
4.0.5 102 8/22/2023
4.0.4 92 8/21/2023
4.0.3 89 8/21/2023
4.0.1 96 8/21/2023
4.0.0 103 8/21/2023
3.0.0 793 11/7/2020