Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/custom-strings/src/bashly-strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ help_flag_text: Show this helpful help
version_flag_text: Show version number

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

4 changes: 4 additions & 0 deletions lib/bashly/models/flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def aliases
end
end

def name
long || short
end

def usage_string(extended: false)
result = [aliases.join(", ")]
result << arg.upcase if arg
Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/templates/strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ help_flag_text: Show this help
version_flag_text: Show version number

# Error messages
flag_requires_an_argument: "%{long} requires an argument: %{usage}"
flag_requires_an_argument: "%{name} requires an argument: %{usage}"
invalid_argument: "invalid argument: %s"
invalid_flag: "invalid option: %s"
missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
Expand Down
6 changes: 3 additions & 3 deletions lib/bashly/views/flag/case.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<%= aliases.join " | " %> )
<%- if arg -%>
if [[ $2 && $2 != -* ]]; then
args[<%= long %>]="$2"
args[<%= name %>]="$2"
shift
shift
else
printf "%s\n" "<%= strings[:flag_requires_an_argument] % { long: long, usage: usage_string } %>"
printf "%s\n" "<%= strings[:flag_requires_an_argument] % { name: name, usage: usage_string } %>"
exit 1
fi
<%- else -%>
args[<%= long %>]=1
args[<%= name %>]=1
shift
<%- end -%>
;;
22 changes: 22 additions & 0 deletions spec/bashly/models/flag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
end
end

describe '#name' do
context "with both short and long options" do
it "returns the long option" do
expect(subject.name).to eq "--help"
end
end

context "with long option only" do
let(:options) { {"long" => "-l"} }
it "returns the long option" do
expect(subject.name).to eq "-l"
end
end

context "with short option only" do
let(:options) { {"short" => "-s"} }
it "returns the short option" do
expect(subject.name).to eq "-s"
end
end
end

describe '#usage_string' do
it "returns a string suitable to be used as a usage pattern" do
expect(subject.usage_string).to eq "--help, -h"
Expand Down