BadScript 2
Loading...
Searching...
No Matches
CommandLine.Parser Class Reference

Provides methods to parse command line arguments. More...

Inheritance diagram for CommandLine.Parser:

Public Member Functions

 Parser ()
 Initializes a new instance of the CommandLine.Parser class.
 
 Parser (Action< ParserSettings > configuration)
 Initializes a new instance of the Parser class, configurable with ParserSettings using a delegate.
 
 Parser (Func< ParserSettings > factory)
 
void Dispose ()
 Frees resources owned by the instance.
 
ParserResult< T > ParseArguments< T > (IEnumerable< string > args)
 Parses a string array of command line arguments constructing values in an instance of type T . Grammar rules are defined decorating public properties with appropriate attributes.
 
ParserResult< T > ParseArguments< T > (Func< T > factory, IEnumerable< string > args)
 Parses a string array of command line arguments constructing values in an instance of type T . Grammar rules are defined decorating public properties with appropriate attributes.
 
ParserResult< object > ParseArguments (IEnumerable< string > args, params Type[] types)
 Parses a string array of command line arguments for verb commands scenario, constructing the proper instance from the array of types supplied by types . Grammar rules are defined decorating public properties with appropriate attributes. The CommandLine.VerbAttribute must be applied to types in the array.
 

Package Functions

 Parser (ParserSettings settings)
 

Properties

static Parser Default [get]
 Gets the singleton instance created with basic defaults.
 
ParserSettings Settings [get]
 Gets the instance that implements CommandLine.ParserSettings in use.
 

Private Member Functions

 ~Parser ()
 Finalizes an instance of the CommandLine.Parser class.
 
void Dispose (bool disposing)
 

Static Private Member Functions

static Result< IEnumerable< Token >, ErrorTokenize (IEnumerable< string > arguments, IEnumerable< OptionSpecification > optionSpecs, ParserSettings settings)
 
static ParserResult< T > MakeParserResult< T > (ParserResult< T > parserResult, ParserSettings settings)
 
static ParserResult< T > DisplayHelp< T > (ParserResult< T > parserResult, TextWriter helpWriter, int maxDisplayWidth)
 
static IEnumerable< ErrorTypeHandleUnknownArguments (bool ignoreUnknownArguments)
 

Private Attributes

bool disposed
 

Static Private Attributes

static readonly Lazy< ParserDefaultParser
 

Detailed Description

Provides methods to parse command line arguments.

Definition at line 20 of file Parser.cs.

Constructor & Destructor Documentation

◆ Parser() [1/4]

CommandLine.Parser.Parser ( )

Initializes a new instance of the CommandLine.Parser class.

Definition at line 30 of file Parser.cs.

31 {
32 Settings = new ParserSettings { Consumed = true };
33 }
ParserSettings Settings
Gets the instance that implements CommandLine.ParserSettings in use.
Definition Parser.cs:80

◆ Parser() [2/4]

CommandLine.Parser.Parser ( Action< ParserSettings configuration)

Initializes a new instance of the Parser class, configurable with ParserSettings using a delegate.

Parameters
configurationThe Action<ParserSettings> delegate used to configure aspects and behaviors of the parser.

Definition at line 43 of file Parser.cs.

44 {
45 if (configuration == null)
46 {
47 throw new ArgumentNullException("configuration");
48 }
49
50 Settings = new ParserSettings();
51 configuration(Settings);
52 Settings.Consumed = true;
53 }

◆ Parser() [3/4]

CommandLine.Parser.Parser ( Func< ParserSettings factory)

Definition at line 55 of file Parser.cs.

56 {
57 if (factory == null)
58 {
59 throw new ArgumentNullException(nameof(factory));
60 }
61
62 Settings = factory();
63 Settings.Consumed = true;
64 }

◆ Parser() [4/4]

CommandLine.Parser.Parser ( ParserSettings  settings)
package

Definition at line 66 of file Parser.cs.

67 {
68 Settings = settings;
69 Settings.Consumed = true;
70 }

◆ ~Parser()

CommandLine.Parser.~Parser ( )
private

Finalizes an instance of the CommandLine.Parser class.

Definition at line 99 of file Parser.cs.

100 {
101 Dispose(false);
102 }
void Dispose()
Frees resources owned by the instance.
Definition Parser.cs:87

Member Function Documentation

◆ DisplayHelp< T >()

static ParserResult< T > CommandLine.Parser.DisplayHelp< T > ( ParserResult< T >  parserResult,
TextWriter  helpWriter,
int  maxDisplayWidth 
)
staticprivate

Definition at line 273 of file Parser.cs.

276 {
277 parserResult.WithNotParsed(errors =>
278 Maybe.Merge(errors.ToMaybe(), helpWriter.ToMaybe())
279 .Do((_, writer) =>
280 writer.Write(HelpText.AutoBuild(parserResult, maxDisplayWidth))
281 )
282 );
283
284 return parserResult;
285 }
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33

◆ Dispose() [1/2]

void CommandLine.Parser.Dispose ( )

Frees resources owned by the instance.

Definition at line 87 of file Parser.cs.

88 {
89 Dispose(true);
90
91 GC.SuppressFinalize(this);
92 }

◆ Dispose() [2/2]

void CommandLine.Parser.Dispose ( bool  disposing)
private

Definition at line 295 of file Parser.cs.

296 {
297 if (disposed)
298 {
299 return;
300 }
301
302 if (disposing)
303 {
304 if (Settings != null)
305 {
307 }
308
309 disposed = true;
310 }
311 }
void Dispose()
Frees resources owned by the instance.

◆ HandleUnknownArguments()

static IEnumerable< ErrorType > CommandLine.Parser.HandleUnknownArguments ( bool  ignoreUnknownArguments)
staticprivate

Definition at line 287 of file Parser.cs.

288 {
289 return ignoreUnknownArguments
290 ? Enumerable.Empty<ErrorType>()
291 .Concat(ErrorType.UnknownOptionError)
292 : Enumerable.Empty<ErrorType>();
293 }
ErrorType
Discriminator enumeration of CommandLine.Error derivates.
Definition Error.cs:13

◆ MakeParserResult< T >()

static ParserResult< T > CommandLine.Parser.MakeParserResult< T > ( ParserResult< T >  parserResult,
ParserSettings  settings 
)
staticprivate

Definition at line 265 of file Parser.cs.

266 {
267 return DisplayHelp(parserResult,
268 settings.HelpWriter,
269 settings.MaximumDisplayWidth
270 );
271 }

◆ ParseArguments()

ParserResult< object > CommandLine.Parser.ParseArguments ( IEnumerable< string >  args,
params Type[]  types 
)

Parses a string array of command line arguments for verb commands scenario, constructing the proper instance from the array of types supplied by types . Grammar rules are defined decorating public properties with appropriate attributes. The CommandLine.VerbAttribute must be applied to types in the array.

Parameters
argsA System.String array of command line arguments, normally supplied by application entry point.
typesA System.Type array used to supply verb alternatives.
Returns
A CommandLine.ParserResult<T> containing the appropriate instance with parsed values as a System.Object and a sequence of CommandLine.Error.
Exceptions
System.ArgumentNullExceptionThrown if one or more arguments are null.
System.ArgumentOutOfRangeExceptionThrown if types array is empty.

All types must expose a parameterless constructor. It's strongly recommended to use a generic overload.

Definition at line 216 of file Parser.cs.

217 {
218 if (args == null)
219 {
220 throw new ArgumentNullException("args");
221 }
222
223 if (types == null)
224 {
225 throw new ArgumentNullException("types");
226 }
227
228 if (types.Length == 0)
229 {
230 throw new ArgumentOutOfRangeException("types");
231 }
232
233 return MakeParserResult(InstanceChooser.Choose((arguments, optionSpecs) =>
234 Tokenize(arguments, optionSpecs, Settings),
235 types,
236 args,
244 ),
246 );
247 }
static ParserResult< object > Choose(Func< IEnumerable< string >, IEnumerable< OptionSpecification >, Result< IEnumerable< Token >, Error > > tokenizer, IEnumerable< Type > types, IEnumerable< string > arguments, StringComparer nameComparer, bool ignoreValueCase, CultureInfo parsingCulture, bool autoHelp, bool autoVersion, IEnumerable< ErrorType > nonFatalErrors)
static Result< IEnumerable< Token >, Error > Tokenize(IEnumerable< string > arguments, IEnumerable< OptionSpecification > optionSpecs, ParserSettings settings)
Definition Parser.cs:249
static IEnumerable< ErrorType > HandleUnknownArguments(bool ignoreUnknownArguments)
Definition Parser.cs:287
bool AllowMultiInstance
Gets or sets a value indicating whether options are allowed to be specified multiple times....
bool CaseInsensitiveEnumValues
Gets or sets a value indicating whether perform case sensitive comparisons of values....
bool AutoHelp
Gets or sets a value indicating whether implicit option or verb 'help' should be supported.
bool IgnoreUnknownArguments
Gets or sets a value indicating whether the parser shall move on to the next argument and ignore the ...
CultureInfo ParsingCulture
Gets or sets the culture used when parsing arguments to typed properties.
bool AutoVersion
Gets or sets a value indicating whether implicit option or verb 'version' should be supported.

◆ ParseArguments< T >() [1/2]

ParserResult< T > CommandLine.Parser.ParseArguments< T > ( Func< T >  factory,
IEnumerable< string >  args 
)

Parses a string array of command line arguments constructing values in an instance of type T . Grammar rules are defined decorating public properties with appropriate attributes.

Template Parameters
TType of the target instance built with parsed value.
Parameters
factoryA System.Func<T> delegate used to initialize the target instance.
argsA System.String array of command line arguments, normally supplied by application entry point.
Returns
A CommandLine.ParserResult<T> containing an instance of type T with parsed values and a sequence of CommandLine.Error.
Exceptions
System.ArgumentNullExceptionThrown if one or more arguments are null.

Definition at line 164 of file Parser.cs.

165 {
166 if (factory == null)
167 {
168 throw new ArgumentNullException("factory");
169 }
170
171 if (!typeof(T).IsMutable())
172 {
173 throw new ArgumentException("factory");
174 }
175
176 if (args == null)
177 {
178 throw new ArgumentNullException("args");
179 }
180
181 return MakeParserResult(InstanceBuilder.Build(Maybe.Just(factory),
182 (arguments, optionSpecs) =>
183 Tokenize(arguments, optionSpecs, Settings),
184 args,
192 ),
194 );
195 }

◆ ParseArguments< T >() [2/2]

ParserResult< T > CommandLine.Parser.ParseArguments< T > ( IEnumerable< string >  args)

Parses a string array of command line arguments constructing values in an instance of type T . Grammar rules are defined decorating public properties with appropriate attributes.

Template Parameters
TType of the target instance built with parsed value.
Parameters
argsA System.String array of command line arguments, normally supplied by application entry point.
Returns
A CommandLine.ParserResult<T> containing an instance of type T with parsed values and a sequence of CommandLine.Error.
Exceptions
System.ArgumentNullExceptionThrown if one or more arguments are null.

Definition at line 120 of file Parser.cs.

121 {
122 if (args == null)
123 {
124 throw new ArgumentNullException("args");
125 }
126
127 Maybe<Func<T>> factory = typeof(T).IsMutable()
128 ? Maybe.Just<Func<T>>(Activator.CreateInstance<T>)
129 : Maybe.Nothing<Func<T>>();
130
131 return MakeParserResult(InstanceBuilder.Build(factory,
132 (arguments, optionSpecs) =>
133 Tokenize(arguments, optionSpecs, Settings),
134 args,
142 ),
144 );
145 }
Models a CSharpx.Maybe when in empty state.
Definition Maybe.cs:76

◆ Tokenize()

static Result< IEnumerable< Token >, Error > CommandLine.Parser.Tokenize ( IEnumerable< string >  arguments,
IEnumerable< OptionSpecification optionSpecs,
ParserSettings  settings 
)
staticprivate

Definition at line 249 of file Parser.cs.

252 {
253 return settings.GetoptMode
254 ? GetoptTokenizer.ConfigureTokenizer(settings.NameComparer,
255 settings.IgnoreUnknownArguments,
256 settings.EnableDashDash,
257 settings.PosixlyCorrect
258 )(arguments, optionSpecs)
259 : Tokenizer.ConfigureTokenizer(settings.NameComparer,
260 settings.IgnoreUnknownArguments,
261 settings.EnableDashDash
262 )(arguments, optionSpecs);
263 }
static Func< IEnumerable< string >, IEnumerable< OptionSpecification >, Result< IEnumerable< Token >, Error > > ConfigureTokenizer(StringComparer nameComparer, bool ignoreUnknownArguments, bool enableDashDash, bool posixlyCorrect)

Member Data Documentation

◆ DefaultParser

readonly Lazy<Parser> CommandLine.Parser.DefaultParser
staticprivate
Initial value:
=
new Lazy<Parser>(() => new Parser(new ParserSettings { HelpWriter = Console.Error }))
Parser()
Initializes a new instance of the CommandLine.Parser class.
Definition Parser.cs:30

Definition at line 22 of file Parser.cs.

◆ disposed

bool CommandLine.Parser.disposed
private

Definition at line 25 of file Parser.cs.

Property Documentation

◆ Default

Parser CommandLine.Parser.Default
staticget

Gets the singleton instance created with basic defaults.

Definition at line 75 of file Parser.cs.

◆ Settings

ParserSettings CommandLine.Parser.Settings
get

Gets the instance that implements CommandLine.ParserSettings in use.

Definition at line 80 of file Parser.cs.

80{ get; }

The documentation for this class was generated from the following file: