SimpleCli 0.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package SimpleCli --version 0.0.2
NuGet\Install-Package SimpleCli -Version 0.0.2
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.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleCli --version 0.0.2
#r "nuget: SimpleCli, 0.0.2"
#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.2

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

SimpleCli

SimpleCli is a simple command line argument parser which supports:

  • positional and optional arguments
  • custom validation
  • default values
  • boolean, single, and multiple types
  • automatic help text
  • conflicting and overriding arguments
  • post argument operands

Example

using System;
using SimpleCli;

namespace Test
{
    class Program
    {
        public static bool ValidatePort(string arg)
        {
            try
            {
                var i = int.Parse(arg);
                if(i < 0 || i > 65535)
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        } 

        static int Main(string[] args)
        {
            try
            {
                var parser = new Parser("mycli", args);

                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("string-list", 
                           'a', 
                           "This is another 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", 
                           'v', 
                           "This is an overriding argument.", 
                           "1234", 
                           Arg.Type.SINGLE,
                           Validator.AcceptAll,
                           new string[]{},
                           new string[]{"optional"});
                parser.Add("port", 
                           'p', 
                           "This is a custom argument.", 
                           "80", 
                           Arg.Type.SINGLE,
                           ValidatePort);

                parser.Parse();

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

                Console.WriteLine($"positional = {positional}");
                Console.WriteLine($"optional = {optional}");
                Console.WriteLine($"flag = {flag}");

                Console.WriteLine("list:");
                foreach(var item in list)
                    Console.WriteLine($"    {item}");
                Console.WriteLine();

                Console.WriteLine("string-list:");
                foreach(var item in stringList)
                    Console.WriteLine($"    {item}");
                Console.WriteLine();

                Console.WriteLine($"conflicts = {conflicts}");
                Console.WriteLine($"overrides = {overrides}");
                Console.WriteLine($"port = {port}");

                Console.WriteLine("Operands:");
                foreach(var item in parser.operands)
                    Console.WriteLine($"    {item}");
                Console.WriteLine();

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

Usage help

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

Full help

> dotnet run -- --help
Usage: mycli [-h] [-c CONFLICTS|-o OPTIONAL] [-f] [-l 'ITEM, ...'] [-a 'ITEM, ...']
             [-v OVERRIDES] [-p PORT] POSITIONAL
Positional arguments:
  POSITIONAL
        This is a positional argument.
Optional arguments:
  -h, --help
        Displays this help message and exits.
  -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.
  -a 'ITEM, ...', --string-list 'ITEM, ...'
        This is another optional list argument.
  -c CONFLICTS, --conflicts CONFLICTS
        This is a conflicting argument.
        Conflicts: optional
        Default: 42
  -v OVERRIDES, --overrides OVERRIDES
        This is an overriding argument.
        Overrides: optional
        Default: 1234
  -p PORT, --port PORT
        This is a custom argument.
        Default: 80

Correct parsing

> dotnet run -- positional -l '1, 2, 3, 4' -a 'This, is, a, string\, list' -o 4 -p80 -f
positional = positional
optional = 4
flag = True
list:
    1
    2
    3
    4

string-list:
    This
    is
    a
    string, list

conflicts = 42
overrides = 1234
port = 80
Operands:

Conflicting parsing error

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

Overriding parsing

> dotnet run -- positional -v 1234 -o 4
positional = positional
optional = 1234
flag = False
list:

string-list:

conflicts = 42
overrides = 1234
port = 80
Operands:

Validation error

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

Type conversion error

> dotnet run -- positional -o 'A string'
Failed to convert "optional" = Int32(A string)

Post argument operands

> dotnet run -- positional -v 1234 -o 4 -p80 -- some operands
positional = positional
optional = 1234
flag = False
list:

string-list:

conflicts = 42
overrides = 1234
port = 80
Operands:
    some
    operands
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,301 5/20/2018
0.0.3 837 5/19/2018
0.0.2 852 5/19/2018