Skip to content

Commit bb20e6c

Browse files
committed
- Add support for default command that is used instead of showing usage
1 parent 0da0929 commit bb20e6c

File tree

13 files changed

+225
-14
lines changed

13 files changed

+225
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tester
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Default Forced Command Example
2+
3+
Demonstrates how to set a command as the default command, that also rune when
4+
the it is executed without arguments, instead of showing the standard usage
5+
text.
6+
7+
This example was generated with:
8+
9+
```bash
10+
$ bashly init
11+
# ... now edit src/bashly.yml to match the example ...
12+
$ bashly generate
13+
```
14+
15+
-----
16+
17+
## `bashly.yml`
18+
19+
```yaml
20+
name: tester
21+
help: Sample application that uses the forced default command option
22+
version: 0.1.0
23+
24+
commands:
25+
- name: all
26+
help: Run all tests
27+
28+
# By setting the `default: force`, it will be executed when the command line
29+
# is not recognized, and when it is empty.
30+
default: force
31+
32+
- name: only
33+
help: Run only specific tests
34+
args:
35+
- name: search
36+
required: true
37+
help: File pattern of tests to run
38+
```
39+
40+
41+
42+
## Generated script output
43+
44+
### `$ ./tester -h`
45+
46+
```shell
47+
tester - Sample application that uses the forced default command option
48+
49+
Usage:
50+
tester COMMAND
51+
tester [COMMAND] --help | -h
52+
tester --version | -v
53+
54+
Commands:
55+
all Run all tests (default)
56+
only Run only specific tests
57+
58+
Options:
59+
--help, -h
60+
Show this help
61+
62+
--version, -v
63+
Show version number
64+
65+
66+
67+
```
68+
69+
### `$ ./tester`
70+
71+
```shell
72+
# this file is located in 'src/all_command.sh'
73+
# code for 'tester all' goes here
74+
# you can edit it freely and regenerate (it will not be overwritten)
75+
args: none
76+
77+
78+
```
79+
80+
### `$ ./tester all`
81+
82+
```shell
83+
# this file is located in 'src/all_command.sh'
84+
# code for 'tester all' goes here
85+
# you can edit it freely and regenerate (it will not be overwritten)
86+
args: none
87+
88+
89+
```
90+
91+
### `$ ./tester only one`
92+
93+
```shell
94+
# this file is located in 'src/only_command.sh'
95+
# code for 'tester only' goes here
96+
# you can edit it freely and regenerate (it will not be overwritten)
97+
args:
98+
- ${args[search]} = one
99+
100+
101+
```
102+
103+
104+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/all_command.sh'"
2+
echo "# code for 'tester all' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: tester
2+
help: Sample application that uses the forced default command option
3+
version: 0.1.0
4+
5+
commands:
6+
- name: all
7+
help: Run all tests
8+
9+
# By setting the `default: force`, it will be executed when the command line
10+
# is not recognized, and when it is empty.
11+
default: force
12+
13+
- name: only
14+
help: Run only specific tests
15+
args:
16+
- name: search
17+
required: true
18+
help: File pattern of tests to run
19+
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 variables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/only_command.sh'"
2+
echo "# code for 'tester only' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
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+
rm -f ./src/*.sh
4+
5+
set -x
6+
7+
bashly generate
8+
9+
### Try Me ###
10+
11+
./tester -h
12+
./tester
13+
./tester all
14+
./tester all -h
15+
./tester only one

lib/bashly/config_validator.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def assert_catch_all_hash(key, value)
3737
assert_boolean "#{key}.required", value['required']
3838
end
3939

40+
def assert_default_command(key, value)
41+
return unless value
42+
43+
assert [true, false, nil, 'force'].include?(value), "#{key} must be a boolean, or the string 'force'"
44+
end
45+
4046
def assert_dependencies(key, value)
4147
return unless value
4248

@@ -145,7 +151,7 @@ def assert_command(key, value)
145151
assert_optional_string "#{key}.function", value['function']
146152

147153
assert_boolean "#{key}.private", value['private']
148-
assert_boolean "#{key}.default", value['default']
154+
assert_default_command "#{key}.default", value['default']
149155
assert_expose "#{key}.expose", value['expose']
150156
assert_version "#{key}.version", value['version']
151157
assert_catch_all "#{key}.catch_all", value['catch_all']
@@ -171,10 +177,6 @@ def assert_command(key, value)
171177
"#{key}.function must contain lowercase alphanumeric characters and underscores only"
172178
end
173179

174-
if value['default']
175-
assert value['args'], "#{key}.default makes no sense without args"
176-
end
177-
178180
if value['catch_all'] && value['args']
179181
repeatable_arg = value['args'].find { |a| a['repeatable'] }&.dig 'name'
180182
refute repeatable_arg, "#{key}.catch_all makes no sense with repeatable arg (#{repeatable_arg})"

lib/bashly/views/command/command_fallback.gtx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
= view_marker
22

3-
> "")
4-
> {{ function_name }}_usage >&2
5-
> exit 1
6-
> ;;
7-
>
3+
if !default_command || default_command.default != 'force'
4+
> "")
5+
> {{ function_name }}_usage >&2
6+
> exit 1
7+
> ;;
8+
>
9+
end
10+
811
> *)
912

1013
if default_command
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
+ bashly generate
2+
creating user files in src
3+
created src/initialize.sh
4+
created src/all_command.sh
5+
created src/only_command.sh
6+
created ./tester
7+
run ./tester --help to test your bash script
8+
+ ./tester -h
9+
tester - Sample application that uses the forced default command option
10+
11+
Usage:
12+
tester COMMAND
13+
tester [COMMAND] --help | -h
14+
tester --version | -v
15+
16+
Commands:
17+
all Run all tests (default)
18+
only Run only specific tests
19+
20+
Options:
21+
--help, -h
22+
Show this help
23+
24+
--version, -v
25+
Show version number
26+
27+
+ ./tester
28+
# this file is located in 'src/all_command.sh'
29+
# code for 'tester all' goes here
30+
# you can edit it freely and regenerate (it will not be overwritten)
31+
args: none
32+
+ ./tester all
33+
# this file is located in 'src/all_command.sh'
34+
# code for 'tester all' goes here
35+
# you can edit it freely and regenerate (it will not be overwritten)
36+
args: none
37+
+ ./tester all -h
38+
tester all - Run all tests
39+
40+
Usage:
41+
tester all
42+
tester all --help | -h
43+
44+
Options:
45+
--help, -h
46+
Show this help
47+
48+
+ ./tester only one
49+
# this file is located in 'src/only_command.sh'
50+
# code for 'tester only' goes here
51+
# you can edit it freely and regenerate (it will not be overwritten)
52+
args:
53+
- ${args[search]} = one

0 commit comments

Comments
 (0)