Utils.SubargSourceThis module provides an extension of the standard Arg library with support for sub-commands. It allows defining multiple commands, each with their own set of options and arguments, and parsing command-line arguments to select and execute the appropriate command.
Note. This module is designed to work with the Arg module from the standard library. It extends its functionality to support complex command line interfaces with multiple sub-commands.
@see Arg (standard library) for the base argument parsing functionality.
type command = {name : string;The name of the command.
*)help : Stdlib.Arg.usage_msg;A short help string describing what the command does.
*)specs : spec list;The list of options and arguments accepted by this command.
*)enter : unit -> unit;A function called when this command is selected.
*)anon : (string -> unit) option;A function to handle anonymous (positional) arguments.
*)commit : unit -> unit;A function called after successful parsing of the command.
*)abort : unit -> unit;A function called when parsing fails.
*)}val command :
string ->
Stdlib.Arg.usage_msg ->
?enter:(unit -> unit) ->
?anon:Stdlib.Arg.anon_fun ->
?commit:(unit -> unit) ->
?abort:(unit -> unit) ->
spec list ->
commandcommand name help ?enter ?anon ?commit ?abort specs creates a new command with the given name and help string. The command accepts the options defined in specs.
usage_string specs commands errmsg returns a complete usage string for the command set. It includes the global options and the sub-command list.
usage speclist commands errmsg prints a complete usage message to standard error and exits with code 2.
val parse_argv :
?current:int Stdlib.ref ->
string array ->
spec list ->
command list ->
?default:string ->
?warn_default:(unit -> unit) ->
?no_subcommand:(unit -> unit) ->
Stdlib.Arg.anon_fun ->
Stdlib.Arg.usage_msg ->
unitparse_argv ?current argv specs commands ?default ?warn_default ?no_subcommand anon msg parses command-line arguments and executes the specified command.
The function selects the appropriate command based on the first argument (which must be one of the command names), then parses the remaining arguments according to that command's specifications.
val parse :
spec list ->
command list ->
?default:string ->
?warn_default:(unit -> unit) ->
?no_subcommand:(unit -> unit) ->
Stdlib.Arg.anon_fun ->
Stdlib.Arg.usage_msg ->
unitparse specs commands ?default ?warn_default ?no_subcommand anon usage parses command-line arguments and executes the specified command, exiting cleanly on error.
This is a wrapper around parse_argv that handles exit codes and exceptions.