CLArgumentParser 1.0.4

dotnet add package CLArgumentParser --version 1.0.4
                    
NuGet\Install-Package CLArgumentParser -Version 1.0.4
                    
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="CLArgumentParser" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CLArgumentParser" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="CLArgumentParser" />
                    
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 CLArgumentParser --version 1.0.4
                    
#r "nuget: CLArgumentParser, 1.0.4"
                    
#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 CLArgumentParser@1.0.4
                    
#: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=CLArgumentParser&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=CLArgumentParser&version=1.0.4
                    
Install as a Cake Tool

Command-line-argument-parser

This is a simple command line argument parser that pretend be configurated with just one string pattern.

The pattern describes the way arguments shuuld be interpteted

Especification:

The string pattern has four parts: CaseSensitive, ArgCharName, ArgCharValue, args.

And have this form: [CaseSensitive] [ArgCharName:c] [ArgCharValue:c] args:<argument list specification>

Just args is mandatory

CaseSensitive: optional. Indicates the parameters will be case sensitive, case insensitive if not especified.

ArgCharName: optional. It's the prefix for each named argument, by default hyphen - ; example ArgCharName:/

ArgCharValue: optional. It's the character thar identify the asignation, by fefault colon : example ArgCharValue:=

Args: it's the pattern to extract argument values, it is a list separated by spaces. Each element of the list has the form: [*]argname[:value]

  • Each argname can be prefixed with an asterisk wich means it is mandatory.
  • The fixed siffix text ":value" is optional and means that a value is expected after the argument name.
  • If the element doesn't have the suffix ":value" it will be a boolean parameter, it's like a switch.
  • Only the argnames suffixed with ":value" can be optional.

CaseSensitive, ArgCharName and ArgCharValue are optionals, and Args must be specified.

Full example1: argcharname:/ argcharvalue:= args:*input:value output:value F S

In this example the valid arguments are:

  • input: is mandatory and expects a value after the argument name
  • output: is optional and must have a value after the argument is especified
  • F: it's a switch
  • S: it's a switch

Full example2: argcharname:/ argcharvalue:= casesensitive args:*input:value output:value F S f

In this example the arguments are case sensitive the valid arguments are:

  • input: is mandatory and expects a value after the argument name
  • output: is optional and must have a value after the argument is especified
  • F: it's a switch
  • S: it's a switch
  • f: it's a switch

Using the class:

static void Main(string[] args)
      {
         // The arguments of this program should be:
         // First, they are case sensitive
         // Argument names must start with a slash /
         // There are two valued arguments: /input and /output
         // The value of each argument will be separated with an equal sign
         // There are two switches: /S and /F
         
         // Configure parameters in a single line:
         ArgumentParser argumentParser = new ArgumentParser("argcharname:/ argcharvalue:= CaseSensitive args:*input:value output:value F S");
         try
         {
            argumentParser.Parse(args);
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex.Message);
            Console.WriteLine("\nUSAGE:\n");
            
            // The class have a useful function that shows the list of parameters in console.
            
            argumentParser.ShowUsage(System.Reflection.Assembly.GetExecutingAssembly().Location);
            return;
         }

         Console.WriteLine("Executed as: {0}", Environment.CommandLine);
         
         // And using the functions GetArgumentValue and GetSwitch you can get the argument values

         Console.WriteLine("Argument /input value: {0}", argumentParser.GetArgumentValue("input"));
         Console.WriteLine("Argument /output value: {0}", argumentParser.GetArgumentValue("output"));
         Console.WriteLine("Argument /F value: {0}", argumentParser.GetSwitch("F"));
         Console.WriteLine("Argument /F value: {0}", argumentParser.GetSwitch("S"));

         Console.Write("End.");
         Console.ReadLine();
      }
   }

For instance, if you run:

program.exe  /input="archivo datos.in" /output=archivo.out /F /S

The result will be:

Argument /input value: archivo datos.in
Argument /output value: archivo.out
Argument /F value: True
Argument /F value: True

Version 1.0.2

  • Now you can add a description for each argument with the function void SetDescription(string argumentName, string description). This description will appear with the usage output.

Now usage can show descriptions for each argument, por example:

program.exe /input=samplevalue /output=samplevalue /F /S

The following arguments are mandatories: /input

Argument names are case sensitive

Arguments:
/input : input json file
/F : Force
/S : Save output to default file

Hope you enjoy it.

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.0.4 257 4/28/2023
1.0.3 460 9/21/2022
1.0.2 471 9/19/2022
1.0.1 464 9/17/2022
1.0.0 462 9/17/2022