Joutsen.ArgumentsParsing  v0.2.1
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Joutsen.ArgumentsParsing.ArgumentsParser Class Reference

A configurable parser that can be used to parse a list of input strings and pass the values to a target objects properties. More...

Public Member Functions

 ArgumentsParser ()
 Creates a new instance of the parser without a program name. More...
 
 ArgumentsParser (DisplayArgument helpArgument)
 Creates a new instance of the parser with a custom help argument. More...
 
 ArgumentsParser (String programName)
 Creates a new instance of the parser with a program name. More...
 
 ArgumentsParser (DisplayArgument helpArgument, String programName)
 Creates a new instance of the parser with a program name. More...
 
 ArgumentsParser (ArgumentsListParserFactory factory)
 Creates a new instance of the parser with a custom factory for list parsers. More...
 
 ArgumentsParser (ArgumentsListParserFactory factory, DisplayArgument helpArgument)
 Creates a new instance of the parser with a custom factory for list parsers and a custom help argument. More...
 
 ArgumentsParser (ArgumentsListParserFactory factory, String programName)
 Creates a new instance of the parser with a custom factory for list parsers and a program name. More...
 
 ArgumentsParser (ArgumentsListParserFactory factory, DisplayArgument helpArgument, String programName)
 Creates a new instance of the parser with a custom factory for list parsers, a custom help argument and a program name. More...
 
void AddArgument (ParserArgument argument)
 Adds a new argument to the parser. More...
 
void AddArgument (string name, ParserArgument argument)
 Adds a new argument that is identified by its name to the parser. More...
 
void AddArgument (string name, string alternative, ParserArgument argument)
 Adds a new argument that is identified by its name or an alternative name to the parser. More...
 
void AddArgument (DisplayArgument argument)
 Adds a new argument that will display a message when invoked during parsing. More...
 
void AddArgument< FlagValueType > (FlagArgument< FlagValueType > argument)
 Adds a new argument that represents a flag. More...
 
void AddArgumentsGroup (ArgumentsGroup group)
 Adds a new argument group to the parser. More...
 
MutuallyExclusiveArgumentsGroup AddMutuallyExclusiveGroup (String name, String prefix="-", bool isRequired=false)
 A short hand method for adding a mutually exclusive arguments group. More...
 
SubParser AddSubParser (String name)
 Adds a new subparser to the parser. More...
 
SubParser AddSubParser (String name, String help)
 Adds a new subparser to the parser. More...
 
SubParser AddSubParser (String name, String destination, String help)
 Adds a new subparser to the parser. More...
 
SubParser< ResultType > AddSubParser< ResultType > (String name)
 Adds a new subparser to the parser that will create a typed result. More...
 
SubParser< ResultType > AddSubParser< ResultType > (String name, String help)
 Adds a new subparser to the parser that will create a typed result. More...
 
SubParser< ResultType > AddSubParser< TargetType, ResultType > (String name, Expression< Func< TargetType, ResultType >> destination, String help)
 
SubParser< ResultType > AddSubParser< ResultType > (String name, String destination, String help)
 Adds a new subparser to the parser that will create a typed result. More...
 
virtual ParseResult Parse (string[] args, object target=null)
 Parses an array of input strings and raises an exception if the list does not match the specified arguments. More...
 
virtual ParseResult Parse (Listable< string > args, object target=null)
 Parses a list of input strings and raises an exception if the list does not match the specified arguments. More...
 
virtual ParseResult Parse (ArgumentsIterator args, object target=null)
 Parses a list of input strings and raises an exception if the list does not match the specified arguments. More...
 
virtual ParseResult ParseKnown (string[] args, object target=null)
 Parses all known values in an array of input strings. More...
 
virtual ParseResult ParseKnown (Listable< string > args, object target=null)
 Parses all known values in a list of input strings. More...
 
virtual ParseResult ParseKnown (ArgumentsIterator args, object target=null)
 Parses all known values in a list of input strings. More...
 
virtual ParseResult< TResult > Parse< TResult > (string[] args)
 Parses an array of input strings and raises an exception if the list does not match the specified arguments. More...
 
virtual ParseResult< TResult > Parse< TResult > (Listable< string > args)
 Parses an array of input strings and raises an exception if the list does not match the specified arguments. More...
 
virtual ParseResult< TResult > Parse< TResult > (ArgumentsIterator args)
 Parses an array of input strings and raises an exception if the list does not match the specified arguments. More...
 
virtual ParseResult< TResult > ParseKnown< TResult > (string[] args)
 Parses all known values in an array of input strings. More...
 
virtual ParseResult< TResult > ParseKnown< TResult > (Listable< string > args)
 Parses all known values in a list of input strings. More...
 
virtual ParseResult< TResult > ParseKnown< TResult > (ArgumentsIterator args)
 Parses all known values in a list of input strings. More...
 
virtual string Usage ()
 Creates a string briefly describing the expected parser input. More...
 
virtual string Description ()
 Creates a description of all the arguments added to the parser. More...
 

Protected Member Functions

void AddSubParser (DefaultSubParser parser)
 Adds an existing SubParser to the parser. More...
 
ArgumentsListParser GetParserImpl (ArgumentsParserType parserType)
 Creates a list parser depending on the parse mode. More...
 

Protected Attributes

ArgumentsGroupCollection groups
 The groups of parser arguments for this parser. More...
 
ArgumentsListParserFactory factory
 The factory creating the actual list parsers depending on the parse mode. More...
 
String programName
 When used as a parser for command line arguments this can contain the name of the program. More...
 

Detailed Description

A configurable parser that can be used to parse a list of input strings and pass the values to a target objects properties.

Remarks
After adding arguments for the values to parse the parser can be used in two ways: Either parsing all the values that are recognized and parse validation from an input list. Or parsing strictly by stopping as soon as an unknown value is encountered or a validation fails.
Since
0.1.0

Constructor & Destructor Documentation

◆ ArgumentsParser() [1/8]

Joutsen.ArgumentsParsing.ArgumentsParser.ArgumentsParser ( )

Creates a new instance of the parser without a program name.

◆ ArgumentsParser() [2/8]

Joutsen.ArgumentsParsing.ArgumentsParser.ArgumentsParser ( DisplayArgument  helpArgument)

Creates a new instance of the parser with a custom help argument.

Parameters
helpArgumentthe argument that should be used to show help text and usage
Remarks
Replacing the default help argument can be usefull if only custom text is to be displayed or the help text should be displayed using a different identifying name. Default is "-h" and "--help".
Since
0.2.0

◆ ArgumentsParser() [3/8]

Joutsen.ArgumentsParsing.ArgumentsParser.ArgumentsParser ( String  programName)

Creates a new instance of the parser with a program name.

Parameters
programNamea name given to the parser that will be printed in the description and usage. This is usually used when parsing command line arguments.
Since
0.1.0

◆ ArgumentsParser() [4/8]

Joutsen.ArgumentsParsing.ArgumentsParser.ArgumentsParser ( DisplayArgument  helpArgument,
String  programName 
)

Creates a new instance of the parser with a program name.

Parameters
helpArgumentthe argument that should be used to show help text and usage
programNamea name given to the parser that will be printed in the description and usage. This is usually used when parsing command line arguments.
Remarks
Replacing the default help argument can be usefull if only custom text is to be displayed or the help text should be displayed using a different identifying name. Default is "-h" and "--help".
Since
0.2.0

◆ ArgumentsParser() [5/8]

Joutsen.ArgumentsParsing.ArgumentsParser.ArgumentsParser ( ArgumentsListParserFactory  factory)

Creates a new instance of the parser with a custom factory for list parsers.

Parameters
factorythe factory for the list parser
Remarks
This constructor can be used to replace the default parsers for the list values or in a derived class to add additional parsing modes.
Since
0.1.0

◆ ArgumentsParser() [6/8]

Joutsen.ArgumentsParsing.ArgumentsParser.ArgumentsParser ( ArgumentsListParserFactory  factory,
DisplayArgument  helpArgument 
)

Creates a new instance of the parser with a custom factory for list parsers and a custom help argument.

Parameters
factorythe factory for the list parser
helpArgumentthe argument that should be used to show help text and usage
Remarks
This constructor can be used to replace the default parsers for the list values or in a derived class to add additional parsing modes. Replacing the default help argument can be usefull if only custom text is to be displayed or the help text should be displayed using a different identifying name. Default is "-h" and "--help".
Since
0.2.0

◆ ArgumentsParser() [7/8]

Joutsen.ArgumentsParsing.ArgumentsParser.ArgumentsParser ( ArgumentsListParserFactory  factory,
String  programName 
)

Creates a new instance of the parser with a custom factory for list parsers and a program name.

Parameters
factorythe factory for the list parser
programNamea name given to the parser that will be printed in the description and usage. This is usually used when parsing command line arguments.
Remarks
This constructor can be used to replace the default parsers for the list values or in a derived class to add additional parsing modes.
Since
0.1.0

◆ ArgumentsParser() [8/8]

Joutsen.ArgumentsParsing.ArgumentsParser.ArgumentsParser ( ArgumentsListParserFactory  factory,
DisplayArgument  helpArgument,
String  programName 
)

Creates a new instance of the parser with a custom factory for list parsers, a custom help argument and a program name.

Parameters
factorythe factory for the list parser
programNamea name given to the parser that will be printed in the description and usage. This is usually used when parsing command line arguments.
helpArgumentthe argument that should be used to show help text and usage
Remarks
This constructor can be used to replace the default parsers for the list values or in a derived class to add additional parsing modes. Replacing the default help argument can be usefull if only custom text is to be displayed or the help text should be displayed using a different identifying name. Default is "-h" and "--help".
Since
0.2.0

Member Function Documentation

◆ AddArgument() [1/4]

void Joutsen.ArgumentsParsing.ArgumentsParser.AddArgument ( ParserArgument  argument)

Adds a new argument to the parser.

◆ AddArgument() [2/4]

void Joutsen.ArgumentsParsing.ArgumentsParser.AddArgument ( string  name,
ParserArgument  argument 
)

Adds a new argument that is identified by its name to the parser.

Parameters
namethe name that identifies this argument in the input list
argumentthe argument
Since
0.2.0

◆ AddArgument() [3/4]

void Joutsen.ArgumentsParsing.ArgumentsParser.AddArgument ( string  name,
string  alternative,
ParserArgument  argument 
)

Adds a new argument that is identified by its name or an alternative name to the parser.

Parameters
namethe name that identifies this argument in the input list
alternativethe alternative name that identifies this argument in the input list
argumentthe argument
Remarks
This method is a shorthand for registering an argument with a short and a long name. E.g. -a and –active.
Since
0.2.0

◆ AddArgument() [4/4]

void Joutsen.ArgumentsParsing.ArgumentsParser.AddArgument ( DisplayArgument  argument)

Adds a new argument that will display a message when invoked during parsing.

◆ AddArgument< FlagValueType >()

void Joutsen.ArgumentsParsing.ArgumentsParser.AddArgument< FlagValueType > ( FlagArgument< FlagValueType >  argument)

Adds a new argument that represents a flag.

Since
0.2.0

◆ AddArgumentsGroup()

void Joutsen.ArgumentsParsing.ArgumentsParser.AddArgumentsGroup ( ArgumentsGroup  group)

Adds a new argument group to the parser.

Remarks
This method allows adding of custom groups of arguments to the parser. A custom group can be used if a different prefix is desired or arguments behave different from the existing ones.

◆ AddMutuallyExclusiveGroup()

MutuallyExclusiveArgumentsGroup Joutsen.ArgumentsParsing.ArgumentsParser.AddMutuallyExclusiveGroup ( String  name,
String  prefix = "-",
bool  isRequired = false 
)

A short hand method for adding a mutually exclusive arguments group.

Parameters
namethe name of the new group
prefixthe prefix of the named arguments in the group
isRequireddetermines if one of the arguments in the group needs to be present when parsing or if the group's arguments are optional.
Returns
an empty MutuallyExclusiveArgumentsGroup
Since
0.2.0

◆ AddSubParser() [1/4]

SubParser Joutsen.ArgumentsParsing.ArgumentsParser.AddSubParser ( String  name)

Adds a new subparser to the parser.

Parameters
namethe name identifying the command this parser represents in the input string
Returns
a new empty SubParser
Since
0.2.0

◆ AddSubParser() [2/4]

SubParser Joutsen.ArgumentsParsing.ArgumentsParser.AddSubParser ( String  name,
String  help 
)

Adds a new subparser to the parser.

Parameters
namethe name identifying the command this parser represents in the input string
helpan additional help text that describes the command of the subparser
Returns
a new empty SubParser
Since
0.2.0

◆ AddSubParser() [3/4]

SubParser Joutsen.ArgumentsParsing.ArgumentsParser.AddSubParser ( String  name,
String  destination,
String  help 
)

Adds a new subparser to the parser.

Parameters
namethe name identifying the command this parser represents in the input string
destinationthe name of the key or property the parsed values will be available at after parsing
helpan additional help text that describes the command of the subparser
Returns
a new empty SubParser
Remarks
Using a destination allows the parser to store all parsed values in a single property of the target.
Since
0.2.0

◆ AddSubParser() [4/4]

void Joutsen.ArgumentsParsing.ArgumentsParser.AddSubParser ( DefaultSubParser  parser)
protected

Adds an existing SubParser to the parser.

◆ AddSubParser< ResultType >() [1/3]

SubParser<ResultType> Joutsen.ArgumentsParsing.ArgumentsParser.AddSubParser< ResultType > ( String  name)

Adds a new subparser to the parser that will create a typed result.

Template Parameters
ResultTypethe type of the instance that will contain all values parsed by the subparser
Parameters
namethe name identifying the command this parser represents in the input string
Since
0.2.0
Type Constraints
ResultType :new() 

◆ AddSubParser< ResultType >() [2/3]

SubParser<ResultType> Joutsen.ArgumentsParsing.ArgumentsParser.AddSubParser< ResultType > ( String  name,
String  help 
)

Adds a new subparser to the parser that will create a typed result.

Template Parameters
ResultTypethe type of the instance that will contain all values parsed by the subparser
Parameters
namethe name identifying the command this parser represents in the input string
helpan additional help text that describes the command of the subparser
Returns
a new empty SubParser
Since
0.2.0
Type Constraints
ResultType :new() 

◆ AddSubParser< ResultType >() [3/3]

SubParser<ResultType> Joutsen.ArgumentsParsing.ArgumentsParser.AddSubParser< ResultType > ( String  name,
String  destination,
String  help 
)

Adds a new subparser to the parser that will create a typed result.

Template Parameters
ResultTypethe type of the instance that will contain all values parsed by the subparser
Parameters
namethe name identifying the command this parser represents in the input string
destinationthe name of the key or property the parsed values will be available at after parsing
helpan additional help text that describes the command of the subparser
Returns
a new empty SubParser
Remarks
Using a destination allows the parser to store all parsed values in a single property of the target.
Since
0.2.0
Type Constraints
ResultType :new() 

◆ AddSubParser< TargetType, ResultType >()

SubParser<ResultType> Joutsen.ArgumentsParsing.ArgumentsParser.AddSubParser< TargetType, ResultType > ( String  name,
Expression< Func< TargetType, ResultType >>  destination,
String  help 
)
Type Constraints
ResultType :new() 

◆ Description()

virtual string Joutsen.ArgumentsParsing.ArgumentsParser.Description ( )
virtual

Creates a description of all the arguments added to the parser.

◆ GetParserImpl()

ArgumentsListParser Joutsen.ArgumentsParsing.ArgumentsParser.GetParserImpl ( ArgumentsParserType  parserType)
protected

Creates a list parser depending on the parse mode.

◆ Parse() [1/3]

virtual ParseResult Joutsen.ArgumentsParsing.ArgumentsParser.Parse ( string []  args,
object  target = null 
)
virtual

Parses an array of input strings and raises an exception if the list does not match the specified arguments.

args the input list to parse target the object which the parsed values should be added to, if null a generic target will be created

Returns
the result of parsing the input string which usually contains the target and an empty list of unparsed arguments ParseException if the input values cannot be parsed usuing the arguments of the parser
Since
0.2.0

◆ Parse() [2/3]

virtual ParseResult Joutsen.ArgumentsParsing.ArgumentsParser.Parse ( Listable< string >  args,
object  target = null 
)
virtual

Parses a list of input strings and raises an exception if the list does not match the specified arguments.

args the input list to parse target the object which the parsed values should be added to, if null a generic target will be created

Returns
the result of parsing the input string which usually contains the target and an empty list of unparsed arguments ParseException if the input values cannot be parsed usuing the arguments of the parser

◆ Parse() [3/3]

virtual ParseResult Joutsen.ArgumentsParsing.ArgumentsParser.Parse ( ArgumentsIterator  args,
object  target = null 
)
virtual

Parses a list of input strings and raises an exception if the list does not match the specified arguments.

args the iterator of the input list target the object which the parsed values should be added to, if null a generic target will be created

Returns
the result of parsing the input string which usually contains the target and an empty list of unparsed arguments ParseException if the input values cannot be parsed usuing the arguments of the parser
Since
0.2.0

◆ Parse< TResult >() [1/3]

virtual ParseResult<TResult> Joutsen.ArgumentsParsing.ArgumentsParser.Parse< TResult > ( string []  args)
virtual

Parses an array of input strings and raises an exception if the list does not match the specified arguments.

Template Parameters
TResultthe type of object to create and store the parsed values in args the input list to parse
Returns
the result of parsing the input string which usually contains the target and an empty list of unparsed arguments ParseException if the input values cannot be parsed usuing the arguments of the parser
Since
0.2.0
Type Constraints
TResult :new() 

◆ Parse< TResult >() [2/3]

virtual ParseResult<TResult> Joutsen.ArgumentsParsing.ArgumentsParser.Parse< TResult > ( Listable< string >  args)
virtual

Parses an array of input strings and raises an exception if the list does not match the specified arguments.

Template Parameters
TResultthe type of object to create and store the parsed values in args the input list to parse
Returns
the result of parsing the input string which usually contains the target and an empty list of unparsed arguments ParseException if the input values cannot be parsed usuing the arguments of the parser
Since
0.2.0
Type Constraints
TResult :new() 

◆ Parse< TResult >() [3/3]

virtual ParseResult<TResult> Joutsen.ArgumentsParsing.ArgumentsParser.Parse< TResult > ( ArgumentsIterator  args)
virtual

Parses an array of input strings and raises an exception if the list does not match the specified arguments.

Template Parameters
TResultthe type of object to create and store the parsed values in args the iterator of the input list to parse
Returns
the result of parsing the input string which usually contains the target and an empty list of unparsed arguments ParseException if the input values cannot be parsed usuing the arguments of the parser
Since
0.2.0
Type Constraints
TResult :new() 

◆ ParseKnown() [1/3]

virtual ParseResult Joutsen.ArgumentsParsing.ArgumentsParser.ParseKnown ( string []  args,
object  target = null 
)
virtual

Parses all known values in an array of input strings.

All input strings that cannot be parsed are ignored. args the input list to parse target the object which the parsed values should be added to, if null a generic target will be created

Returns
the result of parsing the input string which contains the target and the list of input values that could not be parsed
Since
0.2.0

◆ ParseKnown() [2/3]

virtual ParseResult Joutsen.ArgumentsParsing.ArgumentsParser.ParseKnown ( Listable< string >  args,
object  target = null 
)
virtual

Parses all known values in a list of input strings.

All input strings that cannot be parsed are ignored. args the input list to parse target the object which the parsed values should be added to, if null a generic target will be created

Returns
the result of parsing the input string which contains the target and the list of input values that could not be parsed

◆ ParseKnown() [3/3]

virtual ParseResult Joutsen.ArgumentsParsing.ArgumentsParser.ParseKnown ( ArgumentsIterator  args,
object  target = null 
)
virtual

Parses all known values in a list of input strings.

All input strings that cannot be parsed are ignored. args the iterator of the input list to parse target the object which the parsed values should be added to, if null a generic target will be created

Returns
the result of parsing the input string which contains the target and the list of input values that could not be parsed

◆ ParseKnown< TResult >() [1/3]

virtual ParseResult<TResult> Joutsen.ArgumentsParsing.ArgumentsParser.ParseKnown< TResult > ( string []  args)
virtual

Parses all known values in an array of input strings.

All input strings that cannot be parsed are ignored.

Template Parameters
TResultthe type of object to create and store the parsed values in args the input list to parse
Returns
the result of parsing the input string which contains the target and the list of input values that could not be parsed
Since
0.2.0
Type Constraints
TResult :new() 

◆ ParseKnown< TResult >() [2/3]

virtual ParseResult<TResult> Joutsen.ArgumentsParsing.ArgumentsParser.ParseKnown< TResult > ( Listable< string >  args)
virtual

Parses all known values in a list of input strings.

All input strings that cannot be parsed are ignored.

Template Parameters
TResultthe type of object to create and store the parsed values in args the input list to parse
Returns
the result of parsing the input string which contains the target and the list of input values that could not be parsed
Type Constraints
TResult :new() 

◆ ParseKnown< TResult >() [3/3]

Parses all known values in a list of input strings.

All input strings that cannot be parsed are ignored.

Template Parameters
TResultthe type of object to create and store the parsed values in args the iterator of the input list to parse
Returns
the result of parsing the input string which contains the target and the list of input values that could not be parsed
Type Constraints
TResult :new() 

◆ Usage()

virtual string Joutsen.ArgumentsParsing.ArgumentsParser.Usage ( )
virtual

Creates a string briefly describing the expected parser input.

Member Data Documentation

◆ factory

ArgumentsListParserFactory Joutsen.ArgumentsParsing.ArgumentsParser.factory
protected

The factory creating the actual list parsers depending on the parse mode.

◆ groups

ArgumentsGroupCollection Joutsen.ArgumentsParsing.ArgumentsParser.groups
protected

The groups of parser arguments for this parser.

◆ programName

String Joutsen.ArgumentsParsing.ArgumentsParser.programName
protected

When used as a parser for command line arguments this can contain the name of the program.