Skip to content

Commit 960a631

Browse files
committed
improve the README by Diátaxis framework
1 parent 36558b5 commit 960a631

1 file changed

Lines changed: 97 additions & 44 deletions

File tree

README.md

Lines changed: 97 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,82 +24,135 @@ or add the following to your `project.clj` ([Leiningen](https://leiningen.org/))
2424
```
2525
<!-- /installation -->
2626

27-
## Rationale
28-
29-
This is an opinionated CLI argument handling library. It is meant for command
30-
line tools with subcommands (for example git, which has `git commit`, `git log`
31-
and so forth). It works exactly how we like it, which mostly means it sticks to
32-
common conventions (in particular the prominent GNU conventions), needs little
33-
ceremony, and provides your tool with built-in help facilities automagically.
34-
35-
It is Babashka compatible, and in fact pairs really nicely with `bb` for making
36-
home-grown or general purpose tools.
27+
## Getting Started
3728

38-
It scales from extremely low ceremony basic scripts, to fairly complex setups.
29+
Here, we will explain how to use `com.lambdaisland/cli` to create a simple script that can print out the options it receives and handle basic help.
3930

40-
This library helps you write [Well-behaved Command Line
41-
Tools](https://lambdaisland.com/blog/2020-07-28-well-behaved-command-line-tools)
31+
### Step 1: Create the Basic Command
4232

43-
## Usage
33+
Init the `bb.edn` with
4434

45-
The main entrypoint is `lambdaisland.cli/dispatch`, usually you're fine with the
46-
single-argument version, which defaults to consuming `*command-line-args*`.
35+
```
36+
{:deps {com.lambdaisland/cli {:mvn/version "0.24.97"}}}
37+
```
4738

48-
At its simplest you just pass it a var of a function named the same as your
49-
script (this matters for the help text, as well see below).
39+
Create a file (e.g., `cli-test.bb`):
5040

51-
```clj
41+
```
5242
#!/usr/bin/env bb
5343
5444
(require '[lambdaisland.cli :as cli]
5545
'[clojure.pprint :as pprint])
5646
47+
;; 1. Define your main function.
5748
(defn cli-test
5849
"Ground breaking tool breaks new ground."
5950
[flags]
6051
(pprint/pprint flags))
6152
53+
;; 2. Dispatch the function (using its var).
6254
(cli/dispatch #'cli-test)
6355
```
6456

65-
This is enough to get a basic `--help` output.
57+
Run it:
6658

67-
``` shell
68-
$ cli-test --help
69-
Usage: cli-test [<args>]
59+
```
60+
$ bb cli-test.bb --help
61+
NAME
62+
cli-test —— Ground breaking tool breaks new ground.
7063
71-
Ground breaking tool breaks new ground.
64+
SYNOPSIS
65+
cli-test [<args>...]
7266
```
7367

74-
And you can start passing positional arguments and basic flags to your script,
75-
your function will receive these all as a single map.
68+
By passing a function var (`#'cli-test`), the library automatically infers the name and docstring for help output.
69+
70+
### Step 2: Pass Arguments and Flags
71+
72+
Your command function receives all parsed input as a single map argument, conventionally named flags or opts.
73+
74+
Run it with some input
7675

77-
```shell
78-
$ cli-test --abc hello world --format=txt
79-
{:abc 1, :format "txt", :lambdaisland.cli/argv ["hello" "world"]}
76+
```
77+
$ bb cli-test.bb --abc hello world --format=txt
78+
{:lambdaisland.cli/sources {},
79+
:lambdaisland.cli/argv ["hello" "world"],
80+
:abc 1,
81+
:format "txt"}
8082
```
8183

82-
To do more interesting things we first wrap the var in a map.
84+
Flags beginning with `--` become map keys; remaining values are collected in `:lambdaisland.cli/argv`.
8385

84-
```clj
85-
(cli/dispatch {:command #'cli-test})
86+
### Step 3: Define Custom Flags
87+
88+
To gain control over how flags are parsed and to provide richer help text, we wrap the command var in a configuration map.
89+
90+
Modify your `cli-test.bb` to include a `:flags` configuration:
91+
92+
```
93+
(cli/dispatch
94+
{:command #'cli-test ; The function to call
95+
:flags ["-v, --verbose" "Increases verbosity"
96+
"--input FILE" "Specify the input file"]})
8697
```
8798

88-
`:command` doesn't have to be a var, it can be a simple function, but with a var
89-
the docstring will still be used, as well as the var name, so this is equivalent
90-
to:
99+
Run the updated help:
100+
101+
```
102+
$ bb cli-test.bb --help
103+
NAME
104+
cli-test —— Ground breaking tool breaks new ground.
105+
106+
SYNOPSIS
107+
cli-test [-v | --verbose] [--input FILE] [<args>...]
108+
109+
FLAGS
110+
-v, --verbose Increases verbosity
111+
--input FILE Specify the input file
112+
```
113+
114+
Run the tool with the new flags:
91115

92-
```clj
93-
(cli/dispatch
94-
{:command #'cli-test
95-
:doc "Ground breaking tool breaks new ground."
96-
:name "cli-test"})
97116
```
117+
$ bb cli-test.bb -vvv --input=world.txt
118+
{:lambdaisland.cli/sources
119+
{:verbose "-v command line flag", :input "--input command line flag"},
120+
:lambdaisland.cli/argv [],
121+
:verbose 3,
122+
:input "world.txt"}
123+
```
124+
125+
Multiple short flags like `-vvv` are counted automatically, producing `:verbose 3`.
126+
127+
### Summary
128+
129+
- Step 1: Start with a single command using `cli/dispatch`.
130+
- Step 2: Pass arguments and flags; everything becomes a map.
131+
- Step 3: Add `:flags` for option parsing and richer help.
132+
133+
You now have a solid foundation for building more advanced multi-command tools.
134+
135+
## Rationale
136+
137+
This is an opinionated CLI argument handling library. It is meant for command
138+
line tools with subcommands (for example git, which has `git commit`, `git log`
139+
and so forth). It works exactly how we like it, which mostly means it sticks to
140+
common conventions (in particular the prominent GNU conventions), needs little
141+
ceremony, and provides your tool with built-in help facilities automagically.
142+
143+
It is Babashka compatible, and in fact pairs really nicely with `bb` for making
144+
home-grown or general purpose tools.
145+
146+
It scales from extremely low ceremony basic scripts, to fairly complex setups.
147+
148+
This library helps you write [Well-behaved Command Line
149+
Tools](https://lambdaisland.com/blog/2020-07-28-well-behaved-command-line-tools)
150+
151+
## How-to Guides
98152

99-
### Flags
153+
We can add extra things to the dispatch configuration map, notably `:flags` and `:commands`. We'll explain flags first.
100154

101-
Now we can add extra things to the dispatch configuration map, notably `:flags`
102-
and `:commands`. We'll explain flags first.
155+
### How to Configure Flags for Type Conversion and Defaults
103156

104157
Because you haven't told lambdaisland/cli about the flags your script
105158
understands, it has to guess how to handle them. If you have arguments like
@@ -286,7 +339,7 @@ must always be passed:
286339
- `:coll?` flag can be specified multiple times, will result in a vector
287340
- `:parse` function used to coerce the flag argument
288341

289-
### Commands
342+
### How to Build a Multi-Command CLI Tool
290343

291344
`lambdaisland/cli` is specifically meant for CLI tools with multiple subcommands
292345
(and sub-sub-commands, and so forth). In this way it forms an appealing

0 commit comments

Comments
 (0)