File: test_shell.ml

package info (click to toggle)
cmdliner 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: ml: 7,287; sh: 146; makefile: 108
file content (59 lines) | stat: -rw-r--r-- 2,202 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(*---------------------------------------------------------------------------
   Copyright (c) 2025 The cmdliner programmers. All rights reserved.
   SPDX-License-Identifier: ISC
  ---------------------------------------------------------------------------*)

(* Use to test for completion interactively. *)

let tool ~file ~dir ~path ~choice ~cmd ~message =
  print_endline "Happy?";
  Cmdliner.Cmd.Exit.ok

open Cmdliner
open Cmdliner.Term.Syntax

let cmd =
  Cmd.make (Cmd.info "test_shell" ~version:"v2.1.0") @@
  let+ file =
    let doc = "Use me to test for filepath completion." in
    Arg.(value & opt (some filepath) None & info ["file"; "f"] ~doc)
  and+ dir =
    let doc = "Use me to test for dirpath completion." in
    Arg.(value & opt (some dirpath) None & info ["dir"; "d"] ~doc)
  and+ path =
    let doc  = "Use me to test for path completion." in
    Arg.(value & opt (some path) None & info ["path"; "p"] ~doc)
  and+ choice =
    let doc =
      "Use me to test for enum completion for short and long options."
    in
    Arg.(value & opt (enum ["one", `One; "two", `Two; "three", `Three]) `One
         & info ["choice"; "c"] ~doc)
  and+ cmd =
    let arg_conv =
      let completion = Arg.Completion.complete_restart in
      Arg.Conv.of_conv ~completion Arg.string
    in
    let doc =
      "Use me to test for completion restart of other tools after \
       the $(b,--) token. This may not work through a $(b,b0) \
       invocation. Install completion for the $(b,test_shell) tool \
       and invoke the tool directly from the path output with
       $(b,b0 --path -- test_shell)."
    in
    Arg.(value & pos_all arg_conv [] & info [] ~doc ~docv:"ARG")
  and+ message =
    let doc = "Use me to test the Message directive in completion" in
    let msg_conv =
      let completion =
        let complete _ ~token:_ = Error "whoops! This is a message!" in
        Arg.Completion.make ?context:None complete
      in
      Arg.Conv.of_conv ~completion Arg.string
    in
    Arg.(value & opt (some msg_conv) None & info ["message"] ~doc)
  in
  tool ~file ~dir ~path ~choice ~cmd ~message

let main () = Cmd.eval' cmd
let () = if !Sys.interactive then () else exit (main ())