Module Stdlib.Arg

Contents

Instructions: Use this module in your project

In the IDE (CLion, Visual Studio Code, Xcode, etc.) you use for your DkSDK project:

  1. Add the following to your project's dependencies/CMakeLists.txt:

    Copy
    DkSDKProject_DeclareAvailable(ocaml
        CONSTRAINT "= 4.14.0"
        FINDLIBS str unix runtime_events threads dynlink)
    DkSDKProject_MakeAvailable(ocaml)
  2. Add the Findlib::ocaml library to any desired targets in src/*/CMakeLists.txt:

    Copy
    target_link_libraries(YourPackage_YourLibraryName
         # ... existing libraries, if any ...
         Findlib::ocaml)
  3. Click your IDE's Build button

Not using DkSDK?

FIRST, do one or all of the following:

  1. Run:

    Copy
    opam install ocaml.4.14.0
  2. Edit your dune-project and add:

    Copy
    (package
      (name YourExistingPackage)
      (depends
      ; ... existing dependenices ...
      (ocaml (>= 4.14.0))))

    Then run:

    Copy
    dune build *.opam # if this fails, run: dune build
  3. Edit your <package>.opam file and add:

    Copy
    depends: [
      # ... existing dependencies ...
      "ocaml" {>= "4.14.0"}
    ]

    Then run:

    Copy
    opam install . --deps-only

FINALLY, add the library to any desired (library)and/or (executable) targets in your **/dune files:

Copy
(library
  (name YourLibrary)
  ; ... existing library options ...
  (libraries
    ; ... existing libraries ...
    ))

(executable
  (name YourExecutable)
  ; ... existing executable options ...
  (libraries
    ; ... existing libraries ...
    ))
typespec`` =
| Unit of``unit-> unit

(* Call the function with unit argument

*)

| Bool of``bool-> unit

(* Call the function with a bool argument

*)

| Set of``boolref

(* Set the reference to true

*)

| Clear of``boolref

(* Set the reference to false

*)

| String of``string-> unit

(* Call the function with a string argument

*)

| Set_string of``stringref

(* Set the reference to the string argument

*)

| Int of``int-> unit

(* Call the function with an int argument

*)

| Set_int of``intref

(* Set the reference to the int argument

*)

| Float of``float-> unit

(* Call the function with a float argument

*)

| Set_float of``floatref

(* Set the reference to the float argument

*)

| Tuple of spec list

(* Take several arguments according to the spec list

*)

| Symbol of``string list`` * ``string-> unit

(* Take one of the symbols as argument and call the function with the symbol

*)

| Rest of``string-> unit

(* Stop interpreting keywords and call the function with each remaining argument

*)

| Rest_all of``string list``-> unit

(* Stop interpreting keywords and call the function with all remaining arguments

*)

| Expand of``string-> ``string array

(* If the remaining arguments to process are of the form ["-foo"; "arg"] @ rest where "foo" is registered as Expand f, then the arguments f "arg" @ rest are processed. Only allowed in parse_and_expand_argv_dynamic.

*)

The concrete type describing the behavior associated with a keyword.

type key`` = string
type doc`` = string
type usage_msg`` = string
typeanon_fun`` = ``string-> unit
val parse : ``(key*spec*doc)`` list`` -> anon_fun -> usage_msg -> unit

Arg.parse speclist anon_fun usage_msg parses the command line. speclist is a list of triples (key, spec, doc). key is the option keyword, it must start with a '-' character. spec gives the option type and the function to call when this option is found on the command line. doc is a one-line description of this option. anon_fun is called on anonymous arguments. The functions in spec and anon_fun are called in the same order as their arguments appear on the command line.

If an error occurs, Arg.parse exits the program, after printing to standard error an error message as follows:

  • The reason for the error: unknown option, invalid or missing argument, etc.
  • usage_msg
  • The list of options, each followed by the corresponding doc string. Beware: options that have an empty doc string will not be included in the list.

For the user to be able to specify anonymous arguments starting with a -, include for example ("-", String anon_fun, doc) in speclist.

By default, parse recognizes two unit options, -help and --help, which will print to standard output usage_msg and the list of options, and exit the program. You can override this behaviour by specifying your own -help and --help options in speclist.

val parse_dynamic : ``(key*spec*doc)`` list`` ref -> anon_fun -> usage_msg -> unit

Same as Arg.parse, except that the speclist 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:

  • command subcommand options where the list of options depends on the value of the subcommand argument.

  • since 4.01.0

valparse_argv : ``?current:``intref ->``string array``-> ``(key*spec*doc)`` list`` -> anon_fun -> usage_msg -> unit

Arg.parse_argv ~current args speclist anon_fun usage_msg parses the array args as if it were the command line. It uses and updates the value of ~current (if given), or Arg.current. You must set it before calling parse_argv. The initial value of current is the index of the program name (argument 0) in the array. If an error occurs, Arg.parse_argv raises Arg.Bad with the error message as argument. If option -help or --help is given, Arg.parse_argv raises Arg.Help with the help message as argument.

valparse_argv_dynamic : ``?current:``intref ->``string array``-> ``(key*spec*doc)`` list`` ref -> anon_fun ->``string-> unit

Same as Arg.parse_argv, except that the speclist argument is a reference and may be updated during the parsing. See Arg.parse_dynamic.

  • since 4.01.0
valparse_and_expand_argv_dynamic : ``intref ->``string array``ref -> ``(key*spec*doc)`` list`` ref -> anon_fun ->``string-> unit

Same as Arg.parse_argv_dynamic, except that the argv argument is a reference and may be updated during the parsing of Expand arguments. See Arg.parse_argv_dynamic.

  • since 4.05.0
val parse_expand : ``(key*spec*doc)`` list`` -> anon_fun -> usage_msg -> unit

Same as Arg.parse, except that the Expand arguments are allowed and the current reference is not updated.

  • since 4.05.0
exception Help of string

Raised by Arg.parse_argv when the user asks for help.

exception Bad of string

Functions in spec or anon_fun can raise Arg.Bad with an error message to reject invalid arguments. Arg.Bad is also raised by Arg.parse_argv in case of an error.

val usage : ``(key*spec*doc)`` list`` -> usage_msg -> unit

Arg.usage speclist usage_msg prints to standard error an error message that includes the list of valid options. This is the same message that Arg.parse prints in case of error. speclist and usage_msg are the same as for Arg.parse.

val usage_string : ``(key*spec*doc)`` list`` -> usage_msg -> string

Returns the message that would have been printed by Arg.usage, if provided with the same parameters.

valalign : ``?limit:int-> ``(key*spec*doc)`` list`` -> ``(key*spec*doc)`` list

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 Symbol arguments are aligned on the next line.

  • parameter limit

    options with keyword and message longer than limit will not be used to compute the alignment.

valcurrent : ``intref

Position (in Sys.argv) of the argument being processed. You can change this value, e.g. to force Arg.parse to skip some arguments. Arg.parse uses the initial value of Arg.current as the index of argument 0 (the program name) and starts parsing arguments at the next element.

valread_arg : ``string-> ``string array

Arg.read_arg file reads newline-terminated command line arguments from file file.

  • since 4.05.0
valread_arg0 : ``string-> ``string array

Identical to Arg.read_arg but assumes null character terminated command line arguments.

  • since 4.05.0
valwrite_arg : ``string->``string array``-> unit

Arg.write_arg file args writes the arguments args newline-terminated into the file file. If the any of the arguments in args contains a newline, use Arg.write_arg0 instead.

  • since 4.05.0
valwrite_arg0 : ``string->``string array``-> unit

Identical to Arg.write_arg but uses the null character for terminator instead of newline.

  • since 4.05.0

More from the DkSDK Book

    1. DkSDK
      1. Package capnp
        1. Module Capnp
            1. Module type MessageSig.MESSAGE
            1. Module type MessageSig.S
              1. Module S.ListStorage
              1. Module S.Message
              1. Module S.Object
              1. Module S.Segment
              1. Module S.Slice
              1. Module S.StructStorage
            1. Module type MessageSig.SEGMENT
            1. Module type MessageSig.SLICE
        1. Module Capnp_unix
      1. Package cmdliner
        1. Module Cmdliner
        1. Module Cmdliner_arg
        1. Module Cmdliner_base
        1. Module Cmdliner_cline
        1. Module Cmdliner_cmd
        1. Module Cmdliner_docgen
        1. Module Cmdliner_eval
        1. Module Cmdliner_info
        1. Module Cmdliner_manpage
        1. Module Cmdliner_msg
        1. Module Cmdliner_term
        1. Module Cmdliner_term_deprecated
        1. Module Cmdliner_trie
      1. Package fmt
        1. Module Fmt
        1. Module Fmt_cli
        1. Module Fmt_tty
      1. Package logs
        1. Module Logs
          1. Module type Logs.LOG
          1. ...
        1. Module Logs_cli
        1. Module Logs_fmt
        1. Module Logs_lwt
          1. Module type Logs_lwt.LOG
        1. Module Logs_threaded
      1. Package lwt
        1. Module Lwt
        1. Module Lwt_bytes
        1. Module Lwt_condition
        1. Module Lwt_config
        1. Module Lwt_engine
        1. Module Lwt_features
        1. Module Lwt_fmt
        1. Module Lwt_gc
        1. Module Lwt_io
            1. Module type Lwt_io.NumberIO
        1. Module Lwt_list
        1. Module Lwt_main
            1. Module type Lwt_main.Hooks
        1. Module Lwt_mutex
        1. Module Lwt_mvar
        1. Module Lwt_pool
        1. Module Lwt_pqueue
            1. Module type Lwt_pqueue.OrderedType
            1. Module type Lwt_pqueue.S
        1. Module Lwt_preemptive
        1. Module Lwt_process
        1. Module Lwt_result
        1. Module Lwt_seq
        1. Module Lwt_sequence
        1. Module Lwt_stream
        1. Module Lwt_switch
        1. Module Lwt_sys
        1. Module Lwt_throttle
            1. Module type Lwt_throttle.S
        1. Module Lwt_timeout
        1. Module Lwt_unix
      1. Package mtime
        1. Module Mtime
        1. Module Mtime_clock
      1. Package ocaml
        1. Module Bigarray
        1. Module Condition
        1. Module Dynlink
        1. Module Event
        1. Module Mutex
        1. Module Profiling
        1. Module Semaphore
        1. Module Stdlib
          1. ...
          1. Module Stdlib.Arg
        1. Module Str
        1. Module Thread
        1. Module ThreadUnix
        1. Module Topdirs
        1. Module Unix
        1. Module UnixLabels
      1. Package
      1. Package result
        1. Module Result
      1. Package stdint
        1. Module Stdint
            1. Module type Stdint.Int