Skip to content

Commit 3865a21

Browse files
committed
Update readme
1 parent 37e00ce commit 3865a21

3 files changed

Lines changed: 54 additions & 76 deletions

File tree

README.md

Lines changed: 50 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44

55
A declarative command-line parser for OCaml.
66

7-
## Rationale
7+
The API is documented [here](https://ocaml.org/p/climate/latest).
88

9-
Programs written in OCaml should conform to existing UX conventions so as to
10-
match the expectations of users coming from other tools. For command-line
11-
programs which wish to parse their arguments in a declarative style, existing
12-
solutions all seem to deviate from the conventions established by common Unix
13-
tools. The two popular libraries for declarative command-line argument parsing
14-
in OCaml are`cmdliner` and `base`'s `Command` module. Both of these libraries
15-
present unconventional behaviour in that non-ambiguous prefixes of arguments are
16-
treated as the full argument names. Additionally, `cmdliner` lacks support for
17-
generating shell autocompletion scripts, and `base` only supports arguments
18-
beginning with a single `-`.
9+
## About
1910

20-
This library aims to be an alternative to `cmdliner` and `Base.Command` with
21-
support for generating autocompletion scripts and which behaves as
22-
conventionally as possible.
11+
A library for parsing command-line arguments in a declarative style, where
12+
specifying the parser spec and assigning parsed arguments to variables in your
13+
program is done with the same code. Climate supports CLIs with nested
14+
subcommands, and can automatically generate help messages, manpages, and shell
15+
completion scripts.
2316

24-
## Example
17+
Here's a tiny example:
18+
```ocaml
19+
let open Arg_parser in
20+
let+ flag_foo = flag [ "foo" ] ~doc:"some flag"
21+
and+ value_bar = named_opt [ "bar" ] string ~doc:"some string value"
22+
in
23+
(* ...do something with the parsed values... *)
24+
```
25+
26+
## Full Example
2527

2628
Here's a complete example program with built-in support for generating its own
2729
completion script.
@@ -70,46 +72,63 @@ let main ~bold ~underline ~color words =
7072
7173
let () =
7274
let command =
73-
Command.singleton ~desc:"Echo with style!"
75+
Command.singleton ~doc:"Echo with style!"
7476
@@
7577
let open Arg_parser in
76-
(* Describe and parse the command line arguments:*)
77-
let+ bold = flag [ "bold" ] ~desc:"Make the text bold"
78-
and+ underline = flag [ "underline" ] ~desc:"Underline the text"
79-
and+ color = named_opt [ "color" ] Color.conv ~desc:"Set the text color"
78+
(* Describe and parse the command line arguments: *)
79+
let+ bold = flag [ "bold" ] ~doc:"Make the text bold"
80+
and+ underline = flag [ "underline" ] ~doc:"Underline the text"
81+
and+ color = named_opt [ "color" ] Color.conv ~doc:"Set the text color"
8082
and+ words = pos_all string
8183
and+ completion =
82-
flag [ "completion" ] ~desc:"Print this program's completion script and exit"
84+
flag [ "completion" ] ~doc:"Print this program's completion script and exit"
8385
in
8486
if completion
8587
then `Completion
8688
else `Main (fun () -> main ~bold ~underline ~color words)
8789
in
8890
(* Run the parser yielding either a main function to call or an indication
8991
that we should print the completion script. *)
90-
match Command.run command with
92+
let help_style =
93+
let open Help_style in
94+
{ program_doc = { ansi_style_plain with color = Some `Green }
95+
; usage = { ansi_style_plain with color = Some `Yellow }
96+
; section_heading = { ansi_style_plain with color = Some `Red }
97+
; arg_name = { ansi_style_plain with color = Some `Blue }
98+
; arg_doc = { ansi_style_plain with color = Some `Cyan }
99+
; error = { ansi_style_plain with color = Some `Red }
100+
}
101+
in
102+
match
103+
Command.run ~program_name:(Literal "echo-ansi") ~version:"0.0.1" ~help_style command
104+
with
91105
| `Completion -> print_endline (Command.completion_script_bash command)
92106
| `Main main -> main ()
93-
;
107+
;;
94108
```
95109

96110
This program lives in `examples/echo_ansi.ml`. Run it with `dune exec
97111
examples/echo_ansi.exe -- <ARGS>`. E.g.
98112

99113
```
100114
$ dune exec examples/echo_ansi.exe -- --help
101-
Usage: _build/default/examples/echo_ansi.exe [OPTIONS] [STRING]...
102-
103115
Echo with style!
104116
117+
Usage: echo-ansi [OPTION]… [STRING]…
118+
119+
Arguments:
120+
[STRING]...
121+
105122
Options:
106-
--bold Make the text bold
107-
--underline Underline the text
108-
--color <COLOR> Set the text color
109-
--completion Print this program's completion script and exit
110-
--help, -h Print help
123+
--bold Make the text bold
124+
--underline Underline the text
125+
--color <COLOR> Set the text color
126+
--completion Print this program's completion script and exit
127+
-h, --help Show this help message.
111128
```
112129

130+
## Shell Completion
131+
113132
The easiest way to setup the completion script is to first put the executable
114133
in a directory in your PATH. E.g.
115134
```
@@ -147,52 +166,7 @@ compinit
147166
bashcompinit
148167
```
149168

150-
## Manual
151-
152-
### Terminology
153-
154-
__Term__ will refer to each space-delimited string on the command line after the
155-
program name. The command `ls -l --color=always /etc/` has 3 terms. The program
156-
name is `ls` (not a term), and the terms are `-l`, `--color=always`, and
157-
`/etc/`.
158-
159-
__Argument__ will refer to each distinct piece of information passed to the
160-
program on the command line. The command `make -td --jobs 4 all` has 4
161-
arguments. The `-td` term is made up of two arguments combined into a single
162-
term: `-t` and `-d` (more on this later). `--jobs 4` is a single argument
163-
comprising two terms, where `4` is a parameter to the argument `--jobs`. The
164-
final term `all` is also an argument.
165-
166-
Arguments may be __positional__ or __named__. Positional arguments are
167-
identified by their position in the argument list rather than by name. Named
168-
arguments may have two forms: __short__ and __long__. Short named arguments
169-
begin with a single `-` followed by a single non `-` character, such as `-l`.
170-
Long named arguments begin with `--` followed by one or more non `-` characters,
171-
such as `--jobs`. A collection of short named arguments may be combined together
172-
with a single leading `-` followed by each short argument name. For example in
173-
`ls -la`, the `-la` is an alternative way of writing `-l -a`.
174-
175-
A named argument may take a __parameter__. A parameter is a single value which
176-
follows the argument on the command line. Using `make`'s `--jobs` argument as an
177-
example, here are the different ways of passing a parameter to a named argument
178-
on the command line:
179-
180-
```
181-
make --jobs=4 # long name with equals sign
182-
make --jobs 4 # long name space delimited
183-
make -j 4 # short name space delimited
184-
make -j4 # short name without space
185-
```
186-
187-
If multiple short arguments are combined into a single term then only one of
188-
those arguments may take a parameter. If the parameterized argument appears as
189-
the final argument in the sequence then the following term will be treated as
190-
its parameter, such as in `make -dj 4`, which is equivalent to `make -d -j 4`.
191-
If the parameterized argument appears in a non-final position within the
192-
sequence then the remainder of the sequence is treated as its parameter, such as
193-
in `make -dj4` which is also equivalent to `make -d -j 4`.
194-
195-
### Manpages
169+
## Manpages
196170

197171
To generate a manpage for a command, run the command with the hidden flag
198172
`--manpage`. This command will print the manpage in the troff format to stdout.

climate.opam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# This file is generated by dune, edit dune-project instead
22
opam-version: "2.0"
33
synopsis: "Declarative command-line parser for OCaml"
4+
description:
5+
"A library for parsing command-line arguments in a declarative style, where the parser spec and assigning parsed arguments to variables in your program is done with the same code. Climate supports CLIs with nested subcommands, and can automatically generate help messages, manpages, and shell completion scripts."
46
maintainer: ["Stephen Sherratt <stephen@sherra.tt>"]
57
authors: ["Stephen Sherratt <stephen@sherra.tt>"]
68
license: "MIT"

dune-project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
(package
1919
(name climate)
2020
(synopsis "Declarative command-line parser for OCaml")
21+
(description
22+
"A library for parsing command-line arguments in a declarative style, where the parser spec and assigning parsed arguments to variables in your program is done with the same code. Climate supports CLIs with nested subcommands, and can automatically generate help messages, manpages, and shell completion scripts.")
2123
(depends
2224
(ocaml
2325
(>= 4.14))))

0 commit comments

Comments
 (0)