B0_std.CmdCommand lines.
Command line values specify the command line arguments given to tool spawns. Depending on the context this may represent either only tool arguments or the full command specification with the tool to spawn as the first argument.
See examples.
The type for command lines.
A command line is a list of command line arguments. The first argument usually denotes the tool or executable to invoke.
val is_empty : t -> boolis_empty cmd is true iff cmd is an empty list of arguments.
val empty : tempty is an empty list of arguments.
val arg : string -> targ a is the atomic argument a.
val int : int -> tint i is arg (string_of_int i).
val float : float -> tfloat f is arg (float_of_int f).
val list : ?slip:string -> string list -> tlist ?slip l is a command line from the list of arguments l. If slip is specified it is added on the command line before each element of l.
val of_list : ?slip:string -> ('a -> string) -> 'a list -> tof_list ?slip conv l is list ?slip (List.map conv l).
Tools are the first argument of commands.
type tool = Fpath.tThe type for command line tools.
A command line tool is represented by a file path according to the POSIX convention for exec(3):
Fpath.v "ocaml", it represents a program name to be looked up via a search procedure; for example in the PATH environment variable.Note. For portability one should not use the .exe suffix on Windows on tools. This should be handled transparently by tool_search procedures.
val tool : string -> ttool t is arg t, used for reading clarity.
get_tool is like find_tool but returns an english Error msg on None.
set_tool t cmd replaces cmd's first element with t. This is path t if cmd is empty.
The type for tool search functions.
These are functions that resolve and set the get_tool argument of commands to a concrete program executable. Or return an error message if the tool cannot be resolved. See Tool search for implementations.
val is_singleton : t -> boolis_singleton l is true iff l has a single argument.
val fold :
arg:(string -> 'a) ->
unstamp:('a -> 'a) ->
append:('a -> 'a -> 'a) ->
empty:'a ->
t ->
'afold ~arg ~unstamp ~append ~empty l folds over l's structure.
val iter_enc :
arg:('a -> string -> unit) ->
unstamp:('a -> unit) ->
append:('a -> unit) ->
empty:('a -> unit) ->
'a ->
t ->
unitval to_list : t -> string listto_list l converts l to a list of strings.
val to_string : t -> stringto_string l converts l to a string that can be passed to the command(3) POSIX system call.
val of_string : string -> (t, string) Stdlib.resultof_string s tokenizes s into a command line. The tokens are recognized according to the token production of the following grammar which should be mostly be compatible with POSIX shell tokenization.
white ::= ' ' | '\t' | '\n' | '\x0B' | '\x0C' | '\r' squot ::= '\'' dquot ::= '\"' bslash ::= '\\' tokens ::= white+ tokens | token tokens | ϵ token ::= ([^squot dquot white] | squoted | dquoted) token | ϵ squoted ::= squot [^squot]* squot dquoted ::= dquot (qchar | [^dquot])* dquot qchar ::= bslash (bslash | dquot | '$' | '`' | '\n')
qchar are substitued by the byte they escape except for '\n' which removes the backslash and newline from the byte stream. squoted and dquoted represent the bytes they enclose.
pp_shell formats a command as a multiline shell command that can be cut and pasted. Note. Currently this may overflow your boxes.
Stamps are not useful unless you are interested in memoizing tool invocations. A command stamp represents the part of the command line that influences a tool's output. By default arguments are part of the stamp however they can be selectively Cmd.unstamped to remove them from the stamp.
Unstamped arguments have no special semantics. As far as the command line is concerned they simply indicate that the argument value itself does not influence the outputs of the tool. Unstamped arguments do not appear in the command line stamp which can be used as a key to memoize tool spawns.
A typical example of unstamped arguments are file paths to inputs: it's often the file contents not the actual file path that determines the tool output; beware though that some tool use both the file path contents and the actual file path in their outputs (typically compilers which track source locations). See examples.
val to_stamp : t -> string listto_stamp l is the sequence of stamped arguments.
val to_list_and_stamp : t -> string list * string listto_list_and_stamp l is a l as a list of strings tuppled with its stamp: the sequence of stamped arguments.
let ls p = Cmd.(tool "ls" % "-a" % path p)
let tar archive dir =
Cmd.(tool "tar" % "-cvf" %% unstamp (path archive) %% path dir)
let opam cmd = Cmd.(tool "opam" % cmd)
let opam_install pkgs = Cmd.(opam "install" %% list pkgs)
let ocamlc ?(debug = false) file =
Cmd.(tool "ocamlc" % "-c" % if' debug (arg "-g") %% path file)
let ocamlopt ?(profile = false) ?(debug = false) incs file =
let profile = Cmd.(if' profile (arg "-p")) in
let debug = Cmd.(if' debug (arg "-g")) in
let incs = Cmd.(unstamp (paths ~slip:"-I" incs)) in
Cmd.(tool "ocamlopt" % "-c" %% debug %% profile %% incs %%
unstamp (path file))