Module Cmdlang_stdlib_runner.Arg_state

Internal representation for cmdlang arg expressions used during parsing.

This is a projection of Cmdlang.Ast.Arg.t where we added mutable variables to collect and store the intermediate results of parsing the command line arguments during the parsing phase of the execution.

To give a concrete example, let's look at the Flag construct. In the ast, the type is:

| Flag :
     { names : string Ast.Nonempty_list.t
     ; doc : string
     }
     -> bool t

Note how, in this intermediate representation we added a new mutable field as a place to collect and store the value for that flag: var : bool ref:

| Flag :
     { names : string Ast.Nonempty_list.t
     ; doc : string
     ; var : bool ref (* <== Added mutable field *)
     }
     -> bool t

This var is where the parsing engine will store the value read from the command line. Then the rest of the execution chain will be able to read the value from there while going through this runtime ast for evaluation, after the parsing is complete.

type 'a t =
  1. | Return : 'a -> 'a t
  2. | Map : {
    1. x : 'a t;
    2. f : 'a -> 'b;
    } -> 'b t
  3. | Both : 'a t * 'b t -> ('a * 'b) t
  4. | Apply : {
    1. f : ('a -> 'b) t;
    2. x : 'a t;
    } -> 'b t
  5. | Flag : {
    1. names : string Cmdlang_ast.Ast.Nonempty_list.t;
    2. doc : string;
    3. var : bool Stdlib.ref;
    } -> bool t
  6. | Flag_count : {
    1. names : string Cmdlang_ast.Ast.Nonempty_list.t;
    2. doc : string;
    3. var : int Stdlib.ref;
    } -> int t
  7. | Named : {
    1. names : string Cmdlang_ast.Ast.Nonempty_list.t;
    2. param : 'a Cmdlang_ast.Ast.Param.t;
    3. docv : string option;
    4. doc : string;
    5. var : 'a option Stdlib.ref;
    } -> 'a t
  8. | Named_multi : {
    1. names : string Cmdlang_ast.Ast.Nonempty_list.t;
    2. param : 'a Cmdlang_ast.Ast.Param.t;
    3. docv : string option;
    4. doc : string;
    5. rev_var : 'a list Stdlib.ref;
    } -> 'a list t
  9. | Named_opt : {
    1. names : string Cmdlang_ast.Ast.Nonempty_list.t;
    2. param : 'a Cmdlang_ast.Ast.Param.t;
    3. docv : string option;
    4. doc : string;
    5. var : 'a option Stdlib.ref;
    } -> 'a option t
  10. | Named_with_default : {
    1. names : string Cmdlang_ast.Ast.Nonempty_list.t;
    2. param : 'a Cmdlang_ast.Ast.Param.t;
    3. default : 'a;
    4. docv : string option;
    5. doc : string;
    6. var : 'a option Stdlib.ref;
    } -> 'a t
  11. | Pos : {
    1. pos : int;
    2. param : 'a Cmdlang_ast.Ast.Param.t;
    3. docv : string option;
    4. doc : string;
    5. var : 'a option Stdlib.ref;
    } -> 'a t
  12. | Pos_opt : {
    1. pos : int;
    2. param : 'a Cmdlang_ast.Ast.Param.t;
    3. docv : string option;
    4. doc : string;
    5. var : 'a option Stdlib.ref;
    } -> 'a option t
  13. | Pos_with_default : {
    1. pos : int;
    2. param : 'a Cmdlang_ast.Ast.Param.t;
    3. default : 'a;
    4. docv : string option;
    5. doc : string;
    6. var : 'a option Stdlib.ref;
    } -> 'a t
  14. | Pos_all : {
    1. param : 'a Cmdlang_ast.Ast.Param.t;
    2. docv : string option;
    3. doc : string;
    4. rev_var : 'a list Stdlib.ref;
    } -> 'a list t
val create : 'a Cmdlang_ast.Ast.Arg.t -> 'a t

Recursively allocate an arg state for all arguments contained in a parser.

Finalization

This part of the interface deals with finalizing the state and returning an expression suitable for execution.

It must be called last, once all the parsing and mutating is done.

module Parse_error : sig ... end
val finalize : 'a t -> ('a Arg_runner.t, Parse_error.t) Stdlib.Result.t

The idea with finalize is to split the execution into 2 isolated parts : the part where the command line is parsed, and the part where user code is actually ran.