class RemiLib::Args::CommandParser
- RemiLib::Args::CommandParser
- Reference
- Object
Overview
A CommandParser
is a way to associate command names (specified as the
first argument on the command line) with a function. A common use case for
this is a command line program that has multiple "modes", and each mode has
its own ArgParser
instance.
Similar to the ArgParser
class, the CommandParser
will automatically
handle "help" and "version" commands. This behavior can be customized.
Defined in:
remilib/args/commandparser.crConstructors
-
.new(progName : String, progVersion : String, *, helpName : String = "help", verName : String = "version", progBinName : String = PROGRAM_NAME)
Creates a new
CommandParser
.
Class Method Summary
-
.defaultHelpPrinter(parser : CommandParser) : Nil
The default printer for the help command.
-
.defaultVerPrinter(parser : CommandParser) : Nil
The default printer for the version command The default output looks something like this (depending on the values of
#preVerText
and#postVerText
:
Instance Method Summary
-
#[](cmd : String) : CmdHandler
Returns the handler function associated with a command.
-
#[]?(cmd : String) : CmdHandler | Nil
Returns the handler function associated with a command.
-
#add(cmd : String, fn : CmdHandler, *, help : String | Nil = nil) : Nil
Associates a
CmdHandler
function with a name, thereby adding a new command to this instance. -
#caseInsensitive=(caseInsensitive : Bool)
When
true
, then command names are case-insensitive during parsing. -
#caseInsensitive? : Bool
When
true
, then command names are case-insensitive during parsing. -
#detectHelp=(detectHelp : Bool)
When
true
, the help command will be processed. -
#detectHelp? : Bool
When
true
, the help command will be processed. -
#detectVer=(detectVer : Bool)
When
true
, the version command will be processed. -
#detectVer? : Bool
When
true
, the version command will be processed. -
#help(cmd : String) : String | Nil
Looks up the help string, if any, associated with cmd.
-
#helpPrinter : CmdHelpPrinterFunc
This will be called when a help command (e.g.
-
#helpPrinter=(helpPrinter : CmdHelpPrinterFunc)
This will be called when a help command (e.g.
-
#names : Array(String)
Returns all defined command names.
-
#parse(theseArgs : Array(String)) : Nil
Parses the given set of arguments.
-
#parse : Nil
Parses the default set of arguments, as stored in the global
ARGV
. -
#postHelpText : String
A string that is to be printed after the list of arguments when a help command is found on the command line.
-
#postHelpText=(postHelpText : String)
A string that is to be printed after the list of arguments when a help command is found on the command line.
-
#postVerText : String
A string that is to be printed after the version information when a version command is found on the command line.
-
#postVerText=(postVerText : String)
A string that is to be printed after the version information when a version command is found on the command line.
-
#preHelpText : String
A string that is to be printed after the "Usage:" line but before the list of arguments when a help command is found on the command line.
-
#preHelpText=(preHelpText : String)
A string that is to be printed after the "Usage:" line but before the list of arguments when a help command is found on the command line.
-
#preVerText : String
A string that is to be printed before version information when a version command is found on the command line.
-
#preVerText=(preVerText : String)
A string that is to be printed before version information when a version command is found on the command line.
-
#progBinName : String
The name of the binary using this
CommandParser
. -
#progBinName=(progBinName : String)
The name of the binary using this
CommandParser
. -
#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 command is found on the command line. -
#quitOnHelp? : Bool
When
true
,#parse
will callexit(0)
after printing help when a help command is found on the command line. -
#quitOnVer=(quitOnVer : Bool)
When
true
,#parse
will callexit(0)
after printing version information when a version command is found on the command line. -
#quitOnVer? : Bool
When
true
,#parse
will callexit(0)
after printing version information when a version command is found on the command line. -
#usageLine : String | Nil
When non-nil, this will be used for the "Usage:" line instead of the generated one when a help command is called.
-
#usageLine=(usageLine : String | Nil)
When non-nil, this will be used for the "Usage:" line instead of the generated one when a help command is called.
-
#verPrinter : CmdVerPrinterFunc
This will be called when a version command (e.g.
-
#verPrinter=(verPrinter : CmdVerPrinterFunc)
This will be called when a version command (e.g.
Constructor Detail
Creates a new CommandParser
.
Class Method Detail
The default printer for the help command.
The default output looks something like this (depending on the values of
#preHelpText
and #postHelpText
:
Usage: /path/to/binary <command> [options]
Commands:
* help : Display this help.
* version : Display version information.
The default printer for the version command 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 handler function associated with a command. If the command
does not exist, this will raise a KeyError
.
Returns the handler function associated with a command. If the command
does not exist, this returns nil
.
Associates a CmdHandler
function with a name, thereby adding a new
command to this instance. This will overwrite any commands with the same
name. If help is provided, then it will be saved as the help text
associated with this command.
Note that cmd is added in a case-sensitive way internally, regardless of
the value of #caseInsensitive?
.
When true
, then command names are case-insensitive during parsing. Note
that they are still case sensitive when referencing them in code.
When true
, then command names are case-insensitive during parsing. Note
that they are still case sensitive when referencing them in code.
When true
, the help command will be processed. Otherwise it is ignored.
When true
, the version command will be processed. Otherwise it is
ignored.
Looks up the help string, if any, associated with cmd.
Note that cmd is added in a case-sensitive way internally, regardless of
the value of #caseInsensitive?
.
This will be called when a help command (e.g. ./myprogram help
) is
found on the command line.
This will be called when a help command (e.g. ./myprogram help
) is
found on the command line.
Returns all defined command names.
Note that the returned names are handled in a case-sensitive way
internally, regardless of the value of #caseInsensitive?
.
Parses the given set of arguments. The command name must appear first,
while all remaining arguments are passed verbatim to the associated
CmdHandler
function.
If no command is provided, then a NoCommandError
is raised. If the
command is not known, then an UnknownCommandError
is raised.
Parses the default set of arguments, as stored in the global ARGV
. The
command name must appear first, while all remaining arguments are passed
verbatim to the associated CmdHandler
function.
If no command is provided, then a NoCommandError
is raised. If the
command is not known, then an UnknownCommandError
is raised.
A string that is to be printed after the list of arguments when a help command is found on the command line. A newline will be both prepended and appended automatically.
A string that is to be printed after the list of arguments when a help command is found on the command line. A newline will be both prepended and appended automatically.
A string that is to be printed after the version information when a version command 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 command 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 command is found on the command line. Two newlines will appear between this text and the "Commands:" line.
A string that is to be printed after the "Usage:" line but before the list of arguments when a help command is found on the command line. Two newlines will appear between this text and the "Commands:" line.
A string that is to be printed before version information when a version command 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 command is found on the command line. A newline will be appended automatically.
The name of the binary using this CommandParser
. This is used when
printing the default #usageLine
.
The name of the binary using this CommandParser
. This is used when
printing the default #usageLine
.
The name of the program using this parser. Used when a version command is found on the command line.
The name of the program using this parser. Used when a version command is found on the command line.
The version of the program using this parser. Used when a version command is found on the command line.
The version of the program using this parser. Used when a version command is found on the command line.
When true
, #parse
will call exit(0)
after printing help when a help
command is found on the command line.
When true
, #parse
will call exit(0)
after printing help when a help
command is found on the command line.
When true
, #parse
will call exit(0)
after printing version
information when a version command is found on the command line.
When true
, #parse
will call exit(0)
after printing version
information when a version command is found on the command line.
When non-nil, this will be used for the "Usage:" line instead of the generated one when a help command is called.
When non-nil, this will be used for the "Usage:" line instead of the generated one when a help command is called.
This will be called when a version command (e.g. ./myprogram version
) is
found on the command line.
This will be called when a version command (e.g. ./myprogram version
) is
found on the command line.