CommandLineParser Class
Parses command-line arguments with support for named and positional arguments.
public sealed class CommandLineParserRemarks
Named arguments support the following formats:
-name=valueor/name=value- Named argument with equals separator-name:valueor/name:value- Named argument with colon separator-nameor/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
| Name | Description |
|---|---|
| CommandLineParser() |
CommandLineParser()
CommandLineParser.cspublic CommandLineParser()No documentation is available.
Properties
| Name | Description |
|---|---|
| Current | Gets a parser for the current process's command-line arguments, excluding the executable path. |
| HelpRequested | Gets a value indicating whether help was requested through any of the standard help arguments (-?, /?, -help, /help, --help). |
Current
CommandLineParser.csGets a parser for the current process's command-line arguments, excluding the executable path.
public static CommandLineParser Current { get; }Property value
Remarks
Positions match the array provided to the application entry point: position 0 is the first argument, not the path of the executable.
HelpRequested
CommandLineParser.csMethods
| Name | Description |
|---|---|
| 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. |
HasArgument(string)
CommandLineParser.csParse(string[])
CommandLineParser.csParses the specified command-line arguments. Named arguments use / or - prefix with : or = separator. Help arguments (-?, /?, -help, /help, --help) set CommandLineParser.HelpRequested.
Parameters
argsstring[]- The command-line arguments to parse.