module Kwapp:sig..end
Major modules:
Kwapp.Valid: Validators for options and arguments.Kwapp.Option: Defining command-line options (switches).Kwapp.Argv: Defining command-line arguments.Kwapp.Env: Defining environment variables.Kwapp.Version: Defining and displaying version information.Kwapp.Help: Displaying help messages.Kwapp.Messages: Defining verbose, warning, and error messages.
See the Tutorial below.
Author(s): Keith Waclena
module Valid: Kwvalid
See Kwapp.Messages.hdl for a top-level exception handler that converts
all of these exceptions into fatal errors with reasonable error messages.
For example, if you ask for the Kwapp.value of switch "-x", but have
never defined a switch "-x", then the Kwapp.Undefined exception will
be raised. Kwapp.Messages.hdl converts these exceptions into embarassing
fatal "DRYROT" errors.
exception Nameless
exception BadSwitch of string * string
string is the switch name
and the second is an explanation of the problem.exception Dupe of string * string
string
is the name, and the second is either "switch" or "arg".exception Undefined of string
Kwapp.value) a string value for an
undefined switch. The string is the switch name.exception Noarg of string
Kwapp.value) a string value for a Boolean
switch. The string is the switch name.exception ReqAfter of string
exception BadArgv of string
string is the name used.Kwapp.Messages.hdl, so if you wrap that around your
main function, you will get a clean termination of your program with a nice
error message. However, you may want to catch some of these manually in some
cases.exception Unknown of string
string is the switch name.exception Arg of string
string is the switch name.exception Missing of string list
string's are the required switch names.exception Invalid of string * string * string * string
string is the sort of thing that was invalid (probably either
"switch" or "argument"), the second is the thing's name, the
third is the expected "type" of the thing, and the fourth is the
given bad value.exception Mutual of string list list
string list
is a class of options corresponding to the mex attribute of the switch.exception TooMany of string
exception WrongNumber of int
n means n too few were given; positive n means n too many.exception NoArgv of string
exception NoAction of string list
Kwapp.dispatch invoked with no default and no dispatch switch or subcommand on command line
The parameter is a list of dispatchable switches and subcommands.
exception NYI of string * string option
The first parameter is the option, the second some explanation.
module Parsedopt:sig..end
Kwapp.getopt are parsedopts.
typeparsedopts =string list Parsedopt.t
Parsedopt map. Each key is
mapped to a string option representing the value of the option's
argument (if any).module Option:sig..end
module Argv:sig..end
module Env:sig..end
type interface = {
|
opts : |
|
argv : |
|
envs : |
val empty : interfaceval unmash : ?unmash_gnu:bool -> string list -> string list
E.g., ["-z";"foo";"-abc";"-x";"bar";"baz"] becomes ["-z";"foo";"-a";"-b";"-c";"-x";"bar";"baz"].
Returns list of unmashed switches
unmash_gnu : unmash GNU --style args too (default: false)val ungnu : string list -> string list["--abc=12"] becomes ["--abc"; "12"]["-z"; "--abc"; "69"; "--123"; "-x"]switches : list of switches eg ["-z"; "--abc=69"; "--123"; "-x"]val argvlist : unit -> string listSys.argv as a list, less Sys.argv.(0)val getopt : ?opt:string list Parsedopt.t ->
?endopts:string option ->
interface -> string list -> parsedopts * string listgetopt ?opt interface argv: parse command line options in argv
as per interface, extract command-line arguments, and return both.
Also checks the interface for programmer errors.
Typical usage is something like:
Kwapp.(argvlist () |> unmash |> ungnu |> getopt interface)
Unknown when an unknown option is detectedArg when an option that takes an argument is given without oneopt : initial parsed options (default: Parsedopt.empty)endopts : option that ends options, if any (default: Some "--")interface : option definitionsval has : string -> parsedopts -> boolhas sw m: return true if getopt-parsed map m has switch sw.val value : ?def:string ->
?interface:interface -> string -> parsedopts -> stringvalue ?def ?interface sw m: return value of switch sw from the parsed options m
or, if not given on command line, its default from interface.Noarg for requesting a string value for a Boolean switchUndefined for a switch that isn't defined in interface.optsBadSwitch when there's no default value for a switch that wasn't given on the command lineFailure when requesting a single value from a repeatable optiondef : default value for a switch not given on the command line, overriding any default in the interfaceinterface : the interfaceval values : ?def:string list ->
?interface:interface -> string -> parsedopts -> string listvalues ?def ?interface sw m: return all values of switch sw from the parsed options m
or, if not given on command line, its default from interface.Noarg for requesting a string values for a Boolean switchUndefined for a switch that isn't defined in interface.optsdef : default values for a switch not given on the command line, overriding any default in the interfaceinterface : the interfaceval argument : ?def:string -> interface -> string -> string list -> stringargument ?def interface name argv: return value of a non-repeating command-line argument named name, as defined in
interface, from argv.NoArgv if requested arg wasn't provided on command line and no def providedBadArgv if named arg isn't defined in interfacedef : default returned for a requested arg that wasn't provided on command lineinterface : the interfaceargv : the runtime argv, exclusive of options, e.g. as returned by Kwapp.getoptval arguments : ?def:string list -> interface -> string -> string list -> string listarguments ?def interface name argv: return value of the repeating command-line argument named name, as defined in
interface, from argv, as a list.NoArgv if requested arg wasn't provided on command line and no def providedBadArgv if named arg isn't defined as the repeating arg in interfacedef : default returned if the repeating arg wasn't provided on command lineinterface : the interfaceargv : the runtime argv, exclusive of options, e.g. as returned by Kwapp.getopttype dispatchmode =
| |
Switch of |
| |
Sub of |
| |
Default |
The Kwapp.dispatch function allows you to dispatch to an action based
on either a command-line option (Switch) or a subcommand (Sub).
type('a, 'b)action ='a ->
interface option ->
interface -> dispatchmode -> parsedopts * string list -> 'b
Actions are functions that take:
Kwapp.interface option, designating a subcommand interface specific to an actionKwapp.interface of the appKwapp.dispatch, the switch or subcommand that invoked the actionparsedopt * parsed argv), as returned by Kwapp.getopttype('a, 'b)dispatch =dispatchmode * ('a, 'b) action
Each dispatch-pair combines a mode and an action.
val dispatch : ?def:('a ->
'b option ->
interface ->
dispatchmode -> parsedopts * string list -> 'c) ->
'a ->
interface ->
(dispatchmode *
('a ->
interface option ->
interface ->
dispatchmode -> parsedopts * string list -> 'c))
list -> parsedopts * string list -> 'cdispatch ?def ?data interface actions (m,argv): execute default
function def unless one of the actions is requested, passing data to the action.
Actions are typically meta-functions of the application, e.g. help, version information, etc.
Can be used to select amongst disjoint subcommands via distinguishing option or arg.
Raises
Failure if actions contains DefaultNoAction if def = None and no action implied by parsed command linedef : default action invoked if no matches in actionsdata : arbitrary dataactions : list of dispatchable action's(m,argv) : argv : parsed command linem : parsed options
If you write an action a, you can wrap it with one or both of these
combinators to perform appropriate option and argv checks; e.g.
with_check_global (with_check_sub a)val with_check_global : ('a -> 'b -> interface -> 'c -> parsedopts * string list -> 'd) ->
'a -> 'b -> interface -> 'c -> parsedopts * string list -> 'dwith_check_global f interface global disp (m,argv): add a global option check to an action.
If you have an action a, then with_check_global a is that
action preceded by a global option check, equivalent to starting
your action with Option.check global.opts (m,argv).
Raises
Missing if any required options were missingInvalid if option argument or command-line arg has an invalid typeReqAfter for a required option after an optional oneMutual if mutually exclusive arguments were providedf : the actiondata : arbitrary datainterface : the dispatch-level interfaceglobal : the global interfacedisp : the dispatch mode(m,argv) : argv : actual argv, typically from Kwapp.getoptm : parsed options, typically from Kwapp.getoptval with_check_sub : ('a ->
interface option ->
interface -> 'b -> parsedopts * string list -> unit) ->
'a ->
interface option ->
interface -> 'b -> parsedopts * string list -> unitwith_check_sub f interface global disp (m,argv): add a subcommand option and argv check to an action.
If you have an action a, then with_check_sub a is that action
preceded by a subcommand option and argv check, equivalent to
starting your action with:
Option.check interface.opts (m,argv) |> ignore;
Argv.check interface.argv (m,argv)]Invalid if option argument or command-line arg has an invalid typeWrongNumber if the wrong number of arguments was providedf : the actiondata : arbitrary datainterface : the dispatch-level interfaceglobal : the global interfacedisp : the dispatch mode(m,argv) : argv : actual argv, typically from Kwapp.getoptm : parsed options, typically from Kwapp.getoptmodule Version:sig..end
module Help:sig..end
Functions for printing simple messages from command-line applications.
module type MYSELF =sig..end
module type VERBOSITY =sig..end
MYSELF modules.module Argv0:sig..end
Sys.argv.(0).
module Anonymous:sig..end
Kwapp.Messages functions.
VERBOSITY modules.
The verbosity level of your program is a global value and can be
either mutable or immutable. An immutable VERBOSITY module raises a
Failure exception if you call its set function.
module Ref:
VERBOSITY via a global ref.
module Environment:
VERBOSITY via an environment variable read when the program
starts.
module Quiet:sig..end
VERBOSITY is always 0.
module Verbose:sig..end
VERBOSITY is always 1.
module type MESSAGES =sig..end
Messages functor.
module Messages:
type opts = { c : int; m : string; z : bool }
let _ = {
x = value "-x" m |> int_of_string;
y = value "-y" m |> String.uppercase;
z = has "-z" m;
}