CmdLineArgsParser 0.4.1

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

// Install CmdLineArgsParser as a Cake Tool
#tool nuget:?package=CmdLineArgsParser&version=0.4.1                

CmdLineArgsParser

CodeFactor NuGet

CmdLineArgsParser helps you parse command line arguments.

Define a class containing the options you want to parse and then call the Parse method.

When defining the options you can specify:

  • Name (required): the long name of the option (e.g. --option)
  • ShortName: the short name of the option (e.g. -o)
  • Verb: boolean indicating it the option is a verb. You can only have one option set as verb and must be passed as the first argument.
  • OnlyForVerbs: a list of verbs this option is valid for separated by a semicolon
  • Required: boolean indicating if the option is required
  • ValidValues: a list of valid values separated by a semicolon (e.g. "value1;value2")
  • Description: the description of the option
  • Section: the section this option belongs to. This is only used by the ShowUsage method. If no section name is set the option belongs to the 'General' section.

Supported option type:

  • bool: set to true if the argument is passed (e.g. --option or -o). Multiple boolean options can be set using the short name (e.g. -op)
  • enum: set to the value after the argument (e.g. --enumOpt value)
  • string: set to the value after the argument (e.g. --option "string value")
  • int: set to the value after the argument (e.g. --intOpt 5)
  • double/float: set to the value after the argument (e.g. --doubleOpt 5.2)
  • DateTime: set to the value after the argument (e.g. --dateTimeOpt "12/01/2022 3:54:23 PM")
  • Array of one of the supported built-in types: one value added for each argument value (e.g. --input /home/user --input /home/user2)
  • List of one of the supported built-in types: one value added for each argument value

Parse

The Parse method parses the given arguments and returns an instance of the options class. It also checks for errors in the passed arguments (missing required arguments, wrong argument names...) and returns them in errors

ShowUsage

The ShowUsage method prints to the console the options and the help text

Example output:

Usage:
Demo.dll action --input VALUE --output VALUE [OPTIONS]

Action:
  Backup                      Description of backup verb
  Copy                        Description of copy verb
  Delete                      Description of delete verb

General:
  -i, --input=VALUE           The input file full path
  -o, --output=VALUE          The output file full path
  -w, --overwrite             Overwrite output without warning
  -r, --retry=VALUE           The number of time the command is retried in case of error
  -y, --yes                   Assume yes as a reply to all the questions

Example

Options class:

public class Options : IOptions
{
    public enum Verbs
    {
        [Description("Description of backup verb")]
        Backup,
        [Description("Description of copy verb")]
        Copy,
        [Description("Description of delete verb")]
        Delete,
    }

    [Option("action",
        Description = "The command to execute on the file",
        Verb = true, Required = true)]
    public Verbs Command { get; set; }

    [Option("input", ShortName = 'i',
        Description = "The input file full path",
        Required = true)]
    public string? InputFile { get; set; }

    [Option("output", ShortName = 'o',
        Description = "The output file full path",
        Required = true)]
    public string? OutputFile { get; set; }

    [Option("retry", ShortName = 'r',
        Description = "The number of time the command is retried in case of error")]
    public int? Retry { get; set; }

    [Option("yes", ShortName = 'y',
        Description = "Assume yes as a reply to all the questions")]
    public bool AlwaysYes { get; set; }

    [Option("overwrite", ShortName = 'w',
        OnlyForVerbs = "Copy;Backup",
        Description = "Overwrite output without warning")]
    public bool OverwriteOutput { get; set; }
}

Usage example:

string[] testArgs =
{
    "backup",
    "--input", "/home/user/file",
    "-yw",
    "-r", "5"
};

var options = new CmdLineArgsParser.Parser().Parse<Options>(testArgs, out var errors);
if (errors.Count > 0) {
    Console.WriteLine("Errors:");
    foreach (var error in errors) {
        Console.WriteLine(error.Message);
    }
}

The output of the above code is

Errors:
Required option 'output' not set

The returned options object

{
  "Command": 0,
  "InputFile": "/home/user/file",
  "OutputFile": null,
  "Retry": 5,
  "AlwaysYes": true,
  "OverwriteOutput": true
}
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
0.7.4 449 8/27/2022
0.7.3 399 8/24/2022
0.7.2 391 8/21/2022
0.7.1 380 8/17/2022
0.7.0 401 8/15/2022
0.6.0 410 8/11/2022
0.5.1 419 8/6/2022
0.5.0 399 8/6/2022
0.4.1 408 8/2/2022
0.4.0 408 8/1/2022
0.3.0 408 7/31/2022
0.2.0 394 7/30/2022
0.1.0 408 7/30/2022