Skip to content

Commit 85ab77e

Browse files
authored
Merge pull request #81 from DannyBen/add/extensible
Add support for extensible commands
2 parents dce6d18 + a049a64 commit 85ab77e

File tree

33 files changed

+1049
-19
lines changed

33 files changed

+1049
-19
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Create beautiful bash scripts from simple YAML configuration
3131
- [Argument options](#argument-options)
3232
- [Flag options](#flag-options)
3333
- [Environment Variable options](#environment-variable-options)
34+
- [Extensible Commands](#extensible-commands)
3435
- [Real World Examples](#real-world-examples)
3536
- [Contributing / Support](#contributing--support)
3637

@@ -203,6 +204,7 @@ command and subcommands (under the `commands` definition).
203204
`help` | The header text to display when using `--help`. This option can have multiple lines. In this case, the first line will be used as summary wherever appropriate.
204205
`version` | The string to display when using `--version`. *Applicable only in the main command*.
205206
`default` | Setting this to `true` on any command, will cause any unrecognized command line to be passed to this command. *Applicable only in subcommands*.
207+
`extensible` | Specify that this command can be [externally extended](#extensible-commands).
206208
`examples` | Specify an array of examples to show when using `--help`. Each example can have multiple lines.
207209
`environment_variables` | Specify an array of [environment variables](#environment-variable-options) needed by your script.
208210
`commands` | Specify the array of [commands](#command-options). Each command will have its own args and flags. Note: if `commands` is provided, you cannot specify flags or args at the same level.
@@ -262,6 +264,82 @@ set.
262264
`required` | Specify if this variable is required.
263265

264266

267+
## Extensible Commands
268+
269+
You may configure your generated bash script to delegate any unknown command
270+
to an external executable, by setting the `extensible` option to either `true`,
271+
or to a different external command.
272+
273+
This is similar to how `git` works. When you execute `git whatever`, the `git`
274+
command will look for a file named `git-whatever` in the path, and execute it.
275+
276+
Note that this option cannot be specified together with the `default` option,
277+
since both specify a handler for unknown commands.
278+
279+
Bashly supports two operation modes.
280+
281+
### Extension Mode (`extensible: true`)
282+
283+
By setting `extensible` to `true`, a specially named executable will be called
284+
when an unknown command is called by the user.
285+
286+
Given this `bashly.yml` configuration:
287+
288+
```yaml
289+
name: myscript
290+
help: Example
291+
version: 0.1.0
292+
extensible: true
293+
294+
commands:
295+
- name: upload
296+
help: Upload a file
297+
```
298+
299+
And this user command:
300+
301+
```
302+
$ myscript something
303+
304+
```
305+
306+
The generated script will look for an executable named `myscript-something`
307+
in the path. If found, it will be called.
308+
309+
See the [extensible example](examples/extensible)
310+
311+
312+
### Delegate Mode (`extensible: <executable name>`)
313+
314+
By setting `extensible` to any string, unknown command calls by the user will
315+
be delegated to the executable with that name.
316+
317+
Given this `bashly.yml` configuration:
318+
319+
```yaml
320+
name: mygit
321+
help: Example
322+
version: 0.1.0
323+
extensible: git
324+
325+
commands:
326+
- name: push
327+
help: Push to my repository
328+
```
329+
330+
And this user command:
331+
332+
```
333+
$ mygit status
334+
335+
```
336+
337+
The generated script will execute `git status`.
338+
339+
See the [extensible-delegate example](examples/extensible-delegate)
340+
341+
342+
265343
## Real World Examples
266344

267345
- [Rush][rush] - a Personal Package Manager

Runfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,30 @@ end
3333

3434
def examples
3535
[
36+
"examples/catch_all/download",
37+
"examples/catch_all_advanced/cli",
3638
"examples/colors/colorly",
3739
"examples/command-default/ftp",
3840
"examples/command-groups/ftp",
41+
"examples/commands-nested/cli",
3942
"examples/commands/cli",
4043
"examples/config-ini/configly",
4144
"examples/custom-includes/download",
4245
"examples/custom-strings/download",
46+
"examples/default-values/convert",
4347
"examples/dependencies/cli",
4448
"examples/docker-like/docker",
4549
"examples/environment-variables/cli",
50+
"examples/extensible-delegate/mygit",
51+
"examples/extensible/cli",
4652
"examples/git-like/git",
4753
"examples/minimal/download",
4854
"examples/minus-v/cli",
4955
"examples/multiline/multi",
56+
"examples/whitelist/login",
5057
"examples/yaml/yaml",
58+
"spec/fixtures/workspaces/catch-all-no-args/download",
59+
"spec/fixtures/workspaces/flag-args-with-dash/argflag",
5160
"spec/fixtures/workspaces/short-command-code/rush",
5261
]
5362
end

examples/catch_all_advanced/cli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ parse_requirements() {
221221
shift $#
222222
;;
223223

224+
# :command.command_fallback
224225
* )
225226
cli_usage
226227
exit 1

examples/command-default/ftp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ parse_requirements() {
191191
shift $#
192192
;;
193193

194+
# :command.command_fallback
194195
"" )
195196
ftp_usage
196197
exit 1

examples/command-groups/ftp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ parse_requirements() {
270270
shift $#
271271
;;
272272

273+
# :command.command_fallback
273274
* )
274275
ftp_usage
275276
exit 1

examples/commands-nested/cli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ parse_requirements() {
340340
shift $#
341341
;;
342342

343+
# :command.command_fallback
343344
* )
344345
cli_usage
345346
exit 1
@@ -409,6 +410,7 @@ cli_dir_parse_requirements() {
409410
shift $#
410411
;;
411412

413+
# :command.command_fallback
412414
* )
413415
cli_dir_usage
414416
exit 1
@@ -596,6 +598,7 @@ cli_file_parse_requirements() {
596598
shift $#
597599
;;
598600

601+
# :command.command_fallback
599602
* )
600603
cli_file_usage
601604
exit 1

examples/commands/cli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ parse_requirements() {
230230
shift $#
231231
;;
232232

233+
# :command.command_fallback
233234
* )
234235
cli_usage
235236
exit 1

examples/config-ini/configly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ parse_requirements() {
392392
shift $#
393393
;;
394394

395+
# :command.command_fallback
395396
* )
396397
configly_usage
397398
exit 1

examples/dependencies/cli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ parse_requirements() {
169169
shift $#
170170
;;
171171

172+
# :command.command_fallback
172173
* )
173174
cli_usage
174175
exit 1

examples/docker-like/docker

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ parse_requirements() {
288288
shift $#
289289
;;
290290

291+
# :command.command_fallback
291292
* )
292293
docker_usage
293294
exit 1
@@ -357,6 +358,7 @@ docker_container_parse_requirements() {
357358
shift $#
358359
;;
359360

361+
# :command.command_fallback
360362
* )
361363
docker_container_usage
362364
exit 1
@@ -531,6 +533,7 @@ docker_image_parse_requirements() {
531533
shift $#
532534
;;
533535

536+
# :command.command_fallback
534537
* )
535538
docker_image_usage
536539
exit 1

0 commit comments

Comments
 (0)