SimpleCli 0.0.4

dotnet add package SimpleCli --version 0.0.4                
NuGet\Install-Package SimpleCli -Version 0.0.4                
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="SimpleCli" Version="0.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleCli --version 0.0.4                
#r "nuget: SimpleCli, 0.0.4"                
#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 SimpleCli as a Cake Addin
#addin nuget:?package=SimpleCli&version=0.0.4

// Install SimpleCli as a Cake Tool
#tool nuget:?package=SimpleCli&version=0.0.4                

SimpleCli

SimpleCli is a simple command line argument parser which supports:

  • Positional arguments
  • Optional arguments
  • Post argument operands
  • Automatic help and usage text
  • Boolean, single, and multiple argument types
  • Default values
  • Custom validation delegates
  • Custom type conversion delegates
  • Conflicting arguments
  • Overriding arguments

Example

using System;
using SimpleCli;

namespace Test
{
    class Program
    {
        static int Main(string[] args)
        {
            try
            {
                var parser = new Parser(args, name: "mycli", version: "1.0.0");

                parser.Add("positional",
                           "This is a positional argument.");
                parser.Add("optional",
                           'o',
                           "This is an optional argument.",
                           "0",
                           Arg.Type.SINGLE);
                parser.Add("flag",
                           'f',
                           "This is an optional flag argument.",
                           "false",
                           Arg.Type.BOOLEAN);
                parser.Add("list",
                           'l',
                           "This is an optional list argument.",
                           "",
                           Arg.Type.MULTIPLE);
                parser.Add("conflicts",
                           'c',
                           "This is a conflicting argument.",
                           "42",
                           Arg.Type.SINGLE,
                           Validator.AcceptAll,
                           new string[]{"optional"});
                parser.Add("overrides",
                           'e',
                           "This is an overriding argument.",
                           "1234",
                           Arg.Type.SINGLE,
                           Validator.AcceptAll,
                           new string[]{},
                           new string[]{"optional"});
                parser.Add("rstring",
                           'r',
                           "This is a string validated by regex.",
                           "abc",
                           Arg.Type.SINGLE,
                           Validator.ValidateString("^[a-z]+$"));
                parser.Add("port",
                           'p',
                           "This is an integer validated by range.",
                           "80",
                           Arg.Type.SINGLE,
                           Validator.ValidateInt(0, 65535));

                parser.Parse();

                var positional = parser["positional"].GetString();
                var optional = parser["optional"].GetInt();
                var flag = parser["flag"].GetBool();
                var list = parser["list"].GetIntArray();
                var conflicts = parser["conflicts"].GetInt();
                var overrides = parser["overrides"].GetInt();
                var rstring = parser["rstring"].GetString();
                var port = parser["port"].GetInt();

                Console.WriteLine($"positional = {positional}");
                Console.WriteLine($"optional   = {optional}");
                Console.WriteLine($"flag       = {flag}");
                Console.WriteLine($"conflicts  = {conflicts}");
                Console.WriteLine($"overrides  = {overrides}");
                Console.WriteLine($"rstring    = {rstring}");
                Console.WriteLine($"port       = {port}");
                Console.WriteLine($"list       = {Util.ArrayToString<int>(list)}");
                Console.WriteLine($"operands   = {Util.ArrayToString<string>(parser.operands)}");

                return 0;
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                return 1;
            }
        }
    }
}

Usage help

> dotnet run
Usage: mycli [-c CONFLICTS|-o OPTIONAL] [-h] [-v] [-f] [-l 'ITEM, ...'] [-e OVERRIDES]
             [-r RSTRING] [-p PORT] POSITIONAL

Full help

> dotnet run -- --help
Usage: mycli [-c CONFLICTS|-o OPTIONAL] [-h] [-v] [-f] [-l 'ITEM, ...'] [-e OVERRIDES]
             [-r RSTRING] [-p PORT] POSITIONAL
Positional arguments:
  POSITIONAL
        This is a positional argument.
Optional arguments:
  -h, --help
        Displays this help message and exits.
  -v, --version
        Displays the version.
  -o OPTIONAL, --optional OPTIONAL
        This is an optional argument.
        Default: 0
  -f, --flag
        This is an optional flag argument.
  -l 'ITEM, ...', --list 'ITEM, ...'
        This is an optional list argument.
  -c CONFLICTS, --conflicts CONFLICTS
        This is a conflicting argument.
        Conflicts: optional
        Default: 42
  -e OVERRIDES, --overrides OVERRIDES
        This is an overriding argument.
        Overrides: optional
        Default: 1234
  -r RSTRING, --rstring RSTRING
        This is a string validated by regex.
        Default: abc
  -p PORT, --port PORT
        This is an integer validated by range.
        Default: 80

Correct parsing

> dotnet run -- positional -l 1,2,3,4 -o 4 -p80 -f
positional = positional
optional   = 4
flag       = True
conflicts  = 42
overrides  = 1234
rstring    = abc
port       = 80
list       = {1, 2, 3, 4}
operands   = {}

Overriding parsing

> dotnet run -- positional -e 1234 -o 4
positional = positional
optional   = 1234
flag       = False
conflicts  = 42
overrides  = 1234
rstring    = abc
port       = 80
list       = {}
operands   = {}

Post argument operands

> dotnet run -- positional -e 1234 -o 4 -p80 -- some operands
positional = positional
optional   = 1234
flag       = False
conflicts  = 42
overrides  = 1234
rstring    = abc
port       = 80
list       = {}
operands   = {some, operands}

Conflicting parsing error

> dotnet run -- positional -c 5 -o 4
Argument 'conflicts' conflicts with argument 'optional'

Validation error

> dotnet run -- positional -p1234567
Validation failed for 'port' = 1234567

Type conversion error

> dotnet run -- positional -o "string"
Failed to convert 'optional' = Int32("string")
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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SimpleCli:

Package Downloads
NetDetector

A daemon which checks for and responds to a given MAC or IP address on the network.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.4 1,388 5/20/2018
0.0.3 868 5/19/2018
0.0.2 876 5/19/2018