package async

  1. Overview
  2. Docs

Async.Command is Core.Command with additional Async functions.

include module type of struct include Core.Command end
include module type of Core_kernel.Command with module Shape := Core_kernel.Command.Shape with module Deprecated := Core_kernel.Command.Deprecated
module Auto_complete = Core.Command.Auto_complete
module Arg_type = Core.Command.Arg_type

Argument types.

module Flag = Core.Command.Flag

Command-line flag specifications.

module Anons = Core.Command.Anons

Anonymous command-line argument specification.

module Param = Core.Command.Param

Command-line parameter specification.

module Let_syntax = Core.Command.Let_syntax
module Spec = Core.Command.Spec

The old interface for command-line specifications -- Do Not Use.

Commands which can be combined into a hierarchy of subcommands.

type ('main, 'result) basic_spec_command = summary:Base.String.t -> ?readme:(Base.Unit.t -> Base.String.t) -> ('main, Base.Unit.t -> 'result) Spec.t -> 'main -> t
val basic_spec : ('main, Base.Unit.t) basic_spec_command

basic_spec ~summary ?readme spec main is a basic command that executes a function main which is passed parameters parsed from the command line according to spec. summary is to contain a short one-line description of its behavior. readme is to contain any longer description of its behavior that will go on that command's help screen.

type 'result basic_command = summary:Base.String.t -> ?readme:(Base.Unit.t -> Base.String.t) -> (Base.Unit.t -> 'result) Param.t -> t

Same general behavior as basic_spec, but takes a command line specification built up using Params instead of Spec.

val group : summary:Base.String.t -> ?readme:(Base.Unit.t -> Base.String.t) -> ?preserve_subcommand_order:Base.Unit.t -> ?body:(path:Base.String.t Base.List.t -> Base.Unit.t) -> (Base.String.t * t) Base.List.t -> t

group ~summary subcommand_alist is a compound command with named subcommands, as found in subcommand_alist. summary is to contain a short one-line description of the command group. readme is to contain any longer description of its behavior that will go on that command's help screen.

NOTE: subcommand names containing underscores will be rejected; use dashes instead.

body is called when no additional arguments are passed -- in particular, when no subcommand is passed. Its path argument is the subcommand path by which the group command was reached.

val lazy_group : summary:Base.String.t -> ?readme:(Base.Unit.t -> Base.String.t) -> ?preserve_subcommand_order:Base.Unit.t -> ?body:(path:Base.String.t Base.List.t -> Base.Unit.t) -> (Base.String.t * t) Base.List.t Core_kernel.Lazy.t -> t

lazy_group is the same as group, except that the list of subcommands may be generated lazily.

val exec : summary:Base.String.t -> ?readme:(Base.Unit.t -> Base.String.t) -> ?child_subcommand:Base.String.t Base.List.t -> path_to_exe: [ `Absolute of Base.String.t | `Relative_to_argv0 of Base.String.t | `Relative_to_me of Base.String.t ] -> Base.Unit.t -> t

exec ~summary ~path_to_exe runs exec on the executable at path_to_exe. If path_to_exe is `Absolute path then path is executed without any further qualification. If it is `Relative_to_me path then Filename.dirname Sys.executable_name ^ "/" ^ path is executed instead. All of the usual caveats about Sys.executable_name apply: specifically, it may only return an absolute path in Linux. On other operating systems it will return Sys.argv.(0). If it is `Relative_to_argv0 path then Sys.argv.(0) ^ "/" ^ path is executed.

The child_subcommand argument allows referencing a subcommand one or more levels below the top-level of the child executable. It should not be used to pass flags or anonymous arguments to the child.

Care has been taken to support nesting multiple executables built with Command. In particular, recursive help and autocompletion should work as expected.

NOTE: Non-Command executables can be used with this function but will still be executed when help -recursive is called or autocompletion is attempted (despite the fact that neither will be particularly helpful in this case). This means that if you have a shell script called "" that takes no arguments and reboots everything no matter how it is called, you shouldn't use it with exec.

Additionally, no loop detection is attempted, so if you nest an executable within itself, help -recursive and autocompletion will hang forever (although actually running the subcommand will work).

val of_lazy : t Core_kernel.Lazy.t -> t

of_lazy thunk constructs a lazy command that is forced only when necessary to run it or extract its shape.

val summary : t -> Base.String.t

Extracts the summary string for a command.

val exit : Base.Int.t -> _

call this instead of Core.exit if in command-related code that you want to run in tests. For example, in the body of Command.Param.no_arg_abort

val run : ?verbose_on_parse_error:bool -> ?version:string -> ?build_info:string -> ?argv:string list -> ?extend:(string list -> string list) -> ?when_parsing_succeeds:(unit -> unit) -> t -> unit

Runs a command against Sys.argv, or argv if it is specified.

extend can be used to add extra command line arguments to basic subcommands of the command. extend will be passed the (fully expanded) path to a command, and its output will be appended to the list of arguments being processed. For example, suppose a program like this is compiled into exe:

let bar = Command.basic ___
let foo = ~summary:___ ["bar", bar]
let main = ~summary:___ ["foo", foo]
let () = ~extend:(fun _ -> ["-baz"]) main

Then if a user ran exe f b, extend would be passed ["foo"; "bar"] and "-baz" would be appended to the command line for processing by bar. This can be used to add a default flags section to a user config file.

verbose_on_parse_error controls whether to print a line suggesting the user try the "-help" flag when an exception is raised while parsing the arguments. By default it is true.

when_parsing_succeeds is invoked after argument parsing has completed successfully, but before the main function of the associated command has run. One use-case is for performing logging when a command is being invoked, where there's no reason to log incorrect invocations or -help calls.

module Path = Core.Command.Path
module Shape = Core.Command.Shape
val shape : t -> Shape.t

Exposes the shape of a command.

module Deprecated = Core.Command.Deprecated

Deprecated should be used only by Deprecated_command. At some point it will go away.

type 'a with_options = ?extract_exn:bool -> 'a

async is like Core.Command.basic, except that the main function it expects returns unit Deferred.t, instead of unit. async will also start the Async scheduler before main is run, and will stop the scheduler when main returns.

async also handles top-level exceptions by wrapping the user-supplied function in a Monitor.try_with. If an exception is raised, it will print it to stderr and call shutdown 1. The extract_exn argument is passed along to Monitor.try_with; by default it is false.

async_or_error is like async, except that the main function it expects may return an error, in which case it prints out the error message and shuts down with exit code 1.

Staged functions allow the main function to be separated into two stages. The first part is guaranteed to run before the Async scheduler is started, and the second part will run after the scheduler is started. This is useful if the main function runs code that relies on the fact that threads have not been created yet (e.g., Daemon.daemonize).

As an example:

let main () =
  assert (not (Scheduler.is_running ()));
  stage (fun `Scheduler_started ->
    assert (Scheduler.is_running ());
type 'r staged = ([ `Scheduler_started ] -> 'r) Core.Staged.t
module Staged : sig ... end

To create an Arg_type.t that uses auto-completion and uses Async to compute the possible completions, one should use

Arg_type.create ~complete of_string

where complete wraps its Async operations in Thread_safe.block_on_async. With this, the complete function is only called when the executable is auto-completing, not for ordinary execution. This improves performance, and also means that the Async scheduler isn't started for ordinary execution of the command, which makes it possible for the command to daemonize (which requires the scheduler to not have been started).


Innovation. Community. Security.