CommandLineParser80 1.0.1

CommandLineParser80

Sample Usage

Construction Examples

string[] args = Environment.GetCommandLineArgs()

CommandLineTokens tokens = CommandLineParser
	.Create(new WindowsCmdTokenParser())
	.Parse(args);
string[] args = Environment.GetCommandLineArgs()

ITokenParser tokenParser = new LinuxTokenParser();
CommandLineParser commandLinParser = CommandLineParser.Create(tokenParser)
CommandLineTokens tokens = commandLinParser.Parse(args);

Example Consumption

string command = tokens.Command;
bool quiet = tokens.HasSwitch("quiet"); || tokens.HasFlag("q")
bool verbose = tokens.HasSwitch("verbose") || tokens.HasFlag("v")
string? filename = tokens.SwitchValue("filename")?.Text ?? string.Empty;

if (string.IsNullOrEmpty(filename))
	throw new Exception("You must specify a filename with thee  --filename parameter.");

if (quite && verbose)
	throw new Exception("--quiet and --verbose are mutually exclusive!")

string[] lines = []
if (!FileExists(filename))
	throw new FileNotFoundException(filename);

lines = File.ReadAllLines(filename)
if (verbose)
	lines.ToList().ForEach(line => Console.WriteLine(line));

if(!quiet)
	Console.WriteLine($"Done reading file {filename}.");

CommandLineParser and associated test app, unit tests and packages.

Publishing

Build the project. From the repository root, push the .nupkg file to the NuGet server.

dotnet nuget push -s https://nuget.pillidar.com/v3/index.json .\source\CommandLineParser80\bin\debug\CommandLineParser80.1.0.0.nupkg -k <security_key_goes_here>

Usage

Input

You'll need an ICommandLineProvider to provide input. The normal provider for an application is the EnvironmentCommandLineProvider class. The normal provider for unit tests is the ListCommandLineProvider.

The provided ICommandLineProvider implementations are:

  • EnvironmentCommandLineProvider
  • MainArgsCommandLineProvider
  • ListCommandLineProvider

EnvironmentCommandLineProvider

This provider reads the command line parameters from the .NET Environment.GetCommandLineParameters() function.

MainArgsCommandLineProvider

This accepts a string array and feeds it back on demand. This is a simple straight-through implementation. It is designed to make it easy to consume your main(string[] args) parameters in a convenient way.

ListCommandLineProvider

While it may be useful in other scenarios, this is primarily provided for unit testing. It is similar to the MainArgsCommandLineProvider except that it works as a List instead of a string[] array.

Parsers

There are different ICommandLineParsers available. The one you choose usually depends on your operating system or your command line shell.

  • WindowsCmdTokenParser - a token parser that handles DOS/Windows (CMD)-style command lines.
  • LinuxTokenParser - a token parser that handles Linux-style command lines.
  • PowerShellTokenParser - a token parser that handles PowerShell-style command lines.
  • UniversalTokenParser - a hybrid parser that handles some CMD and Linux-style aspects.

WindowsCmdTokenParser

This parser handles these kinds of parameters:

  • /switch
  • /switch:value
  • /switch=value
  • -switch
  • -switch:value
  • -switch=value
  • --switch
  • --switch:value
  • --switch=value
  • paramvalue

LinuxTokenParser

The LinuxTokenparser parser handles these kinds of parameters:

  • -abcd (flags a, b, c, and d, same as -a -b -c -d)
  • --switch
  • --switch=value
  • paramvalue

PowerShellTokenParser

The PowerShellTokenParser parser handles these kinds of parameters:

  • -switch
  • -switch:value
  • -switch=value
  • paramvalue

UniversalTokenParser

The UniversalTokenParser parser handles these kinds of parameters:

  • -switch
  • --switch
  • paramvalue

Tokens

Parsers accept an array of parameters. There is no mechanism in this library to tokenize a command line because .NET always provides the command lines pre-tokenized for you. If you want this functionality, use this in combination with a tokenizing library.

When a parser parses an array of parameters, it produces a list of CommandLineToken objects.
The CommandLineToken class has these concepts:

  • Switch - A single parameter whose existence indicates that a value is "on" and whose absence indicates "off". Switches can also be name/value pairs when specified as a colon-delimited token.
  • Parameter - A single value. This may be treated as a switch by the application, but not by this library. A parameter may be treated by the application as a key or a value in a key/value pair.

Token Types

The valid token types are:

Token Type Description Examples
Parameter A plain text token. command1
filename
10.1.1.155
Switch A token with a special prefix to denote that it is a switch. The valid prefixes depend on which token parser is used. /all
/key:value
/key=value
--filename
--filename:file1
--filename=file1
-key1
-key1=value1
Flags A special switch which contains one or more single-character switches all concatenated together. -x
-czvf

The CommandLineToken Class

The CommandLineToken class represents a single token on the command line. It holds all the queryable attributes about the token.

Property Description Example Command Line Example Usage Example Output
RawText Returns the entire token's original text. "/param1" token.RawText "/param"
Text Returns the token's original text with the prefix removed. "/param1" token.Text "param"
Prefix Returns the part of the token that makes it a switch. Empty string if this is not a switch. "/param1" token.Prefix "/"
HasPrefix Returns whether or not the prefix is an empty string. Use .IsSwitch instead. "/param1" token.HasPrefix true
IsSwitch Returns whether the token has a special prefix to indicate that it is a switch. "/param1" token.IsSwitch true
IsParam Returns whether the token is a parameter, not a Flags or a Switch. "/param1" token.IsParam false
IsFlags Returns whether the token is a Flags. "-abcd" true
Value Returns the value part of the token. NOTE! This is not the same as the SwitchValue() or ParamValue() members of the CommandLineTokens class. These return only value data from this token. "/switch1=value1" token.Value "value1"

The CommandLineTokens Class

The CommandLineTokens class is a collection of CommandLineToken objects. It provides convenience methods for querying the tokens as a whole command line.

Method Description Example Command Line Example Usage Example Output
Switches Returns all switch tokens found in the collection. "/switch1 /switch2 param1" tokens.Switches() CommandLineToken { text: "switch1" }, CommandLineToken { text: "switch2" }
HasSwitch Indicates whether the specified switch is present in the collection. "/switch1 /switch2 param1" tokens.HasSwitch("switch1") true
Params Returns all param tokens found in the collection. "/switch1 param1 param2" tokens.Params() CommandLineToken { text: "param1" }, CommandLineToken { text: "param2" }
HasParam Indicates whether the specified switch is present in the collection. "/switch1 param1 param2" tokens.HasParam("param1") true
Flags Returns all the flags from any flags tokens found in the collection. "-abcd /switch1 param1 param2" tokens.Flags() IEnumerable<string> { "a", "b", "c", "d" }
HasFlag Indicates whether the specified flag is present in any flags tokens found in the collection. "-abcd /switch1 param1 param2" tokens.HasFlag("b") true

| Switch | Returns the token which contains the specified switch by name. | "/switch1 /switch2 param1" | tokens.Switch("switch2") | CommandLineToken { text: "switch2" } | | SwitchValue | Returns the token following the specified switch token, specified by switch name. | "/switch1 /switch2 param1" | tokens.SwitchValue("switch2") | CommandLineToken { text: "param1" } | | Param | Returns the token which contains the specified text / rawtext. | "param1 param2 param3" | tokens.Param("param2") | CommandLineToken { text: "param2" } | | ParamValue | Returns the token following the specified param token. | "param1 param2 param3" | tokens.ParamValue("param2") | CommandLineToken { text: "param3" } |

To Do

  • The WindowsTokenParser should be broken out into a CmdTokenParser and a PowerShellTokenParser. It makes no sense to cram both into a single parser.

  • The LinuxTokenParser does not support flags. That should be fixed.

No packages depend on CommandLineParser80.

.NET 8.0

  • No dependencies.

Version Downloads Last updated
1.0.1 12 06/23/2025
1.0.0 13 11/06/2024