Skip to content

Commit f83363d

Browse files
authored
Merge pull request #305 from DannyBen/add/default-command-force
Add support for default command that is used instead of showing usage
2 parents 0da0929 + c15e817 commit f83363d

File tree

15 files changed

+246
-17
lines changed

15 files changed

+246
-17
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
1111
## Basic features
1212

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

0 commit comments

Comments
 (0)