RopleyIT.CmdArgs 1.0.2

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

// Install RopleyIT.CmdArgs as a Cake Tool
#tool nuget:?package=RopleyIT.CmdArgs&version=1.0.2

CmdArgs - Another Command Line Argument Parser

This simple library uses attributes on a property rich class to define the arguments that should be passed to the command. Arguments determine their data type from the type of the property in the class, with attributes defining what the command line format should be, and whether an argument is mandatory or optional.

Usage

Consider two classes set up to map command line arguments to properties of those classes:

[ArgSet]
public class MyArgs
{
    [Arg("-t")]
    [Arg("--text")]
    public string? TextArg { get; set; }
    
    [Required]
    [Arg("-n")]
    public double Number { get; set; }
    
    [Arg("-y")]
    public bool TrueIfArgPresent { get; set; }
}

[ArgSet]
public class MoreArgs
{
    [Arg("-f")]
    public float Float { get; set; }
}

The presence of multiple Arg attributes on a property allows either format to be used. A boolean property is set to true if the argument appears in the command line, false if not. The Required attribute will cause the argument parsing to throw an Argument exception if the -n flag is not present in the example above.

To populate instances of these classes from a command line, in the main method use the following code:

static void main(string[] args)
{
    MyArgs myArgs = new();
    MoreArgs moreArgs = new();
    Arguments.Parse(args, myArgs, moreArgs);

    ... myArgs and moreArgs will have been populated with values ...
}

Examples of command lines that might parse correctly for the above class:

$ mycmd.exe --text "Text to go into the property TextArg" -n 3.14159
$ mycmd.exe -t "Short text flag" -n 1 -y -f 1.62E-19

Command line help

The library will also automatically construct a multi-line usage string for use with command line help messages. There is an additional Description attribute that can be placed on the ArgSet class, and on each Arg property. This allows you to automatically build the help string that describes each group of options class by class, and within each class, describes each individual set of arguments.

The classes below establish the documentation for each argument and argument set:

    [ArgSet]
    [Description("The set of args")]
    public class MyArgs
    {
        [Description("The string")]
        [Arg("-t")]
        public string TArg { get; set; } = string.Empty;

        [Description("The number")]
        [Arg("-i")]
        [Arg("--integer")]
        public int? MyNumber { get; set; }

        [Arg("-f"), Required]
        public double MyDouble { get; set; }

        [Arg("-s")]
        public string? TOptString { get; set; }
    }

    [ArgSet]
    [Description("These are the other args")]
    public class OtherArgs
    {
        [Description("The other string")]
        [Arg("-to")]
        public string TArgOther { get; set; } = string.Empty;

        [Description("The other integer")]
        [Arg("-io")]
        [Arg("--other_integer")]
        public int? MyNumberOther { get; set; }

        [Arg("-fo"), Required]
        public float MyFloat { get; set; }

        [Arg("-so")]
        public string? TOptStringOther { get; set; }
    }

To generate the usage string for both these classes, use the Arguments.Describe method as follows:

        MyArgs args = new();
        OtherArgs other = new();
        string description = Arguments.Describe(args, other);

A description for the above two classes might appear as follows:

The set of args
  -t "a string"  (optional)
    The string
  -i|--integer integer-value  (optional)
    The number
  -f float-value  (required)
    (no description)
  -s "a string"  (optional)
    (no description)
These are the other args
  -to "a string"  (optional)
    The other string
  -io|--other_integer integer-value  (optional)
    The other integer
  -fo float-value  (required)
    (no description)
  -so "a string"  (optional)
    (no description)
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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
1.0.2 460 3/8/2022
1.0.1 401 3/6/2022
1.0.0 391 3/5/2022