CliArgumentParser 1.3.1.2545

dotnet add package CliArgumentParser --version 1.3.1.2545
                    
NuGet\Install-Package CliArgumentParser -Version 1.3.1.2545
                    
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="CliArgumentParser" Version="1.3.1.2545" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CliArgumentParser" Version="1.3.1.2545" />
                    
Directory.Packages.props
<PackageReference Include="CliArgumentParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CliArgumentParser --version 1.3.1.2545
                    
#r "nuget: CliArgumentParser, 1.3.1.2545"
                    
#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.
#:package CliArgumentParser@1.3.1.2545
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CliArgumentParser&version=1.3.1.2545
                    
Install as a Cake Addin
#tool nuget:?package=CliArgumentParser&version=1.3.1.2545
                    
Install as a Cake Tool

CLIArgumentsParser

image info

Library to easily manage and parse CLI arguments

To install it, use proper command:

dotnet add package CliArgumentParser

For more details about download, see NuGet Web Site

Build Status Security Rating Lines of Code Bugs Vulnerabilities Maintainability Rating Reliability Rating Technical Debt Duplicated Lines (%)

Nuget NuGet Downloads

<b>Other Builds</b>

  • Branch 1.2: Build Status

<b>Main Features</b>


  • define a model of usage for your CLI applications
  • Automatic parse and validation of your arguments against the defined model
  • Add examples of usage
  • Print automatic helper on CLI usage

How to define an Usage Model

To define you model, create a class inheriting from CliCommand one.

 class ScanCommand : CliCommand
 {
    public ScanCommand() : base("scan", "Scan the target folder tree")
    {}
 }

Remember to:

  • define the method SetDefaultValues to fill proper option dictionary
  • define the properties of command, with decorators (see the paragraph)
  • define examples

Option Definition

All properties you add should map an option of the verb, such:

    [Option(OPT_FOLDER, "Root folder to scan", isMandatory: true)]
    public string Folder
    {
            get { return this.GetArgumentValue<ScanCommand, string>(x => x.Folder); }
            set { this.AddOrUpdateArgument<ScanCommand, string>(x => x.Folder, value); }
    }

Then, remember to parse the string argument:

public override void ParseArgument(string[] tokens)
        {
            // take the expecetd value
            if (tokens.Length == 1)
            {
                throw new NotImplementedException();
            }
            else
            {
                // validate option
                switch (tokens[0])
                {
                    case OPT_FOLDER:
                    case OPT_FILENAME_SEARCH_PATTERN:
                    case OPT_FILECONTENT_SEARCH_PATTERN:
                        this.UpdateArgumentValue(tokens[0], tokens[1]);
                        break;

                    case OPT_TO:
                        this.PersistedTo = tokens[1].Trim().ToUpperInvariant();
                        break;

                    default:
                        throw new WrongOptionUsageException(this.Verb, tokens[0]);
                }
            }
        }

Option with Domain Values

to set up an option with predefined values, you have to decorate it as follow:

        [Option(OPT_DOMAIN, "Domain", isMandatory: true, domain: new string[]{"VAL1", "VAL2", "VAL3"})]
        public string Domain
        {
            get { return this.GetArgumentValue<TestWithLookupValue, string>(x => x.Domain); }
            set { this.AddOrUpdateArgument<TestWithLookupValue, string>(x => x.Domain, value); }
        }

Boolean Option

to use boolean options, use the following approach.

<b>Define the option as boolean</b>

        [Option(OPT_VERBOSE, "Verbosity Level", isMandatory: true)]
        public bool IsVerbose
        {
            get { return this.GetBooleanArgumentValue<TestWithFlagCommand, bool>(x => x.IsVerbose); }
            set { this.AddOrUpdateArgument<TestWithFlagCommand, bool>(x => x.IsVerbose, value); }
        }

<b>Set defaults</b>

        public override void SetDefaultValues()
        {
            base.SetDefaultValues();

            this.IsVerbose = false;
        }

<b>Parse the arguments</b>

        public override void ParseArgument(string[] tokens)
        {
            // validate option
            switch (tokens[0])
            {
                case OPT_VERBOSE:
                    this.IsVerbose = true;
                    break;

                default:
                    throw new WrongOptionUsageException(this.Verb, tokens[0]);
            }
        }

Notice that you must to process also specific tokens made by unique command; for more information see tests.

How to define examples

Each command shold contain a list of example, to understand how to use it. So far, you have to complete the abstract method:

        public override List<CliCommandExample> Examples()
        {
            return new List<CliCommandExample>()
            {
                new CliCommandExample("Scan the target folder to search *.csproj Files, containing the text \"NugetPackages\" and save a CSV files with output",
                                        ScanCommand.AsExampleFor(@"C:\Temp\MyFolder", ".csproj", @"\NugetPackages\", "CSV"))
            };
        }

Usage

the usage is very simple:

// Set up the factory of command
var factory = new CommandFactory().RegisterCommand<ScanCommand>("scan");

// parse the argument and run the callback if properly set
int exitResult = factory.InstanceFromFactory()
                                .UsingDefaultErrorManagement()
                                .ParseTheseArguments(args)
                                .CaseWhen<ScanCommand>(x => ExploreTheTree(x))
                                .Return();
                                
// return the proper exit value               
Environment.Exit(exitResult);
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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.3.1.2545 346 4/13/2023
1.2.2182 502 12/2/2022
1.2.2171 438 12/1/2022
1.2.2088 598 11/19/2022 1.2.2088 is deprecated.
1.2.1973 597 11/2/2022 1.2.1973 is deprecated.
1.2.1972 599 11/2/2022 1.2.1972 is deprecated.
1.1.1944 605 11/2/2022 1.1.1944 is deprecated.