Cmdliner cheatsheet

As a follow-up to an earlier conversation, I made a cheatsheet and a template for using cmdliner by @dbuenzli. It was done quickly and I don’t know everything about cmdliner, so please let me know if you see mistakes.

Enjoy!

14 Likes

Good to see this. I believe a common use case to add are sub commands as popularised by git. It looks like this in my code:

module C = Cmdliner

let report =
  let doc = "generate HTML or JSON report for an outing" in
  let man = ..   in
  C.Term.
    (ret (const make $ common_options $ json $ path), info "report" ~doc ~man)

let default =
  let help = `Help (`Pager, None) in
  let doc = "GPS analysis for rowers" in
  C.Term.(ret @@ const help, info "eightplus" ~doc ~man)

let cmds = [ export; report; topspeed; debug; summary; help ]
let main () = C.Term.(eval_choice default cmds |> exit)
let () = if !Sys.interactive then () else main ()
6 Likes

Excellent, thank you, I’ll add this. It’s something I wanted on the cheatsheet until I realized I hadn’t done it before.

In this same vein, I’ve been compiling “executable notes” whenever I find myself needing a certain Cmdlner recipe. I took took these recent discussion as an occasion to document the module a bit: GitHub - shonfeder/kwdcmd: Keywords to Write Command Lines ~ Partial porcelain around Cmdliner

The aim is to provide “self-documenting” constructors that encode the composition of common CLI terms into module namespaces, labeled args, and type aliases. The hope being that I can have the type signature of a combinator give me all the hints I need to avoid having to look up the documentation every time :laughing:

It’s just a very rough (and quite imperfect) collection of idioms I’ve found useful, but it could be worth a look! When i get a chance, I hope to look through your cheat sheet to make sure I have a representative constructor for each idiom you’ve documented.

2 Likes

I just added a demo/template for subcommand handling. There are now two demo programs. One is focused on the different kinds of arguments and the other one on subcommands.

2 Likes