Optparse.hx

Command line argument parsing.

Summary
Optparse.hxCommand line argument parsing.
ReaderResultResult of some reader function.
Variables
valueThe value read by a reader function.
advanceHow many parameters has been parsed by the reader function.
Types
Types
OptionReaderPrototype of a reader function.
OptionWriterPrototype of a writer function.
OptparseExceptionException class thrown by Optparse
Functions
newConstructor
toStringConverts the exception to a string.
OptparseCommand line argument parsing class.
newConstructor.
addOptionAdds a new command line option to the parser.
parseParses the command line arguments according to the previously specified options by addOption.
getHelpGenerates option usage help from the previously specified help and help_arg paramteres in addOption.
Reader functions
readerNullNull option reader.
readerStringString option reader.
readerIntInteger option reader.
readerFloatFloat option reader.
readerKVArrayKey/value pair list option reader.
Writer functions
writerNullNull option writer.
writerStoreStore option writer.
writerStoreTrueTrue boolean value option writer.
writerStoreFalseFalse boolean value option writer.
writerIncrementIncrement option writer.
writerAppendArray append option writer.
makeWriterStoreConstConstant option writer creator.

ReaderResult

Result of some reader function.

Summary
Variables
valueThe value read by a reader function.
advanceHow many parameters has been parsed by the reader function.

Variables

value

value: Dynamic

The value read by a reader function.

advance

advance: Int

How many parameters has been parsed by the reader function.

Types

Summary
Types
OptionReaderPrototype of a reader function.
OptionWriterPrototype of a writer function.

Types

OptionReader

Prototype of a reader function.

Array<String> -> Int -> ReaderResult;

OptionWriter

Prototype of a writer function.

Dynamic -> String -> Dynamic -> Void

OptparseException

Exception class thrown by Optparse

Summary
Functions
newConstructor
toStringConverts the exception to a string.

Functions

new

public function new(message: String,
arg: String,
arg_pos: Int)

Constructor

Parameters

messagethe exception message.
argthe argument where the exception occured.
arg_posthe position of the argument in argument list.

toString

public function toString()

Converts the exception to a string.

Returns

The string representation of the exception.

Optparse

Command line argument parsing class.

Summary
newConstructor.
addOptionAdds a new command line option to the parser.
parseParses the command line arguments according to the previously specified options by addOption.
getHelpGenerates option usage help from the previously specified help and help_arg paramteres in addOption.
Reader functions
readerNullNull option reader.
readerStringString option reader.
readerIntInteger option reader.
readerFloatFloat option reader.
readerKVArrayKey/value pair list option reader.
Writer functions
writerNullNull option writer.
writerStoreStore option writer.
writerStoreTrueTrue boolean value option writer.
writerStoreFalseFalse boolean value option writer.
writerIncrementIncrement option writer.
writerAppendArray append option writer.
makeWriterStoreConstConstant option writer creator.

new

public function new()

Constructor.

addOption

public function addOption(short: String,
long: String,
field: String,
reader: OptionReader,
writer: OptionWriter,
?help: String,
?help_arg: String)

Adds a new command line option to the parser.

Parameters

shortthe short version of the command line option.  Can be null if no short version is required.  (Example: “-h”)
longthe long version of the command line option.  Can be null if no long version is required.  (Example: “--help”)
fieldthe name of the field which will hold the parsed argument value
readerthe option reader function which will parse the command line option and it’s arguments (if there are any)
writerthe option writer function which will store the value parsed by the reader (or any other value if necessary)
helpthe help text for the option.  It will be included in the help message generated by getHelp.  Can be omitted.
help_argthe format description for the argument(s) of the command line option.  Can be omitted.

parse

public function parse(options: Dynamic,  
args: Array<String>,  
start: Int =  0): Int

Parses the command line arguments according to the previously specified options by addOption.

Parameters

optionsthe object holding all the option fields
argsthe array of command line arguments
startthe start index of parsing

Returns

The index of the command line argument which cannot be parsed.

getHelp

public function getHelp(): StringBuf

Generates option usage help from the previously specified help and help_arg paramteres in addOption.

Returns

The generated help as a String.

Reader functions

readerNull

public static function readerNull(args: Array<String>,
act: Int): ReaderResult

Null option reader.  Used for options without arguments.

Parameters

argsthe array of command line arguments
actthe index of actually parsed argument

Returns

valuenull
advance1

readerString

public static function readerString(args: Array<String>,
act: Int): ReaderResult

String option reader.  Used for options with exactly one argument.

Parameters

argsthe array of command line arguments
actthe index of actually parsed argument

Returns

valuethe argument as a String
advance2

readerInt

public static function readerInt(args: Array<String>,
act: Int): ReaderResult

Integer option reader.  Used for options with exactly one integer argument.

Parameters

argsthe array of command line arguments
actthe index of actually parsed argument

Returns

valuethe argument as an Int
advance2

Throws

OptparseException if the argument is missing or cannot be converted to Int.

readerFloat

public static function readerFloat(args: Array<String>,
act: Int): ReaderResult

Float option reader.  Used for options with exactly one floating point argument.

Parameters

argsthe array of command line arguments
actthe index of actually parsed argument

Returns

valuethe argument as a Float
advance2

Throws

OptparseException if the argument is missing or cannot be converted to Float.

readerKVArray

public static function readerKVArray(args: Array<String>,
act: Int): ReaderResult

Key/value pair list option reader.  Used for options with a list of key/value pairs argument.  The key/value pairs are in the following format:

key[=value][:key[=value]...]

Values can be omitted along with equal sign.

Parameters

argsthe array of command line arguments
actthe index of actually parsed argument

Returns

valuethe argument as an Array<{key: String, value: String}>
advance2 (the whole key/value list is one string without spaces)

Throws

OptparseException if the argument is missing or the one of the key/value pairs has an invalid format.

Writer functions

writerNull

public static function writerNull(options: Dynamic,
field: String,
value: Dynamic)

Null option writer.  Used when the processed option doesn’t have any argument or when the previously parsed argument doesn’t need to be stored.

Parameters

optionsthe object holding all the option fields
fieldthe name of the field to store value in
valuethe previously parsed value to be stored

writerStore

public static function writerStore(options: Dynamic,
field: String,
value: Dynamic)

Store option writer.  Stores the previously parsed argument into the specified option field.

writerStoreTrue

public static function writerStoreTrue(options: Dynamic,
field: String,
value: Dynamic)

True boolean value option writer.  Stores true into the specified field as a Bool.

Parameters

optionsthe object holding all the option fields
fieldthe name of the field to store value in
valuethe previously parsed value to be stored

Field requirements

  • Has to be Bool type.

writerStoreFalse

public static function writerStoreFalse(options: Dynamic,
field: String,
value: Dynamic)

False boolean value option writer.  Stores false into the specified field as a Bool.

Parameters

optionsthe object holding all the option fields
fieldthe name of the field to store value in
valuethe previously parsed value to be stored

Field requirements

  • Has to be Bool type.

writerIncrement

public static function writerIncrement(options: Dynamic,
field: String,
value: Dynamic)

Increment option writer.  Increments the specified field by one.

Parameters

optionsthe object holding all the option fields
fieldthe name of the field to store value in
valuethe previously parsed value to be stored

Field requirements

  • Has to be Int or Float type.

writerAppend

public static function writerAppend(options: Dynamic,
field: String,
value: Dynamic)

Array append option writer.  Appends the previously parsed argument to the specified array field.

Parameters

optionsthe object holding all the option fields
fieldthe name of the field to store value in
valuethe previously parsed value to be stored

Field requirements

  • Has to be Array type.

makeWriterStoreConst

public static function makeWriterStoreConst(
   user_const: Dynamic
): Dynamic -> String -> Dynamic -> Void

Constant option writer creator.  Returns a writer function which stores the specified value into the option field.

Parameters

user_constthe value to be stored

Returns

The appropriate option writer function.

value: Dynamic
The value read by a reader function.
advance: Int
How many parameters has been parsed by the reader function.
Command line argument parsing class.
public function new(message: String,
arg: String,
arg_pos: Int)
Constructor
public function toString()
Converts the exception to a string.
public function new()
Constructor.
public function addOption(short: String,
long: String,
field: String,
reader: OptionReader,
writer: OptionWriter,
?help: String,
?help_arg: String)
Adds a new command line option to the parser.
public function parse(options: Dynamic,  
args: Array<String>,  
start: Int =  0): Int
Parses the command line arguments according to the previously specified options by addOption.
public function getHelp(): StringBuf
Generates option usage help from the previously specified help and help_arg paramteres in addOption.
public static function readerNull(args: Array<String>,
act: Int): ReaderResult
Null option reader.
public static function readerString(args: Array<String>,
act: Int): ReaderResult
String option reader.
public static function readerInt(args: Array<String>,
act: Int): ReaderResult
Integer option reader.
public static function readerFloat(args: Array<String>,
act: Int): ReaderResult
Float option reader.
public static function readerKVArray(args: Array<String>,
act: Int): ReaderResult
Key/value pair list option reader.
public static function writerNull(options: Dynamic,
field: String,
value: Dynamic)
Null option writer.
public static function writerStore(options: Dynamic,
field: String,
value: Dynamic)
Store option writer.
public static function writerStoreTrue(options: Dynamic,
field: String,
value: Dynamic)
True boolean value option writer.
public static function writerStoreFalse(options: Dynamic,
field: String,
value: Dynamic)
False boolean value option writer.
public static function writerIncrement(options: Dynamic,
field: String,
value: Dynamic)
Increment option writer.
public static function writerAppend(options: Dynamic,
field: String,
value: Dynamic)
Array append option writer.
public static function makeWriterStoreConst(
   user_const: Dynamic
): Dynamic -> String -> Dynamic -> Void
Constant option writer creator.
Exception class thrown by Optparse
Close