class RemiLib::Args::ArgParser
- RemiLib::Args::ArgParser
- Reference
- Object
Overview
The main class for parsing command line arguments.
Defined in:
remilib/args/arg-parsing.crConstructors
-
.new(progName : String, progVersion : String, *, helpLongName : String = "--help", helpShortName : Char = 'h', verLongName : String = "--version", verShortName : Char = 'V', newProgBinName : String = PROGRAM_NAME, newArgv0 : String = PROGRAM_NAME)
Creates a new
ArgParser
instance.
Class Method Summary
-
.defaultHelpPrinter(parser : ArgParser) : Nil
The default printer for help arguments (e.g.
-
.defaultVerPrinter(parser : ArgParser) : Nil
The default printer for version arguments (e.g.
Instance Method Summary
-
#[](name : String) : Argument
Returns the
Argument
that has the givenArgument#longName
. -
#[](name : Char) : Argument
Returns the
Argument
that has the givenArgument#shortName
. -
#addArg(arg : Argument)
Adds a new
Argument
to this parser. -
#addFlag(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "") : FlagArgument
Adds a new
FlagArgument
. -
#addFlag(longName : String, shortName : Char | Nil = nil, &) : Nil
Adds a new
FlagArgument
. -
#addFloat(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : Float64 = 0.0, minimum : Float64 = Float64::MIN, maximum : Float64 = Float64::MAX) : FloatArgument
Adds a new
FloatArgument
. -
#addFloat(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : Float64 = 0.0, minimum : Float64 = Float64::MIN, maximum : Float64 = Float64::MAX, &) : Nil
Adds a new
FloatArgument
. -
#addInt(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : Int = 0, minimum : Int = Int64::MIN, maximum : Int = Int64::MAX) : IntArgument
Adds a new
IntArgument
. -
#addInt(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : Int = 0, minimum : Int = Int64::MIN, maximum : Int = Int64::MAX, &) : Nil
Adds a new
IntArgument
. -
#addMultiFlag(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "") : MultiFlagArgument
Adds a new
MultiFlagArgument
. -
#addMultiFlag(longName : String, shortName : Char | Nil = nil, &) : Nil
Adds a new
MultiFlagArgument
. -
#addMultiFloat(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : Array(Float64) = [] of Float64) : MultiIntArgument
Adds a new
MultiIntArgument
. -
#addMultiInt(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : Array(Int64) = [] of Int64) : MultiIntArgument
Adds a new
MultiIntArgument
. -
#addMultiInt(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : Array(Float64) = [] of Float64, &) : Nil
Adds a new
MultiIntArgument
. -
#addMultiString(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : Array(String) = [] of String, constraints : Array(String) | Nil = nil) : MultiStringArgument
Adds a new
MultiStringArgument
. -
#addMultiString(longName : String, shortName : Char | Nil = nil, *, group : String = "", &) : Nil
Adds a new
MultiStringArgument
. -
#addString(longName : String, shortName : Char | Nil = nil, *, group : String = "", help : String = "", default : String = "", constraints : Array(String) | Nil = nil) : StringArgument
Adds a new
StringArgument
. -
#addString(longName : String, shortName : Char | Nil = nil, *, group : String = "", &) : Nil
Adds a new
StringArgument
. -
#allArgGroups : Array(String)
Returns an array of Strings containing all fo the argument groups.
-
#allArgsInGroup(group : String) : Array(Argument)
Returns an array of
Argument
s that are in the given group. -
#args : Hash(String, Argument)
The
Argument
s defined for this parser. -
#detectHelp=(detectHelp : Bool)
When
true
, the help argument (e.g. -
#detectHelp? : Bool
When
true
, the help argument (e.g. -
#detectVer=(detectVer : Bool)
When
true
, the version argument (e.g. -
#detectVer? : Bool
When
true
, the version argument (e.g. -
#doubleDashPositional=(doubleDashPositional : Bool)
When true, the double dash
--
will be treated as a positional argument. -
#doubleDashPositional? : Bool
When true, the double dash
--
will be treated as a positional argument. -
#each(&) : Nil
Executes a block on every argument.
-
#eachCalled(&) : Nil
Executes a block on every argument that was
Argument#called
. -
#helpPrinter : HelpPrinterFunc
This will be called when a help argument (e.g.
-
#helpPrinter=(helpPrinter : HelpPrinterFunc)
This will be called when a help argument (e.g.
-
#longestArgName : Int32
Returns the length of the longest
Argument#longName
found in#args
. -
#parse(theseArgs : Array(String)) : Nil
Parses an array containing command line arguments.
-
#parse : Nil
Parses
ARGV
. -
#positionalArgs : Array(String)
Any non-argument strings left on the command line after parsing.
-
#positionalArgs=(positionalArgs : Array(String))
Any non-argument strings left on the command line after parsing.
-
#postHelpText : String
A string that is to be printed after the list of arguments when a help argument (e.g.
-
#postHelpText=(postHelpText : String)
A string that is to be printed after the list of arguments when a help argument (e.g.
-
#postVerText : String
A string that is to be printed after the version information when a version argument (e.g.
-
#postVerText=(postVerText : String)
A string that is to be printed after the version information when a version argument (e.g.
-
#preHelpText : String
A string that is to be printed after the "Usage:" line but before the list of arguments when a help argument (e.g.
-
#preHelpText=(preHelpText : String)
A string that is to be printed after the "Usage:" line but before the list of arguments when a help argument (e.g.
-
#preVerText : String
A string that is to be printed before version information when a version argument (e.g.
-
#preVerText=(preVerText : String)
A string that is to be printed before version information when a version argument (e.g.
-
#progBinName : String
The name of the binary using this
ArgParser
. -
#progBinName=(newName : String)
Sets the name of the binary using this
ArgParser
. -
#progName : String
The name of the program using this parser.
-
#progName=(progName : String)
The name of the program using this parser.
-
#progVersion : String
The version of the program using this parser.
-
#progVersion=(progVersion : String)
The version of the program using this parser.
-
#quitOnHelp=(quitOnHelp : Bool)
When
true
,#parse
will callexit(0)
after printing help when a help argument (e.g. -
#quitOnHelp? : Bool
When
true
,#parse
will callexit(0)
after printing help when a help argument (e.g. -
#quitOnVer=(quitOnVer : Bool)
When
true
,#parse
will callexit(0)
after printing version information when a version argument (e.g. -
#quitOnVer? : Bool
When
true
,#parse
will callexit(0)
after printing version information when a version argument (e.g. -
#singleDashPositional=(singleDashPositional : Bool)
When true, a single dash
-
will be and stored as a positional argument. -
#singleDashPositional? : Bool
When true, a single dash
-
will be and stored as a positional argument. -
#usageLine : String | Nil
When non-nil, this will be used for the "Usage:" line instead of the generated one.
-
#usageLine=(usageLine : String | Nil)
When non-nil, this will be used for the "Usage:" line instead of the generated one.
-
#verPrinter : VerPrinterFunc
This will be called when a version argument (e.g.
-
#verPrinter=(verPrinter : VerPrinterFunc)
This will be called when a version argument (e.g.
-
#withCalledArg(name : String, &)
If an argument named
name
was called, then this yields that argument to the block. -
#withCalledFloatArg(name : String, &)
Looks up the argument named
name
, casts it to aFloatArgument
. -
#withCalledIntArg(name : String, &)
Looks up the argument named
name
, casts it to anIntArgument
. -
#withCalledMultiFloatArg(name : String, &)
Looks up the argument named
name
, casts it to aMultiFloatArgument
. -
#withCalledMultiIntArg(name : String, &)
Looks up the argument named
name
, casts it to aMultiIntArgument
. -
#withCalledMultiStringArg(name : String, &)
Looks up the argument named
name
, casts it to anMultiStringArgument
. -
#withCalledStringArg(name : String, &)
Looks up the argument named
name
, casts it to anStringArgument
. -
#withFlagArg(name : String, &)
Looks up the argument named
name
, casts it to anFlagArgument
, then yields it to the block. -
#withFloatArg(name : String, &)
Looks up the argument named
name
, casts it to aFloatArgument
, then yields it to the block. -
#withIntArg(name : String, &)
Looks up the argument named
name
, casts it to anIntArgument
, then yields it to the block. -
#withMultiFlagArg(name : String, &)
Looks up the argument named
name
, casts it to anMultiFlagArgument
, then yields it to the block. -
#withMultiFloatArg(name : String, &)
Looks up the argument named
name
, casts it to aMultiFloatArgument
, then yields it to the block. -
#withMultiIntArg(name : String, &)
Looks up the argument named
name
, casts it to aMultiIntArgument
, then yields it to the block. -
#withMultiStringArg(name : String, &)
Looks up the argument named
name
, casts it to anMultiStringArgument
, then yields it to the block. -
#withStringArg(name : String, &)
Looks up the argument named
name
, casts it to anStringArgument
, then yields it to the block.
Constructor Detail
Creates a new ArgParser
instance. A help argument and a version
argument will always be added as FlagArgument
s.
Class Method Detail
The default printer for help arguments (e.g. "--help"
/"-h"
).
The default output looks something like this (depending on the values of
#preHelpText
and #postHelpText
:
Usage: /path/to/binary [options]
General Options
================================================================================
--help / -h : Show this help text
--version / -V : Show version information
--foo x / -f x : Adds a foo
The default printer for version arguments (e.g. "--version"
/"-V"
).
The default output looks something like this (depending on the values of
#preVerText
and #postVerText
:
My Program v0.1.0
Instance Method Detail
Returns the Argument
that has the given Argument#longName
. The "--"
prefix is optional.
Returns the Argument
that has the given Argument#shortName
.
Adds a new Argument
to this parser. The argument must have a
Argument#longName
. Arguments with duplicate Argument#longName
s and
Argument#shortName
s will raise an Exception
.
Adds a new FlagArgument
.
Adds a new FloatArgument
.
Adds a new FloatArgument
.
Adds a new IntArgument
.
Adds a new IntArgument
.
Adds a new MultiFlagArgument
.
Adds a new MultiFlagArgument
.
Adds a new MultiIntArgument
.
Adds a new MultiIntArgument
.
Adds a new MultiIntArgument
.
Adds a new MultiStringArgument
.
Adds a new MultiStringArgument
.
Adds a new StringArgument
.
Adds a new StringArgument
.
Returns an array of Strings containing all fo the argument groups. Note that the default argument group is an empty string.
Returns an array of Argument
s that are in the given group.
The Argument
s defined for this parser. Do not add arguments directly to
this field - use one of the add*
methods instead.
When true
, the help argument (e.g. "--help"
/"-h"
) will be processed.
Otherwise it is ignored.
When true
, the help argument (e.g. "--help"
/"-h"
) will be processed.
Otherwise it is ignored.
When true
, the version argument (e.g. "--version"
/"-V"
) will be
processed. Otherwise it is ignored.
When true
, the version argument (e.g. "--version"
/"-V"
) will be
processed. Otherwise it is ignored.
When true, the double dash --
will be treated as a positional argument.
When true, the double dash --
will be treated as a positional argument.
This will be called when a help argument (e.g. "--help"
/"-h"
) is found
on the command line.
This will be called when a help argument (e.g. "--help"
/"-h"
) is found
on the command line.
Returns the length of the longest Argument#longName
found in #args
.
Parses an array containing command line arguments.
If a help argument or version argument is found on the command line, and
#detectHelp
/#detectVer
is true, then the corresponding
#helpPrinter
/#verPrinter
will be called.
If a help argument or version argument is found on the command line, and
#quitOnHelp
/#quitOnVer
is true, then exit(0)
will be called after
executing the corresponding method.
After parsing, any argument that has a Argument#callback
method will
have that method executed.
Parses ARGV
.
If a help argument or version argument is found on the command line, and
#detectHelp
/#detectVer
is true, then the corresponding
#helpPrinter
/#verPrinter
will be called.
If a help argument or version argument is found on the command line, and
#quitOnHelp
/#quitOnVer
is true, then exit(0)
will be called after
executing the corresponding method.
After parsing, any argument that has a Argument#callback
method will
have that method executed.
Any non-argument strings left on the command line after parsing.
Any non-argument strings left on the command line after parsing.
A string that is to be printed after the list of arguments when a help
argument (e.g. "--help"
/"-h"
) is found on the command line. A newline
will be appended automatically.
A string that is to be printed after the list of arguments when a help
argument (e.g. "--help"
/"-h"
) is found on the command line. A newline
will be appended automatically.
A string that is to be printed after the version information when a
version argument (e.g. "--version"
/"-V"
) is found on the command line.
A newline will be appended automatically.
A string that is to be printed after the version information when a
version argument (e.g. "--version"
/"-V"
) is found on the command line.
A newline will be appended automatically.
A string that is to be printed after the "Usage:" line but before the list
of arguments when a help argument (e.g. "--help"
/"-h"
) is found on the
command line. A newline will be appended automatically.
A string that is to be printed after the "Usage:" line but before the list
of arguments when a help argument (e.g. "--help"
/"-h"
) is found on the
command line. A newline will be appended automatically.
A string that is to be printed before version information when a version
argument (e.g. "--version"
/"-V"
) is found on the command line. A
newline will be appended automatically.
A string that is to be printed before version information when a version
argument (e.g. "--version"
/"-V"
) is found on the command line. A
newline will be appended automatically.
The name of the program using this parser. Used when a version argument
(e.g. "--version"
/"-V"
) is found on the command line.
The name of the program using this parser. Used when a version argument
(e.g. "--version"
/"-V"
) is found on the command line.
The version of the program using this parser. Used when a version
argument (e.g. "--version"
/"-V"
) is found on the command line.
The version of the program using this parser. Used when a version
argument (e.g. "--version"
/"-V"
) is found on the command line.
When true
, #parse
will call exit(0)
after printing help when a help
argument (e.g. "--help"
/"-h"
) is found on the command line.
When true
, #parse
will call exit(0)
after printing help when a help
argument (e.g. "--help"
/"-h"
) is found on the command line.
When true
, #parse
will call exit(0)
after printing version
information when a version argument (e.g. "--version"
/"-V"
) is found
on the command line.
When true
, #parse
will call exit(0)
after printing version
information when a version argument (e.g. "--version"
/"-V"
) is found
on the command line.
When true, a single dash -
will be and stored as a positional argument.
When true, a single dash -
will be and stored as a positional argument.
When non-nil, this will be used for the "Usage:" line instead of the generated one.
When non-nil, this will be used for the "Usage:" line instead of the generated one.
This will be called when a version argument (e.g. "--version"
/"-V"
) is
found on the command line.
This will be called when a version argument (e.g. "--version"
/"-V"
) is
found on the command line.
If an argument named name
was called, then this yields that argument to
the block. Otherwise this does nothing. This returns either the result
of the block, or nil if it wasn't called.
Looks up the argument named name
, casts it to a FloatArgument
. If the
argument was called, this yields it to the block, otherwise it does
nothing. If the argument is not a FloatArgument
, this raises an
exception. This returns either the result of the block, or nil if it
wasn't called.
Looks up the argument named name
, casts it to an IntArgument
. If the
argument was called, this yields it to the block, otherwise it does
nothing. If the argument is not an IntArgument
, this raises an
exception. This returns either the result of the block, or nil if it
wasn't called.
Looks up the argument named name
, casts it to a MultiFloatArgument
.
If the argument was called, this yields it to the block, otherwise it does
nothing. If the argument is not a MultiFloatArgument
, this raises an
exception. This returns either the result of the block, or nil if it
wasn't called.
Looks up the argument named name
, casts it to a MultiIntArgument
. If
the argument was called, this yields it to the block, otherwise it does
nothing. If the argument is not a MultiIntArgument
, this raises an
exception. This returns either the result of the block, or nil if it
wasn't called.
Looks up the argument named name
, casts it to an MultiStringArgument
.
If the argument was called, this yields it to the block, otherwise it does
nothing. If the argument is not an MultiStringArgument
, this raises an
exception. This returns either the result of the block, or nil if it
wasn't called.
Looks up the argument named name
, casts it to an StringArgument
. If
the argument was called, this yields it to the block, otherwise it does
nothing. If the argument is not an StringArgument
, this raises an
exception. This returns either the result of the block, or nil if it
wasn't called.
Looks up the argument named name
, casts it to an FlagArgument
, then
yields it to the block. If the argument is not an FlagArgument
, this
raises an exception. Returns the last value of the block.
Looks up the argument named name
, casts it to a FloatArgument
, then
yields it to the block. If the argument is not a FloatArgument
, this
raises an exception. Returns the last value of the block.
Looks up the argument named name
, casts it to an IntArgument
, then
yields it to the block. If the argument is not an IntArgument
, this
raises an exception. Returns the last value of the block.
Looks up the argument named name
, casts it to an MultiFlagArgument
,
then yields it to the block. If the argument is not an
MultiFlagArgument
, this raises an exception. Returns the last value of
the block.
Looks up the argument named name
, casts it to a MultiFloatArgument
,
then yields it to the block. If the argument is not a
MultiFloatArgument
, this raises an exception. Returns the last value of
the block.
Looks up the argument named name
, casts it to a MultiIntArgument
, then
yields it to the block. If the argument is not a MultiIntArgument
, this
raises an exception. Returns the last value of the block.
Looks up the argument named name
, casts it to an MultiStringArgument
,
then yields it to the block. If the argument is not an
MultiStringArgument
, this raises an exception. Returns the last value
of the block.
Looks up the argument named name
, casts it to an StringArgument
, then
yields it to the block. If the argument is not an StringArgument
, this
raises an exception. Returns the last value of the block.