Skip to content

Commit 1710f1b

Browse files
authored
Merge pull request #83 from DannyBen/add/footer
Add support for a custom help footer
2 parents 28d1e2a + 7512be5 commit 1710f1b

File tree

12 files changed

+293
-0
lines changed

12 files changed

+293
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ command and subcommands (under the `commands` definition).
201201
`catch_all` | Specify that this command should allow for additional arbitrary arguments or flags. It can be set in one of three ways:<br>- Set to `true` to just enable it.<br>- Set to a string, to use this string in the usage help text.<br>- Set to a hash containing `label` and `help` keys, to show a detailed help for it when running with `--help`.
202202
`dependencies` | Specify an array of any required external dependencies (commands). The script execution will be halted with a friendly error unless all dependency commands exist.
203203
`group` | In case you have many commands, use this option to specify a caption to display before this command. This option is purely for display purposes, and needs to be specified only for the first command in each group.
204+
`footer` | Add a custom message that will be displayed at the end of the `--help` text.
204205

205206
### Argument options
206207

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
3030
- [command-groups](command-groups#readme) - grouping sub-commands in logical sections
3131
- [custom-strings](custom-strings#readme) - configuring the script's error and usage texts
3232
- [custom-includes](custom-includes#readme) - adding and organizing your custom functions
33+
- [footer](footer#readme) - adding a footer to the help message
3334

3435
## Real-world-like examples
3536

examples/footer/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Footer Example
2+
3+
Demonstrates how to add a footer text to the `--help` message.
4+
5+
-----
6+
7+
## `bashly.yml`
8+
9+
```yaml
10+
name: download
11+
help: Sample application with a help footer
12+
version: 0.1.0
13+
footer: |
14+
This will appear at the end of the --help message.
15+
16+
It can contain multiple lines, and "double" or 'single' quotes are
17+
properly escaped.
18+
19+
args:
20+
- name: source
21+
help: URL to download from
22+
```
23+
24+
## Generated script output
25+
26+
```shell
27+
$ ./download
28+
29+
# this file is located in 'src/root_command.sh'
30+
# you can edit it freely and regenerate (it will not be overwritten)
31+
args: none
32+
33+
34+
$ ./download -h
35+
36+
download - Sample application with a help footer
37+
38+
Usage:
39+
download [SOURCE]
40+
download --help | -h
41+
download --version | -v
42+
43+
Options:
44+
--help, -h
45+
Show this help
46+
47+
--version, -v
48+
Show version number
49+
50+
Arguments:
51+
SOURCE
52+
URL to download from
53+
54+
This will appear at the end of the --help message.
55+
56+
It can contain multiple lines, and "double" or 'single' quotes are
57+
properly escaped.
58+
59+
60+
61+
62+
```
63+
64+
65+

examples/footer/download

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :command.root_command
6+
root_command() {
7+
# :src/root_command.sh
8+
echo "# this file is located in 'src/root_command.sh'"
9+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
10+
inspect_args
11+
}
12+
13+
# :command.version_command
14+
version_command() {
15+
echo "$version"
16+
}
17+
18+
# :command.usage
19+
download_usage() {
20+
if [[ -n $long_usage ]]; then
21+
printf "download - Sample application with a help footer\n"
22+
echo
23+
else
24+
printf "download - Sample application with a help footer\n"
25+
echo
26+
fi
27+
28+
printf "Usage:\n"
29+
printf " download [SOURCE]\n"
30+
printf " download --help | -h\n"
31+
printf " download --version | -v\n"
32+
echo
33+
34+
if [[ -n $long_usage ]]; then
35+
printf "Options:\n"
36+
# :command.usage_fixed_flags
37+
echo " --help, -h"
38+
printf " Show this help\n"
39+
echo
40+
echo " --version, -v"
41+
printf " Show version number\n"
42+
echo
43+
44+
# :command.usage_args
45+
printf "Arguments:\n"
46+
47+
# :argument.usage
48+
echo " SOURCE"
49+
printf " URL to download from\n"
50+
echo
51+
52+
printf "This will appear at the end of the --help message.\n\nIt can contain multiple lines, and \"double\" or 'single' quotes are\nproperly escaped.\n\n"
53+
echo
54+
55+
fi
56+
}
57+
58+
# :command.inspect_args
59+
inspect_args() {
60+
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
61+
if (( ${#args[@]} )); then
62+
echo args:
63+
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
64+
else
65+
echo args: none
66+
fi
67+
68+
if (( ${#other_args[@]} )); then
69+
echo
70+
echo other_args:
71+
echo "- \${other_args[*]} = ${other_args[*]}"
72+
for i in "${!other_args[@]}"; do
73+
echo "- \${other_args[$i]} = ${other_args[$i]}"
74+
done
75+
fi
76+
}
77+
78+
# :command.command_functions
79+
80+
# :command.parse_requirements
81+
parse_requirements() {
82+
# :command.fixed_flag_filter
83+
case "$1" in
84+
--version | -v )
85+
version_command
86+
exit
87+
;;
88+
89+
--help | -h )
90+
long_usage=yes
91+
download_usage
92+
exit 1
93+
;;
94+
95+
esac
96+
# :command.environment_variables_filter
97+
# :command.dependencies_filter
98+
# :command.command_filter
99+
action="root"
100+
# :command.required_args_filter
101+
# :command.required_flags_filter
102+
# :command.parse_requirements_while
103+
while [[ $# -gt 0 ]]; do
104+
key="$1"
105+
case "$key" in
106+
107+
-* )
108+
printf "invalid option: %s\n" "$key"
109+
exit 1
110+
;;
111+
112+
* )
113+
# :command.parse_requirements_case
114+
if [[ ! ${args[source]} ]]; then
115+
args[source]=$1
116+
shift
117+
else
118+
printf "invalid argument: %s\n" "$key"
119+
exit 1
120+
fi
121+
;;
122+
123+
esac
124+
done
125+
# :command.default_assignments
126+
# :command.whitelist_filter
127+
}
128+
129+
# :command.initialize
130+
initialize() {
131+
version="0.1.0"
132+
long_usage=''
133+
set -e
134+
135+
# :src/initialize.sh
136+
# Code here runs inside the initialize() function
137+
# Use it for anything that you need to run before any other function, like
138+
# setting environment vairables:
139+
# CONFIG_FILE=settings.ini
140+
#
141+
# Feel free to empty (but not delete) this file.
142+
}
143+
144+
# :command.run
145+
run() {
146+
declare -A args
147+
declare -a other_args
148+
parse_requirements "$@"
149+
150+
if [[ $action == "root" ]]; then
151+
root_command
152+
fi
153+
}
154+
155+
initialize
156+
run "$@"

examples/footer/src/bashly.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: download
2+
help: Sample application with a help footer
3+
version: 0.1.0
4+
footer: |
5+
This will appear at the end of the --help message.
6+
7+
It can contain multiple lines, and "double" or 'single' quotes are
8+
properly escaped.
9+
10+
args:
11+
- name: source
12+
help: URL to download from

examples/footer/src/initialize.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Code here runs inside the initialize() function
2+
# Use it for anything that you need to run before any other function, like
3+
# setting environment vairables:
4+
# CONFIG_FILE=settings.ini
5+
#
6+
# Feel free to empty (but not delete) this file.

examples/footer/src/root_command.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo "# this file is located in 'src/root_command.sh'"
2+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
3+
inspect_args

examples/footer/test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f ./src/*.sh
4+
5+
set -x
6+
7+
bashly generate
8+
9+
./download
10+
./download -h

lib/bashly/models/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Base
1616
examples
1717
extensible
1818
flags
19+
footer
1920
group
2021
help
2122
long

lib/bashly/views/command/footer.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
printf "<%= footer.gsub("\n", '\n').gsub('"', '\"') %>\n"
2+
echo

0 commit comments

Comments
 (0)