Skip to content

Commit 1886ff1

Browse files
committed
add function example
1 parent c525031 commit 1886ff1

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
4343
- [custom-script-header](custom-script-header#readme) - configuring a different script header
4444
- [footer](footer#readme) - adding a footer to the help message
4545
- [command-filenames](command-filenames#readme) - configuring paths for your source scripts
46+
- [command-function](command-function#readme) - configuring custom internal function names
4647
- [split-config](split-config#readme) - splitting your `bashly.yml` into several smaller files
4748

4849
## Real-world-like examples
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src/*.sh
2+
cli
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Command Function Example
2+
3+
Demonstrates how to specify a custom function name for commands. This option
4+
is used internally by bashly to determine the names of the functions for any
5+
command.
6+
7+
Normally, this should not be modified, but in some use cases it might be
8+
necessary. One such use case, is when you have two commands that naturally
9+
evaluate to the same function name.
10+
11+
Assuming your CLI responds to both:
12+
13+
- `cli container-start`
14+
- `cli container start`
15+
16+
In this case, the internal function names will both be `cli_container_start`,
17+
which might be undesirable. This is where the `function` directive comes in
18+
handy.
19+
20+
Also note that the name specified here is just used as a base name. Bashly will
21+
generate several functions from it:
22+
23+
- `cli_container_start_command`
24+
- `cli_container_start_usage`
25+
- and possibly more
26+
27+
This example was generated with:
28+
29+
```bash
30+
$ bashly init
31+
# ... now edit src/bashly.yml to match the example ...
32+
$ bashly generate
33+
```
34+
35+
-----
36+
37+
## `bashly.yml`
38+
39+
```yaml
40+
name: cli
41+
help: Sample application with custom function names
42+
version: 0.1.0
43+
44+
commands:
45+
- name: container-start
46+
help: Start a new container (deprecated)
47+
48+
# Override the name of the internal functions bashly uses. This is needed
49+
# in this case since the command `container-start` and the nested command
50+
# `container start` will have the same underlying function name otherwise.
51+
function: deprecated_container_start
52+
footer: This command is deprecated, use 'container start' instead
53+
54+
- name: container
55+
help: Container commands
56+
commands:
57+
- name: start
58+
help: Start a new container
59+
```
60+
61+
62+
63+
## Generated script output
64+
65+
### `$ ./cli`
66+
67+
```shell
68+
cli - Sample application with custom function names
69+
70+
Usage:
71+
cli COMMAND
72+
cli [COMMAND] --help | -h
73+
cli --version | -v
74+
75+
Commands:
76+
container-start Start a new container (deprecated)
77+
container Container commands
78+
79+
80+
81+
```
82+
83+
### `$ ./cli -h`
84+
85+
```shell
86+
cli - Sample application with custom function names
87+
88+
Usage:
89+
cli COMMAND
90+
cli [COMMAND] --help | -h
91+
cli --version | -v
92+
93+
Commands:
94+
container-start Start a new container (deprecated)
95+
container Container commands
96+
97+
Options:
98+
--help, -h
99+
Show this help
100+
101+
--version, -v
102+
Show version number
103+
104+
105+
106+
```
107+
108+
### `$ ./cli container-start --help`
109+
110+
```shell
111+
cli container-start - Start a new container (deprecated)
112+
113+
Usage:
114+
cli container-start
115+
cli container-start --help | -h
116+
117+
Options:
118+
--help, -h
119+
Show this help
120+
121+
This command is deprecated, use 'container start' instead
122+
123+
124+
125+
```
126+
127+
### `$ ./cli container start --help`
128+
129+
```shell
130+
cli container start - Start a new container
131+
132+
Usage:
133+
cli container start
134+
cli container start --help | -h
135+
136+
Options:
137+
--help, -h
138+
Show this help
139+
140+
141+
142+
```
143+
144+
145+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: cli
2+
help: Sample application with custom function names
3+
version: 0.1.0
4+
5+
commands:
6+
- name: container-start
7+
help: Start a new container (deprecated)
8+
9+
# Override the name of the internal functions bashly uses. This is needed
10+
# in this case since the command `container-start` and the nested command
11+
# `container start` will have the same underlying function name otherwise.
12+
function: deprecated_container_start
13+
footer: This command is deprecated, use 'container start' instead
14+
15+
- name: container
16+
help: Container commands
17+
commands:
18+
- name: start
19+
help: Start a new container

examples/command-function/test.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
# Cleanup before testing
4+
rm -f "src/*.sh"
5+
6+
set -x
7+
8+
bashly generate
9+
10+
### Try Me ###
11+
12+
./cli
13+
./cli -h
14+
./cli container-start --help
15+
./cli container start --help
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
+ bashly generate
2+
creating user files in src
3+
skipped src/initialize.sh (exists)
4+
skipped src/container_start_command.sh (exists)
5+
skipped src/container_start_command.sh (exists)
6+
created ./cli
7+
run ./cli --help to test your bash script
8+
+ ./cli
9+
cli - Sample application with custom function names
10+
11+
Usage:
12+
cli COMMAND
13+
cli [COMMAND] --help | -h
14+
cli --version | -v
15+
16+
Commands:
17+
container-start Start a new container (deprecated)
18+
container Container commands
19+
20+
+ ./cli -h
21+
cli - Sample application with custom function names
22+
23+
Usage:
24+
cli COMMAND
25+
cli [COMMAND] --help | -h
26+
cli --version | -v
27+
28+
Commands:
29+
container-start Start a new container (deprecated)
30+
container Container commands
31+
32+
Options:
33+
--help, -h
34+
Show this help
35+
36+
--version, -v
37+
Show version number
38+
39+
+ ./cli container-start --help
40+
cli container-start - Start a new container (deprecated)
41+
42+
Usage:
43+
cli container-start
44+
cli container-start --help | -h
45+
46+
Options:
47+
--help, -h
48+
Show this help
49+
50+
This command is deprecated, use 'container start' instead
51+
52+
+ ./cli container start --help
53+
cli container start - Start a new container
54+
55+
Usage:
56+
cli container start
57+
cli container start --help | -h
58+
59+
Options:
60+
--help, -h
61+
Show this help
62+

0 commit comments

Comments
 (0)