CommandLineParserParseT(String, ParseOptions) Method

Parses the specified command line arguments using the type T.

Definition

Namespace: Ookii.CommandLine
Assembly: Ookii.CommandLine (in Ookii.CommandLine.dll) Version: 4.0.0+bb7ed9a8fdfd1cb21824750a8cf508863ad15086
C#
public static T Parse<T>(
	string[] args,
	ParseOptions? options = null
)
where T : class

Parameters

args  String
The command line arguments.
options  ParseOptions  (Optional)
The options that control parsing behavior and usage help formatting. If , the default options are used.

Type Parameters

T
The type defining the command line arguments.

Return Value

T
An instance of the type T, or if an error occurred, or argument parsing was canceled by the CommandLineArgumentAttributeCancelParsing property or a method argument that returned CancelModeAbort or .

Remarks

This is a convenience function that instantiates a CommandLineParserT, calls the ParseWithErrorHandling method, and returns the result. If an error occurs or parsing is canceled, it prints errors to the ParseOptionsError stream, and usage help to the UsageWriter if the HelpRequested property is . It then returns .

If the ParseOptionsError parameter is , output is written to a LineWrappingTextWriter for the standard error stream, wrapping at the console's window width. If the stream is redirected, output may still be wrapped, depending on the value returned by ConsoleWindowWidth.

Color is applied to the output depending on the value of the UsageWriterUseColor property, the ParseOptionsUseErrorColor property, and the capabilities of the console.

If you want more control over the parsing process, including custom error/usage output or handling the ArgumentParsed event, you should use the instance CommandLineParserTParse or CommandLineParserTParseWithErrorHandling method.

This method uses reflection to determine the arguments defined by the type T at runtime, unless the type has the GeneratedParserAttribute applied. For a type using that attribute, you can also use the generated static IParserProviderTSelfCreateParser(ParseOptions) or IParserTSelfParse(ParseOptions) methods on the arguments class instead.

Exceptions

ArgumentNullExceptionargs is .
CommandLineArgumentException An error occurred parsing the command line. Check the CommandLineArgumentExceptionCategory property for the exact reason for the error.
NotSupportedException The CommandLineParser cannot use T as the command line arguments type, because it violates one of the rules concerning argument names or positions, or has an argument type that cannot be parsed.

See Also