.TH "Stdlib.Arg" 3o 2023-09-18 OCamldoc "OCaml library" .SH NAME Stdlib.Arg \- no description .SH Module Module Stdlib.Arg .SH Documentation .sp Module .BI "Arg" : .B (module Stdlib__Arg) .sp .sp .sp .sp .I type spec = | Unit .B of .B (unit -> unit) .I " " (* Call the function with unit argument *) | Bool .B of .B (bool -> unit) .I " " (* Call the function with a bool argument *) | Set .B of .B bool ref .I " " (* Set the reference to true *) | Clear .B of .B bool ref .I " " (* Set the reference to false *) | String .B of .B (string -> unit) .I " " (* Call the function with a string argument *) | Set_string .B of .B string ref .I " " (* Set the reference to the string argument *) | Int .B of .B (int -> unit) .I " " (* Call the function with an int argument *) | Set_int .B of .B int ref .I " " (* Set the reference to the int argument *) | Float .B of .B (float -> unit) .I " " (* Call the function with a float argument *) | Set_float .B of .B float ref .I " " (* Set the reference to the float argument *) | Tuple .B of .B spec list .I " " (* Take several arguments according to the spec list *) | Symbol .B of .B string list * (string -> unit) .I " " (* Take one of the symbols as argument and call the function with the symbol *) | Rest .B of .B (string -> unit) .I " " (* Stop interpreting keywords and call the function with each remaining argument *) | Rest_all .B of .B (string list -> unit) .I " " (* Stop interpreting keywords and call the function with all remaining arguments *) | Expand .B of .B (string -> string array) .I " " (* If the remaining arguments to process are of the form .ft B ["\-foo"; "arg"] @ rest .ft R where "foo" is registered as .ft B Expand f .ft R , then the arguments .ft B f "arg" @ rest .ft R are processed\&. Only allowed in .ft B parse_and_expand_argv_dynamic .ft R \&. *) .sp The concrete type describing the behavior associated with a keyword\&. .sp .I type key = .B string .sp .sp .I type doc = .B string .sp .sp .I type usage_msg = .B string .sp .sp .I type anon_fun = .B string -> unit .sp .sp .I val parse : .B (key * spec * doc) list -> anon_fun -> usage_msg -> unit .sp .ft B Arg\&.parse speclist anon_fun usage_msg .ft R parses the command line\&. .ft B speclist .ft R is a list of triples .ft B (key, spec, doc) .ft R \&. .ft B key .ft R is the option keyword, it must start with a .ft B \&'\-\&' .ft R character\&. .ft B spec .ft R gives the option type and the function to call when this option is found on the command line\&. .ft B doc .ft R is a one\-line description of this option\&. .ft B anon_fun .ft R is called on anonymous arguments\&. The functions in .ft B spec .ft R and .ft B anon_fun .ft R are called in the same order as their arguments appear on the command line\&. .sp If an error occurs, .ft B Arg\&.parse .ft R exits the program, after printing to standard error an error message as follows: .sp \- The reason for the error: unknown option, invalid or missing argument, etc\&. .sp \- .ft B usage_msg .ft R .sp \- The list of options, each followed by the corresponding .ft B doc .ft R string\&. Beware: options that have an empty .ft B doc .ft R string will not be included in the list\&. For the user to be able to specify anonymous arguments starting with a .ft B \- .ft R , include for example .ft B ("\-", String anon_fun, doc) .ft R in .ft B speclist .ft R \&. .sp By default, .ft B parse .ft R recognizes two unit options, .ft B \-help .ft R and .ft B \-\-help .ft R , which will print to standard output .ft B usage_msg .ft R and the list of options, and exit the program\&. You can override this behaviour by specifying your own .ft B \-help .ft R and .ft B \-\-help .ft R options in .ft B speclist .ft R \&. .sp .I val parse_dynamic : .B (key * spec * doc) list ref -> .B anon_fun -> usage_msg -> unit .sp Same as .ft B Arg\&.parse .ft R , except that the .ft B speclist .ft R argument is a reference and may be updated during the parsing\&. A typical use for this feature is to parse command lines of the form: .sp \- command subcommand .ft B options .ft R where the list of options depends on the value of the subcommand argument\&. .sp .B "Since" 4.01.0 .sp .I val parse_argv : .B ?current:int ref -> .B string array -> .B (key * spec * doc) list -> anon_fun -> usage_msg -> unit .sp .ft B Arg\&.parse_argv ~current args speclist anon_fun usage_msg .ft R parses the array .ft B args .ft R as if it were the command line\&. It uses and updates the value of .ft B ~current .ft R (if given), or .ft B Arg\&.current .ft R \&. You must set it before calling .ft B parse_argv .ft R \&. The initial value of .ft B current .ft R is the index of the program name (argument 0) in the array\&. If an error occurs, .ft B Arg\&.parse_argv .ft R raises .ft B Arg\&.Bad .ft R with the error message as argument\&. If option .ft B \-help .ft R or .ft B \-\-help .ft R is given, .ft B Arg\&.parse_argv .ft R raises .ft B Arg\&.Help .ft R with the help message as argument\&. .sp .I val parse_argv_dynamic : .B ?current:int ref -> .B string array -> .B (key * spec * doc) list ref -> .B anon_fun -> string -> unit .sp Same as .ft B Arg\&.parse_argv .ft R , except that the .ft B speclist .ft R argument is a reference and may be updated during the parsing\&. See .ft B Arg\&.parse_dynamic .ft R \&. .sp .B "Since" 4.01.0 .sp .I val parse_and_expand_argv_dynamic : .B int ref -> .B string array ref -> .B (key * spec * doc) list ref -> .B anon_fun -> string -> unit .sp Same as .ft B Arg\&.parse_argv_dynamic .ft R , except that the .ft B argv .ft R argument is a reference and may be updated during the parsing of .ft B Expand .ft R arguments\&. See .ft B Arg\&.parse_argv_dynamic .ft R \&. .sp .B "Since" 4.05.0 .sp .I val parse_expand : .B (key * spec * doc) list -> anon_fun -> usage_msg -> unit .sp Same as .ft B Arg\&.parse .ft R , except that the .ft B Expand .ft R arguments are allowed and the .ft B Arg\&.current .ft R reference is not updated\&. .sp .B "Since" 4.05.0 .sp .I exception Help .B of .B string .sp Raised by .ft B Arg\&.parse_argv .ft R when the user asks for help\&. .sp .I exception Bad .B of .B string .sp Functions in .ft B spec .ft R or .ft B anon_fun .ft R can raise .ft B Arg\&.Bad .ft R with an error message to reject invalid arguments\&. .ft B Arg\&.Bad .ft R is also raised by .ft B Arg\&.parse_argv .ft R in case of an error\&. .sp .I val usage : .B (key * spec * doc) list -> usage_msg -> unit .sp .ft B Arg\&.usage speclist usage_msg .ft R prints to standard error an error message that includes the list of valid options\&. This is the same message that .ft B Arg\&.parse .ft R prints in case of error\&. .ft B speclist .ft R and .ft B usage_msg .ft R are the same as for .ft B Arg\&.parse .ft R \&. .sp .I val usage_string : .B (key * spec * doc) list -> usage_msg -> string .sp Returns the message that would have been printed by .ft B Arg\&.usage .ft R , if provided with the same parameters\&. .sp .I val align : .B ?limit:int -> .B (key * spec * doc) list -> (key * spec * doc) list .sp Align the documentation strings by inserting spaces at the first alignment separator (tab or, if tab is not found, space), according to the length of the keyword\&. Use a alignment separator as the first character in a doc string if you want to align the whole string\&. The doc strings corresponding to .ft B Symbol .ft R arguments are aligned on the next line\&. .sp .I val current : .B int ref .sp Position (in .ft B Sys\&.argv .ft R ) of the argument being processed\&. You can change this value, e\&.g\&. to force .ft B Arg\&.parse .ft R to skip some arguments\&. .ft B Arg\&.parse .ft R uses the initial value of .ft B Arg\&.current .ft R as the index of argument 0 (the program name) and starts parsing arguments at the next element\&. .sp .I val read_arg : .B string -> string array .sp .ft B Arg\&.read_arg file .ft R reads newline\-terminated command line arguments from file .ft B file .ft R \&. .sp .B "Since" 4.05.0 .sp .I val read_arg0 : .B string -> string array .sp Identical to .ft B Arg\&.read_arg .ft R but assumes null character terminated command line arguments\&. .sp .B "Since" 4.05.0 .sp .I val write_arg : .B string -> string array -> unit .sp .ft B Arg\&.write_arg file args .ft R writes the arguments .ft B args .ft R newline\-terminated into the file .ft B file .ft R \&. If the any of the arguments in .ft B args .ft R contains a newline, use .ft B Arg\&.write_arg0 .ft R instead\&. .sp .B "Since" 4.05.0 .sp .I val write_arg0 : .B string -> string array -> unit .sp Identical to .ft B Arg\&.write_arg .ft R but uses the null character for terminator instead of newline\&. .sp .B "Since" 4.05.0 .sp