CommandLineParser Class

Parses command line arguments defined by a type's properties and methods.

Definition

Namespace: Ookii.CommandLine
Assembly: Ookii.CommandLine (in Ookii.CommandLine.dll) Version: 4.0.0+bb7ed9a8fdfd1cb21824750a8cf508863ad15086
C#
public class CommandLineParser
Inheritance
Object    CommandLineParser
Derived

Remarks

The CommandLineParser class parses command line arguments into named, strongly-typed values. The accepted arguments are defined by the properties and methods of the type passed to the CommandLineParser(Type, ParseOptions) constructor. The result of a parsing operation is an instance of that type, created using the values that were supplied on the command line.

The arguments type must have a constructor that has no parameters, or a single parameter with the type CommandLineParser, which will receive the instance of the CommandLineParser class that was used to parse the arguments.

A property defines a command line argument if it is public, not , and has the CommandLineArgumentAttribute attribute applied. The CommandLineArgumentAttribute attribute has properties to determine the behavior of the argument, such as whether it's required or positional.

A method defines a command line argument if it is public, , has the CommandLineArgumentAttribute attribute applied, and one of the signatures shown in the documentation for the CommandLineArgumentAttribute attribute.

To parse arguments, invoke the Parse method or one of its overloads, or use ParseWithErrorHandling or one of its overloads to automatically handle errors and print usage help when requested.

The static ParseT(ParseOptions) method is a helper that create a CommandLineParser instance, and parse arguments with error handling in a single call. If using source generation with the GeneratedParserAttribute attribute, you can also use the generated IParserTSelfParse(ParseOptions) method.

The derived type CommandLineParserT provides strongly-typed instance Parse and CommandLineParserTParseWithErrorHandling methods, if you don't wish to use the static methods.

The CommandLineParser class is for applications with a single (root) command. If you wish to create an application with subcommands, use the CommandManager class instead.

The CommandLineParser supports two sets of rules for how to parse arguments; ParsingModeDefault mode and ParsingModeLongShort mode. For more details on these rules, please see the documentation on GitHub.

Constructors

CommandLineParser(ArgumentProvider, ParseOptions) Initializes a new instance of the CommandLineParser class using the specified arguments type and options.
CommandLineParser(Type, ParseOptions) Initializes a new instance of the CommandLineParser class using the specified arguments type and options.

Properties

AllowDuplicateArguments Gets a value indicating whether duplicate arguments are allowed.
AllowWhiteSpaceValueSeparator Gets a value indicating whether the name and the value of an argument may be in separate argument tokens.
ApplicationFriendlyName Gets the friendly name of the application for use in the version information.
ArgumentNameComparison Gets the string comparison used for argument names.
ArgumentNamePrefixes Gets the argument name prefixes used by this instance.
Arguments Gets the arguments supported by this CommandLineParser instance.
ArgumentsType Gets the type that was used to define the arguments.
Culture Gets the culture used to convert command line argument values from their string representation to the argument type.
Description Gets a description that is used when generating usage information.
HelpArgument Gets the automatic help argument, or an argument with the same name, if there is one.
HelpRequested Gets or sets a value that indicates whether usage help should be displayed if the Parse(String) method returned .
LongArgumentNamePrefix Gets the prefix to use for long argument names.
Mode Gets the command line argument parsing rules used by the parser.
NameValueSeparators Gets the characters used to separate the name and the value of an argument.
Options Gets the options used by this instance.
ParseResult Gets the result of the last command line argument parsing operation.
ProviderKind Gets the kind of provider that was used to determine the available arguments.
StringProvider Gets the LocalizedStringProvider implementation used to get strings for error messages and usage help.
Validators Gets the class validators for the arguments class.

Methods

EqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
GetArgument Gets a command line argument by name or alias.
GetDefaultArgumentNamePrefixes Gets the default argument name prefixes for the current platform.
GetDefaultNameValueSeparators Gets the default characters used to separate the name and the value of an argument.
GetExecutableName Gets the name of the executable used to invoke the application.
GetHashCodeServes as the default hash function.
(Inherited from Object)
GetShortArgument Gets a command line argument by short name or alias.
GetTypeGets the Type of the current instance.
(Inherited from Object)
GetUsage Gets a string containing command line usage help.
MemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
OnArgumentParsed Raises the ArgumentParsed event.
OnDuplicateArgument Raises the DuplicateArgument event.
Parse Parses the arguments returned by the EnvironmentGetCommandLineArgs method.
Parse(ReadOnlyMemoryString) Parses the specified command line arguments.
Parse(String) Parses the specified command line arguments.
ParseT(ParseOptions) Parses the arguments returned by the EnvironmentGetCommandLineArgs method using the type T.
ParseT(ReadOnlyMemoryString, ParseOptions) Parses the specified command line arguments using the type T.
ParseT(String, ParseOptions) Parses the specified command line arguments using the type T.
ParseWithErrorHandling Parses the arguments returned by the EnvironmentGetCommandLineArgs method, and displays error messages and usage help if required.
ParseWithErrorHandling(ReadOnlyMemoryString) Parses the specified command line arguments, and displays error messages and usage help if required.
ParseWithErrorHandling(String) Parses the specified command line arguments and displays error messages and usage help if required.
ToStringReturns a string that represents the current object.
(Inherited from Object)
WriteUsage Writes command line usage help using the specified UsageWriter instance.

Events

ArgumentParsed Event raised when an argument is parsed from the command line.
DuplicateArgument Event raised when an argument that is not multi-value is specified more than once.

Fields

DefaultLongArgumentNamePrefix Gets the default prefix used for long argument names if the Mode property is ParsingModeLongShort.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also