Skip to content

Commit cf6596b

Browse files
authored
Merge pull request #71 from DannyBen/add/catch-all
Add ability to catch arbitrary args and flags
2 parents d3a49e0 + 8403b1f commit cf6596b

File tree

44 files changed

+1164
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1164
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,19 @@ The `bashly.yml` configuration file consists of these types:
196196
Unless otherwise specified, these definitiona can be used for both the root
197197
command and subcommands (under the `commands` definition).
198198

199-
200199
Option | Description
201200
-----------|-------------
202201
`name` | The name of the script or subcommand.
203202
`short` | An additional, optional pattern - usually used to denote a one letter variation of the command name. You can add `*` as a suffix, to denote a "starts with" pattern - for example `short: m*`. *Applicable only in subcommands*.
204203
`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.
205204
`version` | The string to display when using `--version`. *Applicable only in the main command*.
206-
`default` | Setting this to `yes` on any command, will make unrecognized command line arguments to be passed to this command. *Applicable only in subcommands*.
205+
`default` | Setting this to `true` on any command, will make unrecognized command line arguments to be passed to this command. *Applicable only in subcommands*.
207206
`examples` | Specify an array of examples to show when using `--help`. Each example can have multiple lines.
208207
`environment_variables` | Specify an array of environment variables needed by your script.
209208
`commands` | Specify the array of commands. Each command will have its own args and flags. Note: if `commands` is provided, you cannot specify flags or args at the same level.
210209
`args` | Specify the array of positional arguments this script needs.
211210
`flags` | Specify the array of option flags this script needs.
211+
`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`.
212212
`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.
213213
`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.
214214

examples/catch_all/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Catch All Example
2+
==================================================
3+
4+
This example was generated with:
5+
6+
$ bashly init
7+
$ bashly generate

examples/catch_all/download

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 - Catch All Example\n"
22+
echo
23+
else
24+
printf "download - Catch All Example\n"
25+
echo
26+
fi
27+
28+
printf "Usage:\n"
29+
printf " download MESSAGE [...]\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 " MESSAGE"
49+
printf " Message\n"
50+
echo
51+
52+
fi
53+
}
54+
55+
# :command.inspect_args
56+
inspect_args() {
57+
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
58+
if (( ${#args[@]} )); then
59+
echo args:
60+
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
61+
else
62+
echo args: none
63+
fi
64+
65+
if (( ${#other_args[@]} )); then
66+
echo
67+
echo other_args:
68+
echo "- \${other_args[*]} = ${other_args[*]}"
69+
for i in "${!other_args[@]}"; do
70+
echo "- \${other_args[$i]} = ${other_args[$i]}"
71+
done
72+
fi
73+
}
74+
75+
# :command.command_functions
76+
77+
# :command.parse_requirements
78+
parse_requirements() {
79+
# :command.fixed_flag_filter
80+
case "$1" in
81+
--version | -v )
82+
version_command
83+
exit
84+
;;
85+
86+
--help | -h )
87+
long_usage=yes
88+
download_usage
89+
exit 1
90+
;;
91+
92+
esac
93+
# :command.environment_variables_filter
94+
# :command.dependencies_filter
95+
# :command.command_filter
96+
action="root"
97+
# :command.required_args_filter
98+
if [[ $1 && $1 != -* ]]; then
99+
args[message]=$1
100+
shift
101+
else
102+
printf "missing required argument: MESSAGE\nusage: download MESSAGE [...]\n"
103+
exit 1
104+
fi
105+
# :command.required_flags_filter
106+
# :command.parse_requirements_while
107+
while [[ $# -gt 0 ]]; do
108+
key="$1"
109+
case "$key" in
110+
111+
-* )
112+
other_args+=("$1")
113+
shift
114+
;;
115+
116+
* )
117+
# :command.parse_requirements_case
118+
if [[ ! ${args[message]} ]]; then
119+
args[message]=$1
120+
shift
121+
else
122+
other_args+=("$1")
123+
shift
124+
fi
125+
;;
126+
127+
esac
128+
done
129+
# :command.default_assignments
130+
# :command.whitelist_filter
131+
}
132+
133+
# :command.initialize
134+
initialize() {
135+
version="0.1.0"
136+
long_usage=''
137+
set -e
138+
139+
# :src/initialize.sh
140+
# Code here runs inside the initialize() function
141+
# Use it for anything that you need to run before any other function, like
142+
# setting environment vairables:
143+
# CONFIG_FILE=settings.ini
144+
#
145+
# Feel free to empty (but not delete) this file.
146+
}
147+
148+
# :command.run
149+
run() {
150+
declare -A args
151+
declare -a other_args
152+
parse_requirements "$@"
153+
154+
if [[ $action == "root" ]]; then
155+
root_command
156+
fi
157+
}
158+
159+
initialize
160+
run "$@"

examples/catch_all/src/bashly.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: download
2+
help: Catch All Example
3+
version: 0.1.0
4+
catch_all: true
5+
6+
args:
7+
- name: message
8+
required: true
9+
help: Message
10+

examples/catch_all/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.
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/catch_all/test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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
11+
./download something
12+
./download something with --additional args

examples/catch_all_advanced/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Catch All Advanced Example
2+
==================================================
3+
4+
This example was generated with:
5+
6+
$ bashly init
7+
$ bashly generate

0 commit comments

Comments
 (0)