Skip to content

Commit 0523fa8

Browse files
committed
- Allow using short name only for flags and change '%{long}' in flag translation to '%{name}'
1 parent cafbd4b commit 0523fa8

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

examples/custom-strings/src/bashly-strings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ help_flag_text: Show this helpful help
99
version_flag_text: Show version number
1010

1111
# Error messages
12-
flag_requires_an_argument: "Hey! the flag %{long} requires an argument: %{usage}"
12+
flag_requires_an_argument: "Hey! the flag %{name} requires an argument: %{usage}"
1313
missing_required_argument: "Boom! a required argument is missing: %{arg}\\nusage: %{usage}"
1414
missing_required_flag: "Yo! you forgot a flag: %{usage}"
1515

lib/bashly/models/flag.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def aliases
1111
end
1212
end
1313

14+
def name
15+
long || short
16+
end
17+
1418
def usage_string(extended: false)
1519
result = [aliases.join(", ")]
1620
result << arg.upcase if arg

lib/bashly/templates/strings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ help_flag_text: Show this help
2121
version_flag_text: Show version number
2222

2323
# Error messages
24-
flag_requires_an_argument: "%{long} requires an argument: %{usage}"
24+
flag_requires_an_argument: "%{name} requires an argument: %{usage}"
2525
invalid_argument: "invalid argument: %s"
2626
invalid_flag: "invalid option: %s"
2727
missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"

lib/bashly/views/flag/case.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<%= aliases.join " | " %> )
33
<%- if arg -%>
44
if [[ $2 && $2 != -* ]]; then
5-
args[<%= long %>]="$2"
5+
args[<%= name %>]="$2"
66
shift
77
shift
88
else
9-
printf "%s\n" "<%= strings[:flag_requires_an_argument] % { long: long, usage: usage_string } %>"
9+
printf "%s\n" "<%= strings[:flag_requires_an_argument] % { name: name, usage: usage_string } %>"
1010
exit 1
1111
fi
1212
<%- else -%>
13-
args[<%= long %>]=1
13+
args[<%= name %>]=1
1414
shift
1515
<%- end -%>
1616
;;

spec/bashly/models/flag_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@
2626
end
2727
end
2828

29+
describe '#name' do
30+
context "with both short and long options" do
31+
it "returns the long option" do
32+
expect(subject.name).to eq "--help"
33+
end
34+
end
35+
36+
context "with long option only" do
37+
let(:options) { {"long" => "-l"} }
38+
it "returns the long option" do
39+
expect(subject.name).to eq "-l"
40+
end
41+
end
42+
43+
context "with short option only" do
44+
let(:options) { {"short" => "-s"} }
45+
it "returns the short option" do
46+
expect(subject.name).to eq "-s"
47+
end
48+
end
49+
end
50+
2951
describe '#usage_string' do
3052
it "returns a string suitable to be used as a usage pattern" do
3153
expect(subject.usage_string).to eq "--help, -h"

0 commit comments

Comments
 (0)