CommandLineParser Class

Parses command-line arguments with support for named and positional arguments.

public sealed class CommandLineParser

Remarks

Named arguments support the following formats:

  • -name=value or /name=value - Named argument with equals separator
  • -name:value or /name:value - Named argument with colon separator
  • -name or /name - Named argument without value (empty string value)

Argument names are case-insensitive.

Example

// CommandLineParser.Current contains the arguments of the current process.
// You can also parse custom arguments:
var parser = new CommandLineParser();
parser.Parse(["/name=John", "/verbose", "input.txt", "--help"]);

// Check if an argument exists
Assert.True(parser.HasArgument("verbose"));

// Get named argument value
Assert.Equal("John", parser.GetArgument("name"));

// Get argument at position (zero-based index in the argument array)
Assert.Equal("input.txt", parser.GetArgument(2));

// Check if help was requested (detects -?, /?, -help, /help, --help)
Assert.True(parser.HelpRequested);

Constructors

NameDescription
CommandLineParser()

CommandLineParser()

CommandLineParser.cs
public CommandLineParser()

No documentation is available.

Properties

NameDescription
CurrentGets a parser for the current process's command-line arguments, excluding the executable path.
HelpRequestedGets a value indicating whether help was requested through any of the standard help arguments (-?, /?, -help, /help, --help).

Gets a parser for the current process's command-line arguments, excluding the executable path.

public static CommandLineParser Current { get; }

Property value

CommandLineParser

Remarks

Positions match the array provided to the application entry point: position 0 is the first argument, not the path of the executable.

Gets a value indicating whether help was requested through any of the standard help arguments (-?, /?, -help, /help, --help).

public bool HelpRequested { get; }

Property value

bool

Methods

NameDescription
GetArgument(int)Gets the argument at the specified position.
GetArgument(string)Gets the value of the specified named argument.
HasArgument(string)Determines whether the specified named argument exists.
Parse(string[])Parses the specified command-line arguments. Named arguments use / or - prefix with : or = separator. Help arguments (-?, /?, -help, /help, --help) set CommandLineParser.HelpRequested.

GetArgument overloads

GetArgument(int)

CommandLineParser.cs

Gets the argument at the specified position.

public string? GetArgument(int position)
Parameters
position int
The zero-based position of the argument.
Returns

string? The argument at the specified position, or null if no argument exists at that position.

GetArgument(string)

CommandLineParser.cs

Gets the value of the specified named argument.

public string? GetArgument(string name)
Parameters
name string
The name of the argument.
Returns

string? The argument value, or null if the argument does not exist.

HasArgument(string)

CommandLineParser.cs

Determines whether the specified named argument exists.

public bool HasArgument(string name)

Parameters

name string
The name of the argument to check.

Returns

bool true if the argument exists; otherwise, false.

Parse(string[])

CommandLineParser.cs

Parses the specified command-line arguments. Named arguments use / or - prefix with : or = separator. Help arguments (-?, /?, -help, /help, --help) set CommandLineParser.HelpRequested.

public void Parse(string[] args)

Parameters

args string[]
The command-line arguments to parse.