dry-cli-help 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8756ab593e09c6cdc0e0ab32fe6f3909d05625734a07e7d7c334e3d832ba70ac
4
- data.tar.gz: 4477df389d4c4c0d984fe4024f768adf3e254772464694f7acd81609936060a4
3
+ metadata.gz: 603fe4d7d4f905ffd720b33ce12f7847ce9d61658935a68609444ea35de0afed
4
+ data.tar.gz: 5ba0fe8b6319be69b6308f4613afbbaf56b818fdbed8ff1844b790e83d47cb74
5
5
  SHA512:
6
- metadata.gz: 6a61174289b02e6a523ba5ccdae1a79cd72241ea7c5bb16de5d543e3aad488a3de9d973644a0297c2c94e0516475241c490470f5d52208f450d7efc243dfd575
7
- data.tar.gz: 041553f6adda56086400f208d6bef2ce77613e635581e871ffc58488a3ead1cca4038c3ce4d89f39e79049f529ffaa7cb013dea09bea8701e9c7b5c78ea1d3eb
6
+ metadata.gz: cf16c7662f59e52d41f97569be72627dd946af8f0e13f19563f202daa6576895c3d594e5114ab2980830f2b0943b47bd6a1c17b8a45ae121e7f62673b8b594a5
7
+ data.tar.gz: 268c5be6c337e75f509e557d85e5e78ac9e72799b8fbca949ec729a3e8c34dc05e129e97c5f4eb8c37e7d8a7c769347d13e67f5b82b7a30a283e37b290320444
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ - Every setting is made once, in `Dry::CLI::Help.configure`, which now also accepts a block without an argument and runs it against the configuration. The `help` block on registries is removed, along with `Help.config_for` and `Configuration#merge`: the gem adds nothing to dry-cli's registry or command DSL.
4
+ - A `styles` block declares every element's look in one place, replacing `style(element, *names)`. New elements `usage`, `example` and `example_comment`; `comment` is renamed `example_comment` and defaults to bold black.
5
+ - Heading case moves into the heading style: `styles { heading :bold, case: :Capitalize }`, one of `:UPPERCASE`, `:Capitalize`, `:lowercase` or `:as_is`. The `heading_case` setting is removed.
6
+ - `examples/` holds three single-file dry-cli tools that switch this gem on with `--with-dry-cli-help` or `-w`.
7
+
3
8
  ## [0.1.0] - 2026-09-12
4
9
 
5
10
  - Initial release as `dry-cli-help`, converted from `dry-cli-autocomplete`. The shell completion generator is removed; that gem remains the place for it.
data/README.md CHANGED
@@ -11,14 +11,14 @@ dry-cli prints help as it finds it: no title, no description of the program, no
11
11
 
12
12
  ## Before and after
13
13
 
14
- `taxlibris compile -h` with dry-cli alone:
14
+ `my-cli compile -h` with dry-cli alone:
15
15
 
16
16
  ```text
17
17
  Command:
18
- taxlibris compile
18
+ my-cli compile
19
19
 
20
20
  Usage:
21
- taxlibris compile RULES [OUTPUT]
21
+ my-cli compile RULES [OUTPUT]
22
22
 
23
23
  Description:
24
24
  Compile tax rules
@@ -37,7 +37,7 @@ With `require "dry/cli/help"`:
37
37
 
38
38
  ```text
39
39
  USAGE
40
- taxlibris compile RULES [OUTPUT] [OPTIONS]
40
+ my-cli compile RULES [OUTPUT] [OPTIONS]
41
41
 
42
42
  DESCRIPTION
43
43
  Compile tax rules
@@ -54,6 +54,13 @@ OPTIONS
54
54
 
55
55
  Headings are bold and yellow, commands green, options and arguments cyan, and every description wraps to the terminal with a hanging indent.
56
56
 
57
+ Although, it is best to show them side by side as a screenshot: this script `rbcheck` is in the `examples` folder of the gem.
58
+
59
+ | Standard dry-cli Help Screen | Require `dry/cli/help` |
60
+ | :---------------------------------------------- | :--------------------------------------- |
61
+ | ![original](docs/img/without-dry-cli-help.avif) | ![with](docs/img/with-dry-cli-help.avif) |
62
+ | | |
63
+
57
64
  ## Installation
58
65
 
59
66
  ```bash
@@ -71,26 +78,26 @@ require "dry/cli"
71
78
  require "dry/cli/help"
72
79
  ```
73
80
 
74
- Describe the program in the registry:
81
+ Then make every setting once, in one block, before the CLI runs. Your registry, commands and options stay plain dry-cli: the gem adds nothing to them, so you can add or remove it without touching a command.
75
82
 
76
83
  ```ruby
77
- module Taxlibris
78
- module CLI
79
- extend Dry::CLI::Registry
84
+ Dry::CLI::Help.configure do
85
+ title "MyCLI"
80
86
 
81
- help do
82
- title "Taxlibris"
87
+ description <<~TEXT
88
+ This utility does something very important.
89
+ TEXT
83
90
 
84
- description <<~TEXT
85
- Compile, validate, and evaluate tax rules.
86
- TEXT
91
+ epilogue "Documentation: https://example.com/my-cli"
87
92
 
88
- epilogue "Documentation: https://example.com/taxlibris"
93
+ color :auto
94
+ width :terminal
95
+ wrap true
96
+ end
89
97
 
90
- color :auto
91
- width :terminal
92
- wrap true
93
- end
98
+ module My
99
+ module CLI
100
+ extend Dry::CLI::Registry
94
101
 
95
102
  register "compile", Compile
96
103
  register "validate", Validate
@@ -100,15 +107,15 @@ module Taxlibris
100
107
  end
101
108
  ```
102
109
 
103
- `taxlibris -h` then prints:
110
+ `my-cli -h` then prints:
104
111
 
105
112
  ```text
106
- Taxlibris
113
+ MyCLI
107
114
 
108
- Compile, validate, and evaluate tax rules.
115
+ Compile, validate, and evaluate rules.
109
116
 
110
117
  USAGE
111
- taxlibris COMMAND [OPTIONS]
118
+ my-cli COMMAND [OPTIONS]
112
119
 
113
120
  COMMANDS
114
121
  compile Compile tax rules
@@ -120,12 +127,12 @@ OPTIONS
120
127
  -h, --help Show help
121
128
  -v, --version Show version
122
129
 
123
- Documentation: https://example.com/taxlibris
130
+ Documentation: https://example.com/my-cli
124
131
  ```
125
132
 
126
133
  A command reachable as `--version` lists under Options by its dashed names.
127
134
 
128
- Settings for the whole process go through `configure`. A registry's `help` block overrides them:
135
+ A block that takes an argument receives the configuration instead of running against it:
129
136
 
130
137
  ```ruby
131
138
  Dry::CLI::Help.configure do |config|
@@ -134,23 +141,177 @@ Dry::CLI::Help.configure do |config|
134
141
  end
135
142
  ```
136
143
 
137
- A `help` block that takes an argument receives the configuration instead, so `help { |h| h.title = "Taxlibris" }` works too.
144
+ ## Examples
145
+
146
+ The [`examples`](examples) folder holds three single-file tools built on dry-cli. Each one loads and configures this gem only when you pass `--with-dry-cli-help`, or `-w` for short, so you can run the same command both ways and compare:
147
+
148
+ ```bash
149
+ examples/rbcheck -h # dry-cli's own help
150
+ examples/rbcheck -h --with-dry-cli-help # the same help, through this gem
151
+ ```
152
+
153
+ `examples/rbcheck` has two commands, `version` and `check-ruby-syntax [DIR]`. Condensed:
154
+
155
+ ```ruby
156
+ #!/usr/bin/env ruby
157
+ require "dry/cli"
158
+
159
+ if [ARGV.delete("--with-dry-cli-help"), ARGV.delete("-w")].any?
160
+ require "dry/cli/help"
161
+
162
+ Dry::CLI::Help.configure do
163
+ title "RBCheck"
164
+ description "Small checks for Ruby projects."
165
+ epilogue "Report bugs at https://example.com/rbcheck/issues"
166
+ end
167
+ end
168
+
169
+ module Rbcheck
170
+ extend Dry::CLI::Registry
171
+
172
+ class Version < Dry::CLI::Command
173
+ desc "Print the version"
174
+
175
+ def call(**) = puts("rbcheck 1.0.0")
176
+ end
177
+
178
+ class CheckRubySyntax < Dry::CLI::Command
179
+ desc "Check every Ruby file under a directory for syntax errors, and report "\
180
+ "each file that fails to parse with its line number"
181
+
182
+ argument :dir, desc: "Directory to scan", default: "."
183
+ option :exclude, type: :array, desc: "Glob patterns to skip, such as vendor/**"
184
+ option :quiet, type: :boolean, default: false, aliases: ["-q"],
185
+ desc: "Print only the files that fail"
186
+
187
+ example ["lib # check one directory", "--exclude=vendor/** # skip vendored gems"]
188
+
189
+ def call(dir:, quiet:, exclude: [], **)
190
+ # ... parses every file under dir with Prism ...
191
+ end
192
+ end
193
+
194
+ register "version", Version, aliases: ["--version", "-v"]
195
+ register "check-ruby-syntax", CheckRubySyntax
196
+ end
197
+
198
+ Dry::CLI.new(Rbcheck).call
199
+ ```
200
+
201
+ Every screen below comes from running it in an 80-column terminal.
202
+
203
+ ### `rbcheck -h`
204
+
205
+ dry-cli alone prints a list of commands and exits 1. Also note the lack of wrapping on the long description.
206
+
207
+ ```text
208
+ Commands:
209
+ rbcheck check-ruby-syntax [DIR] # Check every Ruby file under a directory for syntax errors, and report each file that fails to parse with its line number
210
+ rbcheck version # Print the version
211
+ ```
212
+
213
+ With this gem in play it exits 0 and prints:
214
+
215
+ ```text
216
+ RB check
217
+
218
+ Small checks for Ruby projects.
219
+
220
+ USAGE
221
+ rbcheck COMMAND [OPTIONS]
222
+
223
+ COMMANDS
224
+ version Print the version
225
+ check-ruby-syntax Check every Ruby file under a directory for syntax errors,
226
+ and report each file that fails to parse with its line
227
+ number
228
+
229
+ OPTIONS
230
+ -h, --help Show help
231
+ -v, --version Print the version
232
+
233
+ Report bugs at https://example.com/rbcheck/issues
234
+ ```
235
+
236
+ - The title, description and epilogue come from the `configure` block.
237
+ - Commands list in the order they were registered, not alphabetically.
238
+ - The long description wraps to the terminal under its own column.
239
+ - `version` is also reachable as `--version` and `-v`, so it lists under Options too. dry-cli never shows those aliases.
240
+
241
+ ### `rbcheck check-ruby-syntax -h`
242
+
243
+ dry-cli alone:
244
+
245
+ ```text
246
+ Command:
247
+ rbcheck check-ruby-syntax
248
+
249
+ Usage:
250
+ rbcheck check-ruby-syntax [DIR]
251
+
252
+ Description:
253
+ Check every Ruby file under a directory for syntax errors, and report each file that fails to parse with its line number
254
+
255
+ Arguments:
256
+ DIR # Directory to scan
257
+
258
+ Options:
259
+ --exclude=VALUE1,VALUE2,.. # Glob patterns to skip, such as vendor/**
260
+ --[no-]quiet, -q # Print only the files that fail, default: false
261
+ --help, -h # Print this help
262
+
263
+ Examples:
264
+ rbcheck check-ruby-syntax lib # check one directory
265
+ rbcheck check-ruby-syntax --exclude=vendor/** # skip vendored gems
266
+ ```
267
+
268
+ With this gem:
269
+
270
+ ```text
271
+ USAGE
272
+ rbcheck check-ruby-syntax [DIR] [OPTIONS]
273
+
274
+ DESCRIPTION
275
+ Check every Ruby file under a directory for syntax errors, and report each
276
+ file that fails to parse with its line number
277
+
278
+ ARGUMENTS
279
+ DIR Directory to scan (default: ".")
280
+
281
+ OPTIONS
282
+ --exclude=VALUE1,VALUE2,.. Glob patterns to skip, such as vendor/**
283
+ -q, --[no-]quiet Print only the files that fail (default: false)
284
+ -h, --help Show help
285
+
286
+ EXAMPLES
287
+ rbcheck check-ruby-syntax lib check one directory
288
+ rbcheck check-ruby-syntax --exclude=vendor/**
289
+ skip vendored gems
290
+ ```
291
+
292
+ - The `Command:` section repeats the usage line, so it is gone, and the usage line shows that the command takes options.
293
+ - The argument's default of `"."` shows up; dry-cli leaves it out.
294
+ - Short aliases come first (`-q, --[no-]quiet`), and every description lines up in one column instead of trailing a `#`.
295
+ - Each example's comment moves into a column of its own. An example longer than that column puts its comment on the next line.
296
+
297
+ In a terminal the headings print bold yellow, usage lines, commands and examples green, arguments and options cyan, and example comments bold black.
298
+
299
+ `examples/todo` shows nested commands, custom headings and a `styles` block. `examples/deploy` shows a fixed width, a banner above command help, reordered sections and help without a command exiting 0. [`examples/README.md`](examples/README.md) lists what each one demonstrates.
138
300
 
139
301
  ## Settings
140
302
 
141
- | Setting | Values | Default |
142
- | ----------------------------- | --------------------------------- | --------------- |
143
- | `title` | String | none |
144
- | `description` | String | none |
145
- | `epilogue` | String | none |
146
- | `color` | `true`, `false`, `:auto` | `:auto` |
147
- | `wrap` | `true`, `false` | `true` |
148
- | `width` | `:terminal`, Integer | `:terminal` |
149
- | `margin` | Integer | `0` |
150
- | `exit_code_without_arguments` | 0 to 255 | `1` |
151
- | `banner_on_subcommands` | `true`, `false` | `false` |
152
- | `heading_case` | `:upcase`, `:capitalize`, `:none` | `:upcase` |
153
- | `command_order` | `:registration`, `:alphabetical` | `:registration` |
303
+ | Setting | Values | Default | What it does |
304
+ | ----------------------------- | -------------------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------- |
305
+ | `title` | String | none | Prints a bold first line at the top of the top-level help |
306
+ | `description` | String | none | Prints paragraphs under the title, reflowed to the wrap width |
307
+ | `epilogue` | String | none | Prints paragraphs at the very end of the top-level help |
308
+ | `color` | `true`, `false`, `:auto` | `:auto` | Paints headings, commands, arguments and options; `:auto` paints only a terminal |
309
+ | `wrap` | `true`, `false` | `true` | Wraps descriptions with a hanging indent; `false` prints each one on a single line as written |
310
+ | `width` | `:terminal`, Integer | `:terminal` | Sets the column text wraps at; `:terminal` follows the terminal's width |
311
+ | `margin` | Integer | `0` | Keeps that many columns free at the right edge when `width` is `:terminal` |
312
+ | `exit_code_without_arguments` | 0 to 255 | `1` | Sets the exit status of `my-cli` or `my-cli db` run with no command; `0` also prints the help to stdout instead of stderr |
313
+ | `banner_on_subcommands` | `true`, `false` | `false` | Prints the title and description above command help and group listings too, not only above the top-level help |
314
+ | `command_order` | `:registration`, `:alphabetical` | `:registration` | Lists commands in the order you registered them, or sorted by name as dry-cli does |
154
315
 
155
316
  `color :auto` colors a terminal and honors [`NO_COLOR`](https://no-color.org). `width :terminal` reads `COLUMNS`, then the console, then falls back to 80, and `margin` keeps columns free at the right edge.
156
317
 
@@ -159,9 +320,8 @@ Running the program with no command prints the top-level help and exits 1, as dr
159
320
  ### Headings, sections and groups
160
321
 
161
322
  ```ruby
162
- help do
323
+ Dry::CLI::Help.configure do
163
324
  heading :commands, "Available commands"
164
- heading_case :capitalize
165
325
 
166
326
  group "Rules", "compile", "validate"
167
327
  group "Returns", "evaluate"
@@ -171,7 +331,7 @@ help do
171
331
  end
172
332
  ```
173
333
 
174
- - `heading` replaces one section's heading text.
334
+ - `heading` replaces one section's heading text. How headings are cased belongs to their style, below.
175
335
  - `group` lists commands under a heading of their own, in the order given. Ungrouped commands stay under Commands. A group inside a group names the full path, such as `"db migrate"`.
176
336
  - `sections` sets the order; a section left out is hidden. `hide` hides sections without restating the order.
177
337
 
@@ -179,14 +339,37 @@ The sections are `banner`, `usage`, `description`, `commands`, `subcommands`, `a
179
339
 
180
340
  ### Styles
181
341
 
342
+ Every element's look is declared together, in one `styles` block inside `configure`:
343
+
182
344
  ```ruby
183
- help do
184
- style :heading, :bold, :bright_blue
185
- style :comment # no styles: print it plain
345
+ Dry::CLI::Help.configure do
346
+ styles do
347
+ title :bold
348
+ heading :bold, :yellow, case: :UPPERCASE
349
+ usage :green
350
+ command :green
351
+ argument :cyan
352
+ option :cyan
353
+ example :green
354
+ example_comment :bold, :black
355
+ end
186
356
  end
187
357
  ```
188
358
 
189
- The styled elements are `title`, `heading`, `command`, `argument`, `option` and `comment`, the last being the part of an example after the first `#` surrounded by spaces, as in `"rules.form # compile one file"`.
359
+ Those are the defaults. Name only the elements you want to change; a line with no styles, such as `example_comment`, prints that element plain.
360
+
361
+ | Element | Applies to |
362
+ | ----------------- | ---------------------------------------------------------------------------------------------------- |
363
+ | `title` | The banner title |
364
+ | `heading` | Every section and group heading |
365
+ | `usage` | Every usage line, whole |
366
+ | `command` | Command names in a list |
367
+ | `argument` | Argument names |
368
+ | `option` | Option names |
369
+ | `example` | The command line of an example |
370
+ | `example_comment` | The part of an example after the first `#` surrounded by spaces, as in `"lib # check one directory"` |
371
+
372
+ `heading` alone takes `case:`, one of `:UPPERCASE`, `:Capitalize`, `:lowercase` or `:as_is`, each written the way it cases. `:Capitalize` raises only the first letter. `heading case: :as_is` with no styles changes the case and keeps the colors.
190
373
 
191
374
  ### The Colors module
192
375
 
@@ -210,16 +393,26 @@ The methods are the eight colors `black red green yellow blue magenta cyan white
210
393
  The gem prepends one module to `Dry::CLI`, overriding the two private methods dry-cli prints help from. It does not replace `Dry::CLI::Banner` or `Dry::CLI::Usage`.
211
394
 
212
395
  ```mermaid
213
- flowchart LR
214
- argv[ARGV] --> call["Dry::CLI#call"]
215
- call -->|"command found, -h given"| help["#help"]
216
- call -->|"no command, a group, -h at a level, a typo"| spell["#spell_checker"]
217
- help --> command[Screens::Command]
218
- spell --> listing[Screens::Listing]
219
- command --> formatter[Formatter]
220
- listing --> formatter
221
- config["Help.configure + registry help block"] --> formatter
222
- formatter --> out[stdout or stderr]
396
+ ---
397
+ config:
398
+ layout: elk
399
+ theme: forest
400
+ ---
401
+ flowchart TB
402
+ argv["ARGV"] --> cli_call["Dry::CLI#call"]
403
+
404
+ cli_call -->|"command found, --help given"| help_method["#help"]
405
+ cli_call -->|"no command, a group, -h at a level, a typo"| spell_checker["#spell_checker"]
406
+
407
+ help_method --> command_screen["Screens::Command"]
408
+ spell_checker --> listing_screen["Screens::Listing"]
409
+
410
+ command_screen --> formatter["Formatter"]
411
+ listing_screen --> formatter
412
+
413
+ help_config["Help.configure + registry help block"] --> formatter
414
+
415
+ formatter --> output["stdout or stderr"]
223
416
  ```
224
417
 
225
418
  Both methods are `@api private` in dry-cli. `spec/dry/cli/help/dry_cli_contract_spec.rb` asserts every internal the gem reads, so a dry-cli release that moves one fails this suite, naming what moved.
data/SPECIFICATION.md CHANGED
@@ -27,46 +27,46 @@ It does not replace `dry-cli` or introduce another command framework. It takes t
27
27
 
28
28
  ## Example
29
29
 
30
+ Every setting is made once, in one block, before any CLI runs. The registry, its commands and their options stay exactly as dry-cli declares them:
31
+
30
32
  ```ruby
31
33
  require "dry/cli"
32
34
  require "dry/cli/help"
33
35
 
34
- Dry::CLI::Help.configure do |config|
35
- config.width = :terminal
36
- config.wrap = true
37
- config.color = true
38
- end
39
- ```
36
+ Dry::CLI::Help.configure do
37
+ title "MyCLI"
40
38
 
41
- An application could provide richer top-level help:
39
+ description <<~TEXT
40
+ Compile, validate, and evaluate tax rules.
41
+ TEXT
42
42
 
43
- ```ruby
44
- class CLI
45
- extend Dry::CLI::Registry
43
+ color :auto
44
+ width :terminal
46
45
 
47
- help do
48
- title "Taxlibris"
46
+ styles do
47
+ heading :bold, :yellow, case: :UPPERCASE
48
+ example_comment :bold, :black
49
+ end
50
+ end
49
51
 
50
- description <<~TEXT
51
- Compile, validate, and evaluate tax rules.
52
- TEXT
52
+ module CLI
53
+ extend Dry::CLI::Registry # plain dry-cli, nothing added
53
54
 
54
- color true
55
- width :terminal
56
- wrap true
57
- end
55
+ register "compile", Compile
56
+ register "validate", Validate
57
+ register "evaluate", Evaluate
58
58
  end
59
59
  ```
60
60
 
61
61
  Result:
62
62
 
63
63
  ```text
64
- Taxlibris
64
+ MyCLI
65
65
 
66
66
  Compile, validate, and evaluate tax rules.
67
67
 
68
68
  USAGE
69
- taxlibris COMMAND [OPTIONS]
69
+ my-cli COMMAND [OPTIONS]
70
70
 
71
71
  COMMANDS
72
72
  compile Compile tax rules
@@ -125,32 +125,31 @@ Requiring `dry/cli/help` changes help output for every `Dry::CLI` in the process
125
125
 
126
126
  Both are `@api private` in dry-cli 1.4.1. A spec asserts they exist, so a dry-cli release that renames them fails this gem's suite rather than a host's help screen. `Dry::CLI::Help::Integration` holds the override.
127
127
 
128
- `help` is added to `Dry::CLI::Registry`, so any module or class that extends a registry can call it.
128
+ The gem adds nothing to `Dry::CLI::Registry` or `Dry::CLI::Command`. The DSL a host uses to declare commands, arguments, options and examples is dry-cli's, unchanged, so adding or removing this gem never touches a command definition.
129
129
 
130
130
  ### Configuration
131
131
 
132
- Two levels, one vocabulary. `Dry::CLI::Help.configure` sets process-wide values. A registry's `help` block overrides them for that registry. A setting neither level sets takes the default below.
133
-
134
- Every setting reads and writes both ways: `title "Taxlibris"` inside a `help` block, and `config.title = "Taxlibris"` on the yielded object. A `help` block taking one argument receives the configuration instead of being evaluated against it.
135
-
136
- | Setting | Values | Default | Effect |
137
- | ----------------------------- | ------------------------------------ | --------------- | ---------------------------------------------------------------------- |
138
- | `title` | String | none | First line of the banner |
139
- | `description` | String | none | Paragraphs under the title, wrapped |
140
- | `epilogue` | String | none | Paragraphs at the end of the top-level help |
141
- | `color` | `true`, `false`, `:auto` | `:auto` | `:auto` colors a terminal and honors `NO_COLOR` |
142
- | `wrap` | `true`, `false` | `true` | `false` prints descriptions as written |
143
- | `width` | `:terminal`, Integer | `:terminal` | The column text wraps at |
144
- | `margin` | Integer | `0` | Columns kept free at the right edge when `width` is `:terminal` |
145
- | `exit_code_without_arguments` | Integer, 0 to 255 | `1` | Status for `mycli` or `mycli group` with no command |
146
- | `banner_on_subcommands` | `true`, `false` | `false` | Print the title and description above `mycli command -h` |
147
- | `heading_case` | `:upcase`, `:capitalize`, `:none` | `:upcase` | How every heading is cased; `:capitalize` raises only the first letter |
148
- | `command_order` | `:registration`, `:alphabetical` | `:registration` | Order commands list in; dry-cli sorts alphabetically |
149
- | `heading(section, text)` | Section name, String | see below | Replaces one heading's text |
150
- | `style(element, *styles)` | Element name, `Colors::STYLES` names | see below | Replaces one element's styles |
151
- | `group(name, *commands)` | String, command paths | none | Lists those commands under their own heading, in declaration order |
152
- | `sections(*names)` | Section names | all | Order of sections; a section left out is hidden |
153
- | `hide(*names)` | Section names | none | Hides sections without restating the order |
132
+ One place. `Dry::CLI::Help.configure` holds every setting for the process, and every CLI in the process renders with it. A setting it leaves alone takes the default below.
133
+
134
+ The block reads and writes both ways: `title "MyCLI"` when the block takes no argument and runs against the configuration, and `config.title = "MyCLI"` when it takes one and receives it.
135
+
136
+ | Setting | Values | Default | Effect |
137
+ | ----------------------------- | -------------------------------- | --------------- | ------------------------------------------------------------------ |
138
+ | `title` | String | none | First line of the banner |
139
+ | `description` | String | none | Paragraphs under the title, wrapped |
140
+ | `epilogue` | String | none | Paragraphs at the end of the top-level help |
141
+ | `color` | `true`, `false`, `:auto` | `:auto` | `:auto` colors a terminal and honors `NO_COLOR` |
142
+ | `wrap` | `true`, `false` | `true` | `false` prints descriptions as written |
143
+ | `width` | `:terminal`, Integer | `:terminal` | The column text wraps at |
144
+ | `margin` | Integer | `0` | Columns kept free at the right edge when `width` is `:terminal` |
145
+ | `exit_code_without_arguments` | Integer, 0 to 255 | `1` | Status for `mycli` or `mycli group` with no command |
146
+ | `banner_on_subcommands` | `true`, `false` | `false` | Print the title and description above `mycli command -h` |
147
+ | `command_order` | `:registration`, `:alphabetical` | `:registration` | Order commands list in; dry-cli sorts alphabetically |
148
+ | `heading(section, text)` | Section name, String | see below | Replaces one heading's text |
149
+ | `styles { ... }` | One line per element | see below | Declares how every element looks, and how headings are cased |
150
+ | `group(name, *commands)` | String, command paths | none | Lists those commands under their own heading, in declaration order |
151
+ | `sections(*names)` | Section names | all | Order of sections; a section left out is hidden |
152
+ | `hide(*names)` | Section names | none | Hides sections without restating the order |
154
153
 
155
154
  Terminal width comes from `COLUMNS`, then the console, then 80. A resolved wrap width never falls below 20 columns.
156
155
 
@@ -170,7 +169,7 @@ In default order, with default headings:
170
169
  | `examples` | Examples | no | yes |
171
170
  | `epilogue` | none | yes | only for a single command |
172
171
 
173
- A single command passed to `Dry::CLI.new(SomeCommand)` is the whole program, so its command help prints the banner and the epilogue too. It has no registry, so it renders with the process-wide settings alone.
172
+ A single command passed to `Dry::CLI.new(SomeCommand)` is the whole program, so its command help prints the banner and the epilogue too.
174
173
 
175
174
  A group listing, `mycli db` where `db` has no command of its own, renders as top-level help scoped to the group, with the banner only when `banner_on_subcommands` is set, and no epilogue.
176
175
 
@@ -178,16 +177,30 @@ Every Options section starts with `-h, --help Show help`, because both spelling
178
177
 
179
178
  At any level, a command registered under a name or alias starting with `-`, such as `register "version", Version, aliases: ["--version"]`, lists under Options rather than Commands.
180
179
 
181
- ### Styled elements
180
+ ### Styles
181
+
182
+ Every element's look is declared in one `styles` block inside `configure`. Each line names an element and its styles, from `Colors::STYLES`. An element left out keeps its default; a line with no styles prints the element plain.
183
+
184
+ ```ruby
185
+ styles do
186
+ heading :bold, :blue, case: :Capitalize
187
+ example :yellow
188
+ example_comment # plain
189
+ end
190
+ ```
182
191
 
183
- | Element | Default styles | Applies to |
184
- | ---------- | ---------------- | -------------------------------------- |
185
- | `title` | `bold` | The banner title |
186
- | `heading` | `bold`, `yellow` | Every section heading |
187
- | `command` | `green` | Command names and the program in usage |
188
- | `argument` | `cyan` | Argument names |
189
- | `option` | `cyan` | Option names |
190
- | `comment` | `bright_black` | The comment half of an example |
192
+ | Element | Default styles | Applies to |
193
+ | ----------------- | ---------------- | ------------------------------------------ |
194
+ | `title` | `bold` | The banner title |
195
+ | `heading` | `bold`, `yellow` | Every section and group heading |
196
+ | `usage` | `green` | Every usage line, whole |
197
+ | `command` | `green` | Command names in a list |
198
+ | `argument` | `cyan` | Argument names |
199
+ | `option` | `cyan` | Option names |
200
+ | `example` | `green` | The command line of an example |
201
+ | `example_comment` | `bold`, `black` | The part of an example after the first `#` |
202
+
203
+ `heading` alone takes `case:`, one of `:UPPERCASE` (the default), `:Capitalize`, `:lowercase` or `:as_is`, each written the way it cases. It applies to every heading. `:Capitalize` raises only the first letter and leaves the rest as written. `heading case: :as_is` with no styles changes the case and keeps the heading's styles.
191
204
 
192
205
  ### Layout
193
206
 
@@ -3,19 +3,18 @@
3
3
  module Dry
4
4
  class CLI
5
5
  module Help
6
- # Help settings, readable and writable two ways:
6
+ # Every help setting, made once for the whole process, two ways:
7
7
  #
8
- # help do
9
- # title "Taxlibris" # inside a registry's help block
8
+ # Dry::CLI::Help.configure do
9
+ # title "MyCLI" # as a DSL
10
10
  # end
11
11
  #
12
12
  # Dry::CLI::Help.configure do |config|
13
- # config.title = "Taxlibris" # on the yielded object
13
+ # config.title = "MyCLI" # on the yielded object
14
14
  # end
15
15
  #
16
- # An instance stores only what was set on it. Everything else reads from
17
- # {DEFAULTS}, which is what lets a registry's settings sit over the
18
- # process-wide ones through {#merge} without either copying the other.
16
+ # Every element's look is declared in one {#styles} block. Anything not
17
+ # set reads from {DEFAULTS}, {HEADINGS} or {STYLES}.
19
18
  class Configuration
20
19
  # Every section, in default order.
21
20
  SECTIONS = %i[
@@ -37,13 +36,18 @@ module Dry
37
36
  STYLES = {
38
37
  title: %i[bold],
39
38
  heading: %i[bold yellow],
39
+ usage: %i[green],
40
40
  command: %i[green],
41
41
  argument: %i[cyan],
42
42
  option: %i[cyan],
43
- comment: %i[bright_black]
43
+ example: %i[green],
44
+ example_comment: %i[bold black]
44
45
  }.freeze
45
46
 
46
- HEADING_CASES = %i[upcase capitalize none].freeze
47
+ # How headings can be cased; each name is written the way it cases.
48
+ # `:Capitalize` raises only the first letter.
49
+ HEADING_CASES = %i[UPPERCASE Capitalize lowercase as_is].freeze
50
+ DEFAULT_HEADING_CASE = :UPPERCASE
47
51
  COMMAND_ORDERS = %i[registration alphabetical].freeze
48
52
 
49
53
  # Narrowest column text wraps at, however small the terminal.
@@ -63,7 +67,6 @@ module Dry
63
67
  margin: ->(value) { value.is_a?(Integer) && !value.negative? },
64
68
  exit_code_without_arguments: ->(value) { value.is_a?(Integer) && value.between?(0, 255) },
65
69
  banner_on_subcommands: BOOLEAN,
66
- heading_case: ->(value) { HEADING_CASES.include?(value) },
67
70
  command_order: ->(value) { COMMAND_ORDERS.include?(value) }
68
71
  }.freeze
69
72
 
@@ -77,7 +80,6 @@ module Dry
77
80
  margin: 0,
78
81
  exit_code_without_arguments: 1,
79
82
  banner_on_subcommands: false,
80
- heading_case: :upcase,
81
83
  command_order: :registration
82
84
  }.freeze
83
85
 
@@ -122,26 +124,36 @@ module Dry
122
124
  HEADINGS.merge(@values.fetch(:headings, {}))
123
125
  end
124
126
 
125
- # Replace the styles of one element. No styles prints it plain.
127
+ # Declare how elements look, all in one place. Each line names an
128
+ # element from {STYLES} and its styles from {Colors::STYLES}; no styles
129
+ # prints it plain. Elements left out keep their defaults. `heading` also
130
+ # takes `case:`, one of {HEADING_CASES}. A block taking an argument
131
+ # receives the declarations instead.
132
+ #
133
+ # @example
134
+ # styles do
135
+ # heading :bold, :blue, case: :Capitalize
136
+ # example_comment # plain
137
+ # end
126
138
  #
127
- # @param element [Symbol] a key of {STYLES}
128
- # @param names [Array<Symbol>] names from {Colors::STYLES}
129
- # @return [Array<Symbol>]
130
- def style(element, *names)
131
- raise ArgumentError, "#{element.inspect} is not a styled element" unless STYLES.key?(element)
132
-
133
- unknown = names - Colors::STYLES
134
- raise ArgumentError, "unknown styles: #{unknown.inspect}" unless unknown.empty?
135
-
136
- @values[:styles] = @values.fetch(:styles, {}).merge(element => names.freeze)
137
- names
138
- end
139
-
140
139
  # @return [Hash{Symbol => Array<Symbol>}] every element's styles
141
- def styles
140
+ def styles(&block)
141
+ if block
142
+ sheet = StyleSheet.new
143
+ block.arity == 1 ? yield(sheet) : sheet.instance_eval(&block)
144
+ @values[:styles] = @values.fetch(:styles, {}).merge(sheet.styles)
145
+ @values[:heading_case] = sheet.heading_case if sheet.heading_case
146
+ end
142
147
  STYLES.merge(@values.fetch(:styles, {}))
143
148
  end
144
149
 
150
+ # How every heading is cased, set by `heading ..., case:` in {#styles}.
151
+ #
152
+ # @return [Symbol] one of {HEADING_CASES}
153
+ def heading_case
154
+ @values.fetch(:heading_case, DEFAULT_HEADING_CASE)
155
+ end
156
+
145
157
  # List commands under a heading of their own, in the order given.
146
158
  # Groups print in the order they are declared, after ungrouped commands.
147
159
  #
@@ -206,27 +218,6 @@ module Dry
206
218
  [columns, MIN_WIDTH].max
207
219
  end
208
220
 
209
- # These settings with another instance's laid over them. Headings and
210
- # styles merge key by key, hidden sections add up, and any other setting
211
- # the other instance made replaces this one's.
212
- #
213
- # @param other [Configuration]
214
- # @return [Configuration] a new instance
215
- def merge(other)
216
- combined = @values.merge(other.values) do |key, mine, theirs|
217
- case key
218
- when :headings, :styles then mine.merge(theirs)
219
- when :hidden then mine | theirs
220
- else theirs
221
- end
222
- end
223
- self.class.new(combined)
224
- end
225
-
226
- protected
227
-
228
- attr_reader :values
229
-
230
221
  private
231
222
 
232
223
  def validate_sections(names)
@@ -235,6 +226,48 @@ module Dry
235
226
 
236
227
  names.freeze
237
228
  end
229
+
230
+ # What a {Configuration#styles} block runs against: one method per
231
+ # element, each validating what it is given.
232
+ class StyleSheet
233
+ # @return [Hash{Symbol => Array<Symbol>}] the elements declared
234
+ attr_reader :styles
235
+
236
+ # @return [Symbol, nil] the heading case, when declared
237
+ attr_reader :heading_case
238
+
239
+ def initialize
240
+ @styles = {}
241
+ end
242
+
243
+ STYLES.each_key do |element|
244
+ define_method(element) do |*names, **options|
245
+ declare(element, names.flatten, options)
246
+ end
247
+ end
248
+
249
+ private
250
+
251
+ def declare(element, names, options)
252
+ unknown = names - Colors::STYLES
253
+ raise ArgumentError, "unknown styles for #{element}: #{unknown.inspect}" unless unknown.empty?
254
+
255
+ @heading_case = letter_case(element, options) unless options.empty?
256
+ # `heading case: :as_is` alone changes the case and keeps the styles.
257
+ @styles[element] = names.freeze unless names.empty? && !options.empty?
258
+ end
259
+
260
+ def letter_case(element, options)
261
+ unless element == :heading && options.keys == [:case]
262
+ raise ArgumentError, "#{element} takes no #{options.keys.map { "#{it}:" }.join(', ')}"
263
+ end
264
+
265
+ value = options.fetch(:case)
266
+ return value if HEADING_CASES.include?(value)
267
+
268
+ raise ArgumentError, "case cannot be #{value.inspect}; use one of #{HEADING_CASES.inspect}"
269
+ end
270
+ end
238
271
  end
239
272
  end
240
273
  end
@@ -49,12 +49,15 @@ module Dry
49
49
  title(config.headings.fetch(section))
50
50
  end
51
51
 
52
- # A heading with the configured case and style, for any text.
52
+ # A heading in the configured case and style, for any text.
53
+ #
54
+ # @param text [String]
53
55
  # @return [String]
54
56
  def title(text)
55
57
  cased = case config.heading_case
56
- when :upcase then text.upcase
57
- when :capitalize then text.sub(/\A\p{Ll}/, &:upcase)
58
+ when :UPPERCASE then text.upcase
59
+ when :lowercase then text.downcase
60
+ when :Capitalize then text.sub(/\A\p{Ll}/, &:upcase)
58
61
  else text
59
62
  end
60
63
  paint(cased, :heading)
@@ -20,7 +20,7 @@ module Dry
20
20
  def help(command, prog_name)
21
21
  screen = Screens::Command.new(
22
22
  command:, prog_name:, top_level: !kommand.nil?,
23
- config: Help.config_for(registry), io: out
23
+ config: Help.config, io: out
24
24
  )
25
25
  out.puts screen.render
26
26
  exit(0)
@@ -30,7 +30,7 @@ module Dry
30
30
  # all, a group without a command of its own, `-h` or `--help` at a
31
31
  # registry level, or a typo.
32
32
  def spell_checker(result, arguments)
33
- config = Help.config_for(registry)
33
+ config = Help.config
34
34
  unmatched = arguments.drop(result.names.length)
35
35
 
36
36
  if unmatched.empty?
@@ -50,34 +50,6 @@ module Dry
50
50
  exit(status)
51
51
  end
52
52
  end
53
-
54
- # Included into Dry::CLI::Registry, so every registry can describe itself.
55
- module RegistryMethods
56
- # Configure this registry's help. A block taking an argument receives
57
- # the configuration; any other block runs against it.
58
- #
59
- # @example
60
- # help do
61
- # title "Taxlibris"
62
- # width 100
63
- # end
64
- #
65
- # @return [Configuration] this registry's settings
66
- def help(&block)
67
- @help_config ||= Configuration.new
68
- if block&.arity == 1
69
- yield @help_config
70
- elsif block
71
- @help_config.instance_eval(&block)
72
- end
73
- @help_config
74
- end
75
-
76
- # @return [Configuration, nil] nil until {#help} is called
77
- def help_config
78
- @help_config
79
- end
80
- end
81
53
  end
82
54
  end
83
55
  end
@@ -32,10 +32,9 @@ module Dry
32
32
  end
33
33
 
34
34
  def render_usage
35
- program = format.paint(prog_name, :command)
36
- lines = ["#{INDENT}#{program}#{usage_arguments} [OPTIONS]"]
37
- lines << "#{INDENT}#{program} COMMAND [OPTIONS]" if subcommand_rows.any?
38
- section(:usage, lines)
35
+ lines = ["#{prog_name}#{usage_arguments} [OPTIONS]"]
36
+ lines << "#{prog_name} COMMAND [OPTIONS]" if subcommand_rows.any?
37
+ section(:usage, lines.map { INDENT + format.paint(it, :usage) })
39
38
  end
40
39
 
41
40
  def render_description
@@ -59,7 +58,8 @@ module Dry
59
58
  def render_examples
60
59
  rows = command.examples.map do |example|
61
60
  line, comment = example.split(" # ", 2)
62
- Row.new(term: "#{prog_name} #{line.strip}", text: comment&.strip, text_style: :comment)
61
+ Row.new(term: "#{prog_name} #{line.strip}", text: comment&.strip,
62
+ term_style: :example, text_style: :example_comment)
63
63
  end
64
64
  section(:examples, format.definitions(rows, format.column_for(rows)))
65
65
  end
@@ -72,7 +72,7 @@ module Dry
72
72
  required = command.required_arguments.map { argument_name(it) }
73
73
  optional = command.optional_arguments.map { "[#{argument_name(it)}]" }
74
74
  names = [*required, *optional]
75
- " #{format.paint(names.join(' '), :argument)}" unless names.empty?
75
+ " #{names.join(' ')}" unless names.empty?
76
76
  end
77
77
 
78
78
  def subcommand_rows
@@ -32,8 +32,8 @@ module Dry
32
32
  end
33
33
 
34
34
  def render_usage
35
- program = format.paint(ProgramName.call(result.names), :command)
36
- section(:usage, ["#{INDENT}#{program} COMMAND [OPTIONS]"])
35
+ usage = format.paint("#{ProgramName.call(result.names)} COMMAND [OPTIONS]", :usage)
36
+ section(:usage, ["#{INDENT}#{usage}"])
37
37
  end
38
38
 
39
39
  def render_commands
@@ -9,7 +9,7 @@ module Dry
9
9
  # inherits from Object, so an empty reopening is compatible either way.
10
10
  class CLI
11
11
  module Help
12
- VERSION = "0.1.0"
12
+ VERSION = "0.2.1"
13
13
  end
14
14
  end
15
15
  end
data/lib/dry/cli/help.rb CHANGED
@@ -19,14 +19,22 @@ module Dry
19
19
  # Configurable, wrapped and colored help screens for dry-cli.
20
20
  #
21
21
  # Requiring this file changes the help every Dry::CLI in the process prints.
22
- # Settings come from {.configure} for the whole process, and from a
23
- # registry's `help` block for that registry.
22
+ # Every setting is made once, in {.configure}, so the way dry-cli declares
23
+ # registries, commands and options stays exactly as dry-cli ships it.
24
24
  module Help
25
25
  class << self
26
- # @yieldparam config [Configuration] the process-wide settings
26
+ # Make every setting. A block taking an argument receives the
27
+ # configuration; any other block runs against it.
28
+ #
29
+ # @example
30
+ # Dry::CLI::Help.configure do
31
+ # title "MyCLI"
32
+ # styles { heading :bold, :blue }
33
+ # end
34
+ #
27
35
  # @return [Configuration]
28
- def configure
29
- yield config
36
+ def configure(&block)
37
+ block.arity == 1 ? yield(config) : config.instance_eval(&block)
30
38
  config
31
39
  end
32
40
 
@@ -40,21 +48,9 @@ module Dry
40
48
  def reset!
41
49
  @config = nil
42
50
  end
43
-
44
- # The settings a registry renders with: its own `help` block over the
45
- # process-wide settings. A single command passed to Dry::CLI.new has no
46
- # `help` block, so it renders with the process-wide settings alone.
47
- #
48
- # @param registry [Module, Class, Dry::CLI::Command, nil]
49
- # @return [Configuration]
50
- def config_for(registry)
51
- own = registry.help_config if registry.respond_to?(:help_config)
52
- own ? config.merge(own) : config
53
- end
54
51
  end
55
52
  end
56
53
  end
57
54
  end
58
55
 
59
56
  Dry::CLI.prepend(Dry::CLI::Help::Integration::CLIMethods)
60
- Dry::CLI::Registry.include(Dry::CLI::Help::Integration::RegistryMethods)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-cli-help
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Gredeskoul