Skip to content

Commit 26d6f19

Browse files
committed
- Add support for captuing --help when using catch_all
1 parent dc240d2 commit 26d6f19

File tree

23 files changed

+186
-27
lines changed

23 files changed

+186
-27
lines changed

examples/render-mandoc/docs/download.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" Automatically generated by Pandoc 3.2
22
.\"
3-
.TH "download" "1" "September 2025" "Version 0.1.0" "Sample application"
3+
.TH "download" "1" "November 2025" "Version 0.1.0" "Sample application"
44
.SH NAME
55
\f[B]download\f[R] \- Sample application
66
.SH SYNOPSIS

examples/render-mandoc/docs/download.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% download(1) Version 0.1.0 | Sample application
22
% Lana Lang
3-
% September 2025
3+
% November 2025
44

55
NAME
66
==================================================

lib/bashly/config_validator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def assert_catch_all_hash(key, value)
3535
assert_string "#{key}.label", value['label']
3636
assert_optional_string "#{key}.help", value['help']
3737
assert_boolean "#{key}.required", value['required']
38+
assert_boolean "#{key}.catch_help", value['catch_help']
3839
end
3940

4041
def assert_default_command(key, value)

lib/bashly/script/catch_all.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Script
33
class CatchAll
44
class << self
55
def option_keys
6-
@option_keys ||= %i[label help required]
6+
@option_keys ||= %i[label help required catch_help]
77
end
88

99
def from_config(config)
@@ -13,7 +13,7 @@ def from_config(config)
1313
when String
1414
{ label: config }
1515
when Hash
16-
{ label: config['label'], help: config['help'], required: config['required'] }
16+
config.transform_keys(&:to_sym).slice(*option_keys)
1717
else
1818
{}
1919
end
@@ -22,11 +22,12 @@ def from_config(config)
2222
end
2323
end
2424

25-
def initialize(label: nil, help: nil, required: false, enabled: true)
25+
def initialize(label: nil, help: nil, required: false, catch_help: false, enabled: true)
2626
@label = label
2727
@help = help
2828
@required = required
2929
@enabled = enabled
30+
@catch_help = catch_help
3031
end
3132

3233
def enabled?
@@ -45,6 +46,10 @@ def required?
4546
@required
4647
end
4748

49+
def catch_help?
50+
@catch_help
51+
end
52+
4853
def usage_string
4954
return nil unless enabled?
5055

lib/bashly/script/introspection/flags.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ def flags
1616
end
1717
end
1818

19+
# Returns true if there is at least one fixed flag (--version / --help)
20+
# Root command always has --version and subcommands always have --help
21+
# except when it is consumed by catch_all
22+
def fixed_flags?
23+
root_command? || !catch_all.catch_help?
24+
end
25+
1926
# Returns true if this command's flags should be considered as gloal
2027
# flags, and cascade to subcommands
2128
def global_flags?

lib/bashly/views/command/fixed_flags_filter.gtx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ if root_command?
1212
>
1313
end
1414

15-
= (short_flag_exist?("-h") ? "--help)" : "--help | -h)").indent(4)
16-
> long_usage=yes
17-
> <%= function_name %>_usage
18-
> exit
19-
> ;;
20-
>
15+
unless catch_all.catch_help?
16+
= (short_flag_exist?("-h") ? "--help)" : "--help | -h)").indent(4)
17+
> long_usage=yes
18+
> <%= function_name %>_usage
19+
> exit
20+
> ;;
21+
>
22+
end
2123

2224
if global_flags?
2325
flags.each do |flag|

lib/bashly/views/command/long_usage.gtx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
= view_marker
22

33
> if [[ -n "$long_usage" ]]; then
4-
options_caption = strings[:options]
5-
if commands.any? && (public_flags.any? || (flags.any? && Settings.private_reveal_key))
6-
options_caption = strings[:global_options]
7-
end
8-
9-
> printf "%s\n" "{{ options_caption.color(:caption) }}"
10-
>
11-
= render(:usage_flags).indent 2 if flags.any?
12-
= render(:usage_fixed_flags).indent 2
4+
= render(:usage_options).indent 2
135
= render(:usage_args).indent 2 if args.any? or catch_all.help
146
= render(:usage_environment_variables).indent 2 if environment_variables.any?
157
= render(:usage_examples).indent 2 if examples

lib/bashly/views/command/usage_fixed_flags.gtx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
= view_marker
22

3-
if short_flag_exist?("-h")
4-
> printf " %s\n" "{{ '--help'.color(:flag) }}"
5-
else
6-
> printf " %s\n" "{{ '--help, -h'.color(:flag) }}"
7-
end
3+
unless catch_all.catch_help?
4+
if short_flag_exist?("-h")
5+
> printf " %s\n" "{{ '--help'.color(:flag) }}"
6+
else
7+
> printf " %s\n" "{{ '--help, -h'.color(:flag) }}"
8+
end
89

9-
> printf " {{ strings[:help_flag_text] }}\n"
10-
> echo
10+
> printf " {{ strings[:help_flag_text] }}\n"
11+
> echo
12+
end
1113

1214
if root_command?
1315
if short_flag_exist?("-v")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
= view_marker
2+
3+
if flags.any? || fixed_flags?
4+
options_caption = strings[:options]
5+
if commands.any? && (public_flags.any? || (flags.any? && Settings.private_reveal_key))
6+
options_caption = strings[:global_options]
7+
end
8+
9+
> printf "%s\n" "{{ options_caption.color(:caption) }}"
10+
>
11+
= render(:usage_flags).indent 2 if flags.any?
12+
= render(:usage_fixed_flags).indent 2
13+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
+ bundle exec bashly generate
2+
creating user files in src
3+
skipped src/docker_command.sh (exists)
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
+ ./cli docker --help
7+
args: none
8+
9+
other_args:
10+
- ${other_args[*]} = --help
11+
- ${other_args[0]} = --help
12+
+ ./cli docker --show-help-anyway
13+
cli docker - Pass all arguments to the docker cli
14+
15+
Usage:
16+
cli docker [OPTIONS] [--] [PARAMS...]
17+
cli docker --help | -h
18+
19+
Options:
20+
--show-help-anyway
21+
Show this help message
22+
23+
Arguments:
24+
PARAMS...
25+
Parameters to pass to docker
26+

0 commit comments

Comments
 (0)