CommandLineParser Class

Parses command line arguments defined by a class of the specified type.

Definition

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

Remarks

The CommandLineParser class can parse a set of command line arguments into values. Which arguments are accepted is determined from the constructor parameters, 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.

An argument defined by a constructor parameter is always positional, and is required if the parameter has no default value. If your type has multiple constructors, use the CommandLineConstructorAttribute attribute to indicate which one to use.

A constructor parameter with the type CommandLineParser is not an argument, but will be passed the instance of the CommandLineParser class used to parse the arguments when the type is instantiated.

A property defines a command line argument if it is public, not , and has the CommandLineArgumentAttribute attribute defined. The properties of the argument are determined by the properties of the CommandLineArgumentAttribute class.

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. The static ParseT(ParseOptions) method is a helper that will parse arguments and print error and usage information if required. Calling this method will be sufficient for most use cases.

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

The CommandLineParser class can generate detailed usage help for the defined arguments, which can be shown to the user to provide information about how to invoke your application from the command line. This usage is shown automatically by the ParseT(ParseOptions) method and the CommandManager class, or you can use the WriteUsage(UsageWriter) and GetUsage(UsageWriter, Int32) methods to generate it manually.

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; Default mode and LongShort mode. For more details on these rules, please see the documentation on GitHub.

Constructors

CommandLineParser 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 value indicating whether the value of an argument may be in a separate argument from its name.
ApplicationFriendlyName Gets the friendly name of the application.
ArgumentNameComparer Gets the string comparer 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, Int32) method returned .
LongArgumentNamePrefix Gets the prefix to use for long argument names.
Mode Gets the command line argument parsing rules used by the parser.
NameValueSeparator Gets or sets the character 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 call to the Parse(String, Int32) method.
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.
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.
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 GetCommandLineArgs method.
Parse(String, Int32) Parses the specified command line arguments, starting at the specified index.
ParseT(ParseOptions) Parses the arguments returned by the GetCommandLineArgs method using the type T.
ParseT(String, ParseOptions) Parses the specified command line arguments using the type T.
ParseT(String, Int32, ParseOptions) Parses the specified command line arguments, starting at the specified index, using the type T.
ParseWithErrorHandling Parses the arguments returned by the GetCommandLineArgs method, and displays error messages and usage help if required.
ParseWithErrorHandling(String, Int32) Parses the specified command line arguments, starting at the specified index, 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 to the specified TextWriter using the specified options.

Events

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

Fields

DefaultLongArgumentNamePrefix Gets the default prefix used for long argument names if Mode is LongShort.
DefaultNameValueSeparator Gets the default character used to separate the name and the value of an argument.

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