Skip to content

Commit abea013

Browse files
authored
Merge pull request #236 from DannyBen/refactor/expose
Refactor `command.expose` to show subcommands without `--help` when set to `always`
2 parents 514b529 + 872a93e commit abea013

File tree

11 files changed

+45
-19
lines changed

11 files changed

+45
-19
lines changed

examples/commands-expose/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ commands:
2626
help: Config management commands
2727

2828
# Setting `expose` to true will show the summary of the subcommands when
29-
# running the help for the parent command. In this case, `config edit` and
30-
# `config show` will be displayed when running `cli --help`.
29+
# using `cli --help` for the parent command. In this case, `config edit` and
30+
# `config show` will be displayed.
3131
expose: true
3232
commands:
3333
- name: edit
@@ -38,9 +38,12 @@ commands:
3838
- name: server
3939
help: Server management commands
4040

41+
# Setting `expose` to `always` will also show the summary of the subcommands
42+
# when running `cli` without arguments.
43+
expose: always
44+
4145
# Adding a `group` works well with `expose`. In this case, `server start` and
4246
# `server stop` will be listed under `Cluster Commands`.
43-
expose: true
4447
group: Cluster
4548

4649
commands:
@@ -81,6 +84,8 @@ Commands:
8184
8285
Cluster Commands:
8386
server Server management commands
87+
server start Start the server
88+
server stop Stop the server
8489
container Container management commands
8590
8691

examples/commands-expose/src/bashly.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ commands:
88
help: Config management commands
99

1010
# Setting `expose` to true will show the summary of the subcommands when
11-
# running the help for the parent command. In this case, `config edit` and
12-
# `config show` will be displayed when running `cli --help`.
11+
# using `cli --help` for the parent command. In this case, `config edit` and
12+
# `config show` will be displayed.
1313
expose: true
1414
commands:
1515
- name: edit
@@ -20,9 +20,12 @@ commands:
2020
- name: server
2121
help: Server management commands
2222

23+
# Setting `expose` to `always` will also show the summary of the subcommands
24+
# when running `cli` without arguments.
25+
expose: always
26+
2327
# Adding a `group` works well with `expose`. In this case, `server start` and
2428
# `server stop` will be listed under `Cluster Commands`.
25-
expose: true
2629
group: Cluster
2730

2831
commands:

lib/bashly/config_validator.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def assert_extensible(key, value)
4141
"#{key} must be a boolean or a string"
4242
end
4343

44+
def assert_expose(key, value)
45+
return unless value
46+
assert [true, false, nil, 'always'].include?(value), "#{key} must be a boolean, or the string 'always'"
47+
end
48+
4449
def assert_arg(key, value)
4550
assert_hash key, value, Script::Argument.option_keys
4651
assert_string "#{key}.name", value['name']
@@ -117,7 +122,7 @@ def assert_command(key, value)
117122

118123
assert_boolean "#{key}.private", value['private']
119124
assert_boolean "#{key}.default", value['default']
120-
assert_boolean "#{key}.expose", value['expose']
125+
assert_expose "#{key}.expose", value['expose']
121126
assert_version "#{key}.version", value['version']
122127
assert_catch_all "#{key}.catch_all", value['catch_all']
123128
assert_string_or_array "#{key}.alias", value['alias']

lib/bashly/script/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def command_help_data
8686
command.public_commands.each do |subcommand|
8787
result[command.group_string]["#{command.name} #{subcommand.name}"] = {
8888
summary: subcommand.summary_string,
89-
extended: true
89+
help_only: command.expose != 'always'
9090
}
9191
end
9292
end

lib/bashly/views/command/usage_commands.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
% command_help_data.each do |group, commands|
44
printf "<%= group %>\n"
55
% commands.each do |command, info|
6-
% if info[:extended]
6+
% if info[:help_only]
77
[[ -n $long_usage ]] && echo " <%= command.ljust maxlen %> <%= info[:summary] %>"
88
% else
99
echo " <%= command.ljust maxlen %> <%= info[:summary] %>"

spec/approvals/examples/commands-expose

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Commands:
2424

2525
Cluster Commands:
2626
server Server management commands
27+
server start Start the server
28+
server stop Stop the server
2729
container Container management commands
2830

2931
+ ./cli -h

spec/approvals/script/command/exposed_commands

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
:summary: Config management commands
77
config edit:
88
:summary: Edit config file
9-
:extended: true
9+
:help_only: false
1010
config show:
1111
:summary: Show config file
12-
:extended: true
12+
:help_only: false
1313
'Cluster Commands:':
1414
server:
1515
:summary: Server management commands
1616
server start:
1717
:summary: Start the server
18-
:extended: true
18+
:help_only: true
1919
server stop:
2020
:summary: Stop the server
21-
:extended: true
21+
:help_only: true
2222
container:
2323
:summary: Container management commands
2424
container exec:
2525
:summary: Run a command in the container
26-
:extended: true
26+
:help_only: true
2727
container down:
2828
:summary: Terminate a container
29-
:extended: true
29+
:help_only: true
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::ConfigurationError: root.commands[0].expose must be a boolean>
1+
#<Bashly::ConfigurationError: root.commands[0].expose must be a boolean, or the string 'always'>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.commands[0].expose must be a boolean, or the string 'always'>

spec/fixtures/script/commands.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
help: Start a new project
148148
- name: config
149149
help: Config management commands
150-
expose: true
150+
expose: always
151151
commands:
152152
- name: edit
153153
help: Edit config file

0 commit comments

Comments
 (0)