class RemiLib::Args::CommandParser

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.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(progName : String, progVersion : String, *, helpName : String = "help", verName : String = "version", progBinName : String = PROGRAM_NAME) #

Creates a new CommandParser.


[View source]

Class Method Detail

def self.defaultHelpPrinter(parser : CommandParser) : Nil #

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.

[View source]
def self.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:

My Program v0.1.0

[View source]

Instance Method Detail

def [](cmd : String) : CmdHandler #

Returns the handler function associated with a command. If the command does not exist, this will raise a KeyError.


[View source]
def []?(cmd : String) : CmdHandler | Nil #

Returns the handler function associated with a command. If the command does not exist, this returns nil.


[View source]
def 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. 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?.


[View source]
def caseInsensitive=(caseInsensitive : Bool) #

When true, then command names are case-insensitive during parsing. Note that they are still case sensitive when referencing them in code.


[View source]
def caseInsensitive? : Bool #

When true, then command names are case-insensitive during parsing. Note that they are still case sensitive when referencing them in code.


[View source]
def detectHelp=(detectHelp : Bool) #

When true, the help command will be processed. Otherwise it is ignored.


[View source]
def detectHelp? : Bool #

When true, the help command will be processed. Otherwise it is ignored.


[View source]
def detectVer=(detectVer : Bool) #

When true, the version command will be processed. Otherwise it is ignored.


[View source]
def detectVer? : Bool #

When true, the version command will be processed. Otherwise it is ignored.


[View source]
def help(cmd : String) : String | Nil #

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?.


[View source]
def helpPrinter : CmdHelpPrinterFunc #

This will be called when a help command (e.g. ./myprogram help) is found on the command line.


[View source]
def helpPrinter=(helpPrinter : CmdHelpPrinterFunc) #

This will be called when a help command (e.g. ./myprogram help) is found on the command line.


[View source]
def names : Array(String) #

Returns all defined command names.

Note that the returned names are handled in a case-sensitive way internally, regardless of the value of #caseInsensitive?.


[View source]
def parse(theseArgs : Array(String)) : Nil #

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.


[View source]
def parse : Nil #

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.


[View source]
def postHelpText : String #

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.


[View source]
def 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. A newline will be both prepended and appended automatically.


[View source]
def postVerText : String #

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.


[View source]
def postVerText=(postVerText : String) #

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.


[View source]
def 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. Two newlines will appear between this text and the "Commands:" line.


[View source]
def 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. Two newlines will appear between this text and the "Commands:" line.


[View source]
def preVerText : String #

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.


[View source]
def preVerText=(preVerText : String) #

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.


[View source]
def progBinName : String #

The name of the binary using this CommandParser. This is used when printing the default #usageLine.


[View source]
def progBinName=(progBinName : String) #

The name of the binary using this CommandParser. This is used when printing the default #usageLine.


[View source]
def progName : String #

The name of the program using this parser. Used when a version command is found on the command line.


[View source]
def progName=(progName : String) #

The name of the program using this parser. Used when a version command is found on the command line.


[View source]
def progVersion : String #

The version of the program using this parser. Used when a version command is found on the command line.


[View source]
def progVersion=(progVersion : String) #

The version of the program using this parser. Used when a version command is found on the command line.


[View source]
def quitOnHelp=(quitOnHelp : Bool) #

When true, #parse will call exit(0) after printing help when a help command is found on the command line.


[View source]
def quitOnHelp? : Bool #

When true, #parse will call exit(0) after printing help when a help command is found on the command line.


[View source]
def quitOnVer=(quitOnVer : Bool) #

When true, #parse will call exit(0) after printing version information when a version command is found on the command line.


[View source]
def quitOnVer? : Bool #

When true, #parse will call exit(0) after printing version information when a version command is found on the command line.


[View source]
def 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.


[View source]
def 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.


[View source]
def verPrinter : CmdVerPrinterFunc #

This will be called when a version command (e.g. ./myprogram version) is found on the command line.


[View source]
def verPrinter=(verPrinter : CmdVerPrinterFunc) #

This will be called when a version command (e.g. ./myprogram version) is found on the command line.


[View source]