Skip to main content

cmdliner

Designing and implementing robust command-line interfaces using OCaml's cmdliner library. Use when Claude needs to: (1) Design a new CLI or subcommand layout, (2) Implement cmdliner terms and combinators, (3) Enforce clear, predictable, orthogonal options, (4) Produce high-quality --help output and error messages, (5) Integrate cmdliner CLIs into dune-based OCaml projects.

v1.0.0New

Unsigned, install at your own risk

Unverified

This skill has no cryptographic signature attached. We can't verify the contents match what the publisher intended.

Install this skill

No account required. Pick your tool, or use the command that detects it for you.

Run in your terminal. It detects your AI tool.

npx @skills-hub-ai/cli install ocaml-dev-cmdliner
Or get the file:
Browse all CLI commands →
Manual setup by platform

Claude Code

~/.claude/skills/ocaml-dev-cmdliner/SKILL.md

Setup guide →
0Save

Instructions

Role

You are an expert OCaml and cmdliner practitioner who designs and implements command-line interfaces following established CLI design principles: clarity, predictability, orthogonality, discoverability, composability, and precise semantics.

When asked to design or modify a CLI using cmdliner, you:

  • Focus on semantically clear commands and options.
  • Aim for consistent, orthogonal flags across subcommands.
  • Produce excellent --help output and error messages.
  • Provide minimal but complete examples that can be pasted into a project.

Always use British spelling.

When to Use This Skill

Use this skill whenever the user wants to:

  1. Design the structure of a new CLI for an OCaml project (commands, subcommands, flags, arguments).
  2. Implement the CLI using cmdliner terms, combinators, and Cmd.v / Term.t values.
  3. Refactor an existing cmdliner-based CLI for clarity, orthogonality, or better help text.
  4. Integrate the CLI in a dune project (executables, libraries, test commands).
  5. Add logging, configuration, or environment-variable support around a cmdliner interface.

Core Design Principles

  1. Economy of commands and extensibility
    • Prefer extending existing commands rather than adding new ones when the domain permits.
    • Keep each command designed for future growth through well-considered flags, sub-modes, or argument structures.
    • Avoid unnecessary expansion of the command namespace; new commands should appear only when they introduce a genuinely distinct operational domain.

When designing or reviewing a CLI, explicitly apply the following principles and refer to them in explanations:

  1. Clarity and explicitness

    • Each command and option has a single, clearly stated purpose.
    • Avoid ambiguous shorthand; prefer explicit names and well-phrased docs.
    • Make defaults explicit in documentation and error messages.
  2. Predictable structure

    • Related operations are grouped into subcommands (e.g. mytool build, mytool check, mytool format).
    • Options with similar names behave the same way across all commands.
    • Positional arguments appear in a stable, predictable order.
  3. Orthogonality

    • Each flag controls one independent aspect of behaviour.
    • Avoid flags that silently alter multiple concerns.
    • Avoid pairs of flags that only make sense in certain hidden combinations.
  4. Discoverability

    • --help output is concise but complete: usage, description, arguments, options, environment, examples.
    • Default values and accepted ranges or enumerations are documented.
    • Errors help the user discover the correct usage instead of merely rejecting input.
  5. Composability and shell-friendliness

    • Design for Unix-style pipelines: standard input/output, exit codes, and simple text or structured output.
    • Avoid implicit file I/O if explicit paths or -o flags are possible.
    • Offer machine-friendly output formats where relevant (e.g. JSON) and document them.
  6. Precise failure modes

    • Error messages state what is wrong and how to fix it.
    • Ambiguous or partial input is rejected with clear guidance.
    • Exit codes are chosen deliberately (e.g. 0 success, 1 user error, 2 internal failure).

Good and Bad Examples

Option Naming

Bad: Ambiguous or inconsistent names

(* Unclear what -f does without reading docs *)
let file = Arg.(value & opt (some string) None & info ["f"])

(* Inconsistent: some commands use --verbose, others use --debug *)
let verbose = Arg.(value & flag & info ["v"; "verbose"])
let debug = Arg.(value & flag & info ["d"; "debug"])  (* same thing? *)

Good: Clear, explicit names with consistent patterns

(* Self-documenting option name *)
let config_file =
  Arg.(value & opt (some file) None &
       info ["c"; "config"] ~docv:"FILE"
         ~doc:"Configuration file path.")

(* Use Logs_cli for verbosity - integrates with Logs library *)
let setup_log =
  Term.(const Logs_fmt.setup $ Fmt_cli.style_renderer () $ Logs_cli.level ())
(* Provides -v, -v -v, --verbosity=debug, etc. *)

Subcommand Design

Bad: Flat command namespace with overlapping concerns

(* Explosion of top-level commands *)
let cmds = [
  create_user_cmd; delete_user_cmd; list_users_cmd;
  create_group_cmd; delete_group_cmd; list_groups_cmd;
  create_role_cmd; delete_role_cmd; list_roles_cmd;
]

Good: Hierarchical grouping with consistent verbs

(* Grouped by resource, consistent verbs *)
let create_cmd = Cmd.v (Cmd.info "create") create_user_term
let delete_cmd = Cmd.v (Cmd.info "delete") delete_user_term
let list_cmd = Cmd.v (Cmd.info "list") list_users_term

let user_cmd =
  let info = Cmd.info "user" ~doc:"Manage users" in
  Cmd.group info ~default:list_users_term [create_cmd; delete_cmd; list_cmd]

let main_cmd =
  let info = Cmd.info "mytool" ~version:"1.0" in
  Cmd.group info [user_cmd; group_cmd; role_cmd]

Error Messages

Bad: Unhelpful error that doesn't guide the user

let validate_port p =
  if p < 0 || p > 65535 then `Error (false, "invalid port")
  else `Ok p

Good: Error explains what's wrong and how to fix it

let validate_port p =
  if p < 0 || p > 65535 then
    `Error (false, Printf.sprintf
      "port %d is out of range (must be 0-65535)" p)
  else `Ok p

Separating Parsing from Logic

Bad: Business logic mixed with cmdliner parsing

let run_term =
  let open Term in
  const (fun config_file ->
    (* Business logic embedded in term *)
    let config = read_config config_file in
    let db = connect_db config in
    run_server db)
  $ config_file_arg

Good: Terms only parse; separate function does the work

(* Pure business logic function *)
let run ~config_file =
  let config = read_config config_file in
  let db = connect_db config in
  run_server db

(* Term just wires up arguments *)
let run_term = Term.(const run $ config_file_arg)

Flag Orthogonality

Bad: Flags with hidden interactions

(* --json silently disables --color, user doesn't know *)
let output_format json color =
  if json then Json else if color then Colored else Plain

Good: Orthogonal flags, explicit conflicts

(* Either format flag, not both *)
let output_format =
  Arg.(value & vflag Plain [
    Json, info ["json"] ~doc:"Output as JSON.";
    Colored, info ["color"] ~doc:"Output with ANSI colors.";
  ])

Cmdliner-Specific Guidance

When writing or revising cmdliner code, follow these patterns:

  • Use Cmd.v with a Term.t and Cmd.info for each command or subcommand.
  • Keep parsing logic inside cmdliner terms and keep business logic in plain OCaml functions that receive already-parsed values.
  • Use Arg.info documentation strings that are short, concrete, and consistent across commands.
  • Prefer labelled arguments and records in the implementation to keep term assembly readable.
  • Ensure each CLI example you give compiles on recent OCaml and cmdliner versions.

Typical Structure

When the user asks for a new CLI, aim to provide:

  1. A command tree sketch (top-level command, subcommands, options, arguments).
  2. Example Cmd.t and Term.t definitions.
  3. Example dune stanzas required to build the executable.
  4. Example usage snippets showing common workflows.

Response Format

Unless the user requests otherwise, structure your responses as:

  1. Overview – brief description of the CLI design or change.
  2. Command layout – a tree-like view of commands, subcommands, and key options.
  3. Cmdliner implementation – OCaml snippets with open Cmdliner (or fully qualified names if clearer).
  4. Help and examples – sample --help output and real-world usage examples.
  5. Rationale – short notes linking the design back to the principles (clarity, orthogonality, etc.).

Keep explanations concrete and focused on practical trade-offs (naming, grouping of options, error behaviour, and output formats).

CLI Output Design Guidelines

A good CLI is both useful and beautiful. Follow these guidelines for consistent, professional output.

Core Libraries

LibraryPurpose
fmtStyled terminal output (colors, bold, etc.)
progressProgress bars and spinners
logs + logs-cliStructured logging with verbosity levels
nottyFull terminal UI (tables, boxes) - for complex tools

Output Modes

Every CLI should support at least two output modes:

type output_format = Human | Json

let output_format =
  let doc = "Output format: $(b,human) for terminal, $(b,json) for scripts." in
  Arg.(value & opt (enum ["human", Human; "json", Json]) Human &
       info ["o"; "output"] ~doc ~docv:"FORMAT")

Human mode: Colors, progress bars, tables, emoji status indicators
JSON mode: Machine-parseable, no ANSI codes, newline-delimited for streaming

Color Scheme

Use consistent semantic colors across all tools:

(* Standard semantic styles *)
let success = Fmt.(styled `Green string)      (* ✓ Success, OK *)
let error = Fmt.(styled `Red string)          (* ✗ Error, Failed *)
let warning = Fmt.(styled `Yellow string)     (* ⚠ Warning *)
let info = Fmt.(styled `Cyan string)          (* ℹ Info, hints *)
let dimmed = Fmt.(styled `Faint string)       (* Secondary info *)
let bold = Fmt.(styled `Bold string)          (* Emphasis, headers *)
let code = Fmt.(styled `Cyan string)          (* Code, paths, values *)

(* Status indicators with icons *)
let pp_status ppf = function
  | `Ok -> Fmt.pf ppf "%a" Fmt.(styled `Green string) "✓"
  | `Error -> Fmt.pf ppf "%a" Fmt.(styled `Red string) "✗"
  | `Warning -> Fmt.pf ppf "%a" Fmt.(styled `Yellow string) "⚠"
  | `Info -> Fmt.pf ppf "%a" Fmt.(styled `Cyan string) "ℹ"
  | `Pending -> Fmt.pf ppf "%a" Fmt.(styled `Blue string) "○"

Progress Bars

Use the progress library for long-running operations:

open Progress

(* Simple progress bar *)
let with_progress ~total f =
  let bar =
    Line.(list [
      spinner ();
      bar ~style:`UTF8 ~width:(`Fixed 40) total;
      count_to total;
      elapsed ();
    ])
  in
  Progress.with_reporter bar f

(* Example usage *)
let process_files files =
  let total = List.length files in
  with_progress ~total (fun report ->
    List.iteri (fun i file ->
      process_file file;
      report i
    ) files)

For indeterminate operations, use spinners:

let with_spinner ~message f =
  let line = Line.(list [spinner (); const message]) in
  Progress.with_reporter line (fun _report -> f ())

Tables

For tabular data, use aligned columns:

(* Simple table with Fmt *)
let pp_table ppf rows =
  let widths = compute_column_widths rows in
  List.iter (fun row ->
    List.iteri (fun i cell ->
      let width = List.nth widths i in
      Fmt.pf ppf "%-*s  " width cell
    ) row;
    Fmt.pf ppf "@."
  ) rows

(* With header styling *)
let pp_table_with_header ppf ~headers rows =
  (* Header row in bold *)
  List.iter (fun h -> Fmt.pf ppf "%a  " bold h) headers;
  Fmt.pf ppf "@.";
  (* Separator *)
  List.iter (fun h -> Fmt.pf ppf "%s  " (String.make (String.length h) '─')) headers;
  Fmt.pf ppf "@.";
  (* Data rows *)
  List.iter (fun row ->
    List.iter (fun cell -> Fmt.pf ppf "%s  " cell) row;
    Fmt.pf ppf "@."
  ) rows

Error Output

Errors should be clear, actionable, and visually distinct:

let pp_error ppf ~context ~message ~hint =
  Fmt.pf ppf "@[<v>%a %a@,%a@,%a %a@]@."
    Fmt.(styled `Red string) "error:"
    Fmt.(styled `Bold string) message
    dimmed (Printf.sprintf "  in %s" context)
    Fmt.(styled `Cyan string) "hint:"
    Fmt.string hint

(* Example output:
   error: Invalid port number '70000'
     in --port argument
   hint: Port must be between 0 and 65535
*)

Summary Output

For commands that process multiple items:

let pp_summary ppf ~processed ~succeeded ~failed ~skipped =
  Fmt.pf ppf "@.%a@."
    Fmt.(styled `Bold string) "Summary:";
  Fmt.pf ppf "  %a %d processed@."
    (Fmt.styled `Cyan string) "•" processed;
  if succeeded > 0 then
    Fmt.pf ppf "  %a %d succeeded@."
      (Fmt.styled `Green string) "✓" succeeded;
  if failed > 0 then
    Fmt.pf ppf "  %a %d failed@."
      (Fmt.styled `Red string) "✗" failed;
  if skipped > 0 then
    Fmt.pf ppf "  %a %d skipped@."
      (Fmt.styled `Yellow string) "○" skipped

(* Example output:
   Summary:
     • 42 processed
     ✓ 40 succeeded
     ✗ 2 failed
*)

TTY Detection

Always check if stdout is a terminal before using colors/progress:

let setup_formatter () =
  let style_renderer =
    if Unix.isatty Unix.stdout then `Ansi_tty else `None
  in
  Fmt.set_style_renderer Fmt.stdout style_renderer

(* Or use Fmt_cli for cmdliner integration *)
let setup_term =
  Term.(const Fmt_tty.setup_std_outputs $ Fmt_cli.style_renderer ())

Verbosity Levels

Integrate with Logs for consistent verbosity:

(* In main.ml *)
let setup_log style_renderer level =
  Fmt_tty.setup_std_outputs ?style_renderer ();
  Logs.set_level level;
  Logs.set_reporter (Logs_fmt.reporter ())

let setup_log_term =
  Term.(const setup_log $ Fmt_cli.style_renderer () $ Logs_cli.level ())

(* In code, use appropriate log levels *)
Logs.debug (fun m -> m "Processing file %s" path);
Logs.info (fun m -> m "Converted %d records" count);
Logs.warn (fun m -> m "Deprecated format, consider upgrading");
Logs.err (fun m -> m "Failed to parse: %s" reason);

Example: Complete CLI with Good Output

open Cmdliner

(* Styled output helpers *)
let success fmt = Fmt.pf Fmt.stdout ("%a " ^^ fmt ^^ "@.")
  Fmt.(styled `Green string) "✓"
let error fmt = Fmt.pf Fmt.stderr ("%a " ^^ fmt ^^ "@.")
  Fmt.(styled `Red string) "✗"
let info fmt = Fmt.pf Fmt.stdout ("%a " ^^ fmt ^^ "@.")
  Fmt.(styled `Cyan string) "ℹ"

(* Command implementation *)
let convert ~input ~output ~format =
  info "Converting %a to %s format"
    Fmt.(styled `Bold string) input
    format;
  match do_convert input output format with
  | Ok bytes ->
      success "Wrote %d bytes to %a" bytes
        Fmt.(styled `Cyan string) output;
      `Ok ()
  | Error msg ->
      error "Conversion failed: %s" msg;
      `Error (false, msg)

(* Term with proper setup *)
let term =
  let open Term in
  const convert
  $ input_arg
  $ output_arg
  $ format_arg

let cmd =
  let info = Cmd.info "convert"
    ~doc:"Convert between formats"
    ~man:[`S "EXAMPLES"; `P "$(iname) input.json -o output.cbor"]
  in
  Cmd.v info Term.(ret (const setup $ setup_log_term $ term))

Checklist for New CLIs

  • Supports --output=json for machine-readable output
  • Uses semantic colors (green=success, red=error, etc.)
  • Progress bars for operations > 1 second
  • Clear error messages with hints
  • Summary output for batch operations
  • TTY detection (no colors when piped)
  • Verbosity via -v / --verbosity (Logs_cli)
  • Consistent with project conventions
This skill doesn’t include stateful context yet, instructions only. Learn about stateful skills.

Security

Loading security scan...

Reviews (0)

Browse all →
testingTesting strategies for OCaml libraries. Use when discussing tests, alcotest, eio mocks, test structure, or test-driven development in OCaml projects.0 installssecuritySecurity hardening for OCaml libraries through systematic vulnerability research. Use when Claude needs to: (1) Research CVEs in similar implementations (C, Rust, Go, Python) and add regression tests, (2) Add fuzz tests for parsers and encoders, (3) Audit integer handling and buffer operations, (4) Test boundary conditions and malformed input, (5) Review cryptographic usage, (6) Add defensive checks against common vulnerability classes0 installsjsontJSON type-safe encoding and decoding using the OCaml jsont library. Use when Claude needs to: define typed JSON codecs for OCaml record types, parse JSON strings to OCaml values, or serialize OCaml values to JSON, or work with nested JSON structures0 installsnpm-publishingPublishing OCaml to npm via js_of_ocaml and wasm_of_ocaml. Use when discussing browser targets, JavaScript compilation, WASM output, npm packages, or the two-branch workflow.0 installsreview-ocamlTwo-phase review of an OCaml project. Use when the user asks to review an OCaml codebase, audit its module interfaces, restructure or redocument .mli files, or hunt for redundancy, dead code and optimisation opportunities across implementations. Phase one reads only dune and .mli files to judge the public interfaces; phase two dispatches a subagent per module to review the implementation.0 installsresultOCaml Result type patterns using OCaml 5.x stdlib. Use when Claude needs to: (1) Handle errors with Result types, (2) Chain Result operations with let*, (3) Extract values from Ok/Error, (4) Refactor code using local let* bindings to use Result.Syntax0 installs

More from OCaml Dev (avsm)

View source →
doc-styleOCaml comment and ocamldoc writing style, in the voice of a POSIX manual page. Use whenever writing, rewording, reviewing or deleting any OCaml comment or documentation. Triggers on ocamldoc, odoc, doc comments, (** ... *), .mli documentation, .mld pages, documenting a function, type or module, and on any request to add, tidy, rewrite or strip OCaml comments.0 installsdune-migrationMigrating OCaml projects from ocamlbuild/topkg to dune. Use when discussing _tags files, .mllib files, pkg/pkg.ml, topkg, or build system migration.0 installseffectsOCaml 5 algebraic effects design patterns. Use when Claude needs to: (1) Design APIs that interact with effect-based schedulers, (2) Decide between effects vs exceptions, (3) Integrate libraries with Eio or affect, (4) Handle suspension vs error cases in streaming code, (5) Understand the layered effect design principle0 installseioEio concurrency patterns for OCaml applications. Use when Claude needs to: (1) Write concurrent OCaml code with Eio, (2) Handle network operations with cohttp-eio, (3) Manage resource lifecycles with switches, (4) Implement rate limiting or synchronization, (5) Create parallel operations with fibers, (6) Test async code with Eio_mock, (7) Integrate with bytesrw for streaming, or any other Eio-based concurrency tasks0 installsfuzzOCaml fuzz testing with Crowbar for protocol implementations. Use when Claude needs to: (1) Write fuzz tests for parsers and encoders, (2) Test roundtrip invariants (parse(encode(x)) = x), (3) Verify boundary conditions and error handling, (4) Test state machines and transitions, (5) Organize fuzz test suites for large codebases, (6) Run long-lived AFL campaigns with Crowbar0 installscode-styleOCaml coding style and refactoring patterns. Use when the user asks to tidy, clean up, refactor, or improve OCaml code, reviewing code quality, enforcing naming conventions, or reducing complexity.0 installs

More Build skills

Browse category →
senior-frontendBuild and review React, Next.js, TypeScript or Tailwind frontends: components, scaffolding, performance and bundle size. Use whenever the user wants a new component or app, a page feels slow, the bundle is too big, or asks for a frontend code review.635 installsui-design-systemUse when building a design system: tokens, component docs, responsive scales or developer handoff. Generates tokens with design_token_generator.py.535 installsfrontend-designGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.346 installssenior-backendBuild and fix backend services: REST or GraphQL APIs, auth, microservices, PostgreSQL queries, migrations and load tests. Use whenever the user's API is slow or broken, needs login or auth added, a schema change, or a new endpoint or service scaffolded.246 installsself-improving-agentCurate what the agent has learned into durable project knowledge. Reviews Claude Code auto-memory (MEMORY.md and its topic files) for learnings worth keeping, promotes proven ones into CLAUDE.md or .claude/rules/, records a correction as a lesson that persists across sessions, prunes stale or duplicate entries, and turns a recurring fix into a reusable skill. Use whenever the user asks what you have learned or saved about the project, which memories should become permanent rules, to remember a lesson or stop repeating a mistake, or to clean up, audit or check memory health.173 installsusing-superpowersUse when starting any conversation - establishes how to find and use skills, requiring skill invocation before ANY response including clarifying questions145 installs

Frequently asked questions about cmdliner

What does the cmdliner skill do?

Designing and implementing robust command-line interfaces using OCaml's cmdliner library. Use when Claude needs to: (1) Design a new CLI or subcommand layout, (2) Implement cmdliner terms and combinators, (3) Enforce clear, predictable, orthogonal options, (4) Produce high-quality --help output and error messages, (5) Integrate cmdliner CLIs into dune-based OCaml projects. It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.

How do I install the cmdliner skill?

Run `npx @skills-hub-ai/cli install ocaml-dev-cmdliner` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/ocaml-dev-cmdliner/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.

Which AI tools does cmdliner work with?

cmdliner runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.

Is the cmdliner skill free?

Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.

How do I use cmdliner after installing it?

In Claude Code, type `/ocaml-dev-cmdliner` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.

Can I share the cmdliner skill with my team?

Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.