CliArgumentParser 1.2.2088

Additional Details

print example feature containing a bug that prevent you to use it properly

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

// Install CliArgumentParser as a Cake Tool
#tool nuget:?package=CliArgumentParser&version=1.2.2088                

CLIArgumentsParser

Library to easily manage and parse CLI arguments

Build Status Quality Gate Status Lines of Code Bugs Vulnerabilities

Nuget NuGet Downloads

Other Builds:

  • Branch 1.2: Build Status

To install it, use proper command:

dotnet add package CliArgumentParser

For more details about download, see NuGet Web Site

Main Features

  • 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]);
                }
            }
        }

Bollean 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. 
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 203 4/13/2023
1.2.2182 364 12/2/2022
1.2.2171 310 12/1/2022
1.2.2088 389 11/19/2022 1.2.2088 is deprecated.
1.2.1973 385 11/2/2022 1.2.1973 is deprecated.
1.2.1972 391 11/2/2022 1.2.1972 is deprecated.
1.1.1944 396 11/2/2022 1.1.1944 is deprecated.

- Minor issues solved
- Test Coverage increased
- README file updated with usage details and samples
- LICENSE specification