Skip to content

Commit 512338a

Browse files
committed
- Add support for global command flags
1 parent bcbc7b4 commit 512338a

Some content is hidden

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

41 files changed

+150
-134
lines changed

lib/bashly/config_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def assert_command(key, value)
112112
assert_hash key, value, Script::Command.option_keys
113113

114114
refute value['commands'] && value['args'], "#{key} cannot have both commands and args"
115-
refute value['commands'] && value['flags'], "#{key} cannot have both commands and flags"
115+
# refute value['commands'] && value['flags'], "#{key} cannot have both commands and flags"
116116

117117
assert_string "#{key}.name", value['name']
118118
assert_optional_string "#{key}.help", value['help']

lib/bashly/script/command.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ def full_name
177177
parents.any? ? (parents + [name]).join(' ') : name
178178
end
179179

180+
# Returns true if this command's flags should be considered as gloal
181+
# flags, and cascade to subcommands
182+
def global_flags?
183+
flags.any? and commands.any?
184+
end
185+
180186
# Returns the string for the group caption
181187
def group_string
182188
if group
@@ -239,11 +245,12 @@ def summary_string
239245
# Returns a constructed string suitable for Usage pattern
240246
def usage_string
241247
result = [full_name]
242-
result << "[command]" if commands.any?
248+
result << "[OPTIONS]" if global_flags?
249+
result << "COMMAND" if commands.any?
243250
args.each do |arg|
244251
result << arg.usage_string
245252
end
246-
result << "[options]" unless flags.empty?
253+
result << "[OPTIONS]" unless flags.empty? || global_flags?
247254
result << catch_all.usage_string if catch_all.enabled?
248255
result.join " "
249256
end

lib/bashly/views/command/fixed_flags_filter.gtx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,12 @@ end
1616
> exit
1717
> ;;
1818
>
19+
20+
if global_flags?
21+
flags.each do |flag|
22+
= flag.render(:case)
23+
end
24+
end
25+
1926
> esac
2027
>

lib/bashly/views/command/parse_requirements_while.gtx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
> key="$1"
55
> case "$key" in
66

7-
flags.each do |flag|
8-
= flag.render(:case).indent 2
7+
unless global_flags?
8+
flags.each do |flag|
9+
= flag.render(:case).indent 2
10+
end
911
end
1012

1113
>

lib/bashly/views/command/usage.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ end
2929
> printf " {{ usage_string }}\n"
3030

3131
if commands.any?
32-
> printf " {{ full_name }} [command] --help{{ " | -h" unless short_flag_exist? "-h" }}\n"
32+
> printf " {{ full_name }} [COMMAND] --help{{ " | -h" unless short_flag_exist? "-h" }}\n"
3333
else
3434
> printf " {{ full_name }} --help{{ " | -h" unless short_flag_exist? "-h" }}\n"
3535
end

spec/approvals/examples/catch-all-advanced

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ run ./cli --help to test your bash script
99
cli - Sample application
1010

1111
Usage:
12-
cli [command]
13-
cli [command] --help | -h
12+
cli COMMAND
13+
cli [COMMAND] --help | -h
1414
cli --version | -v
1515

1616
Commands:
@@ -23,7 +23,7 @@ cli download - Download a file
2323
Alias: d
2424

2525
Usage:
26-
cli download SOURCE [TARGET] [options] [AWS PARAMS...]
26+
cli download SOURCE [TARGET] [OPTIONS] [AWS PARAMS...]
2727
cli download --help | -h
2828

2929
Options:

spec/approvals/examples/catch-all-stdin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ run ./cli --help to test your bash script
66
cli - Sample application
77

88
Usage:
9-
cli [options] [FILE...]
9+
cli [OPTIONS] [FILE...]
1010
cli --help | -h
1111
cli --version | -v
1212

spec/approvals/examples/command-aliases

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ run ./cli --help to test your bash script
99
cli - Sample application
1010

1111
Usage:
12-
cli [command]
13-
cli [command] --help | -h
12+
cli COMMAND
13+
cli [COMMAND] --help | -h
1414
cli --version | -v
1515

1616
Commands:
@@ -21,8 +21,8 @@ Commands:
2121
cli - Sample application
2222

2323
Usage:
24-
cli [command]
25-
cli [command] --help | -h
24+
cli COMMAND
25+
cli [COMMAND] --help | -h
2626
cli --version | -v
2727

2828
Commands:

spec/approvals/examples/command-default

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ run ./ftp --help to test your bash script
99
ftp - Sample application that uses the default command option
1010

1111
Usage:
12-
ftp [command]
13-
ftp [command] --help | -h
12+
ftp COMMAND
13+
ftp [COMMAND] --help | -h
1414
ftp --version | -v
1515

1616
Commands:
@@ -21,8 +21,8 @@ Commands:
2121
ftp - Sample application that uses the default command option
2222

2323
Usage:
24-
ftp [command]
25-
ftp [command] --help | -h
24+
ftp COMMAND
25+
ftp [COMMAND] --help | -h
2626
ftp --version | -v
2727

2828
Commands:

spec/approvals/examples/command-filenames

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ run ./cli --help to test your bash script
1111
cli - Demonstrate custom command filenames
1212

1313
Usage:
14-
cli [command]
15-
cli [command] --help | -h
14+
cli COMMAND
15+
cli [COMMAND] --help | -h
1616
cli --version | -v
1717

1818
Commands:
@@ -23,8 +23,8 @@ Commands:
2323
cli - Demonstrate custom command filenames
2424

2525
Usage:
26-
cli [command]
27-
cli [command] --help | -h
26+
cli COMMAND
27+
cli [COMMAND] --help | -h
2828
cli --version | -v
2929

3030
Commands:

0 commit comments

Comments
 (0)