-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
392c131
commit 30c65b9
Showing
3 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module TTY | ||
module Option | ||
class Parameter | ||
class Option < Parameter | ||
def short(value = (not_set = true)) | ||
if not_set | ||
@settings[:short] | ||
else | ||
@settings[:short] = value | ||
end | ||
end | ||
|
||
def short? | ||
@settings.key?(:short) && !@settings[:short].nil? | ||
end | ||
|
||
# Extract short flag name | ||
# | ||
# @api public | ||
def short_name | ||
short.to_s.sub(/^(-.).*$/, "\\1") | ||
end | ||
|
||
def long(value = (not_set = true)) | ||
if not_set | ||
@settings.fetch(:long) { default_long } | ||
else | ||
@settings[:long] = value | ||
end | ||
end | ||
|
||
def default_long | ||
"--#{name.to_s.gsub("_", "-")}" unless short? | ||
end | ||
|
||
def long? | ||
!long.nil? | ||
end | ||
|
||
# Extract long flag name | ||
# | ||
# @api public | ||
def long_name | ||
long.to_s.sub(/^(--.+?)(\s+|\=|\[).*$/, "\\1") | ||
end | ||
|
||
def required? | ||
@settings.fetch(:required) { false } | ||
end | ||
end # Option | ||
end # Parameter | ||
end # Option | ||
end # TTY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe TTY::Option::Parameter::Option do | ||
it "generates a default optional long name" do | ||
option = described_class.new(:foo) | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq(nil) | ||
expect(option.short?).to eq(false) | ||
expect(option.long).to eq("--foo") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
|
||
context "arity setting" do | ||
it "defaults to 1" do | ||
arg = described_class.new(:foo) | ||
expect(arg.arity).to eq(1) | ||
expect(arg.multiple?).to eq(false) | ||
end | ||
end | ||
|
||
context "default setting" do | ||
it "defaults to nil for shortcut option" do | ||
option = described_class.new(:foo) | ||
|
||
expect(option.default).to eq(nil) | ||
end | ||
|
||
it "defaults to nil for options without arguments" do | ||
option = described_class.new(:foo, short: "-f", long: "--foo") | ||
|
||
expect(option.default).to eq(nil) | ||
end | ||
|
||
it "defaults to nil for options with arguments" do | ||
option = described_class.new(:foo, short: "-f", long: "--foo string") | ||
|
||
expect(option.default).to eq(nil) | ||
end | ||
end | ||
|
||
context "short setting" do | ||
it "extracts a short name without argument" do | ||
option = described_class.new(:foo, short: "-f") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq(nil) | ||
expect(option.long_name).to eq("") | ||
expect(option.long?).to eq(false) | ||
end | ||
|
||
it "extracts a short name with argument" do | ||
option = described_class.new(:foo, short: "-f string") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-f string") | ||
expect(option.short_name).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq(nil) | ||
expect(option.long_name).to eq("") | ||
expect(option.long?).to eq(false) | ||
end | ||
|
||
it "extracts a short name with argument glued together" do | ||
option = described_class.new(:foo, short: "-fstring") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-fstring") | ||
expect(option.short_name).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq(nil) | ||
expect(option.long_name).to eq("") | ||
expect(option.long?).to eq(false) | ||
end | ||
|
||
it "extracts a short name with an optional argument" do | ||
option = described_class.new(:foo, short: "-f [string]") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-f [string]") | ||
expect(option.short_name).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq(nil) | ||
expect(option.long?).to eq(false) | ||
end | ||
|
||
it "extracts a short name with an optional argument glued together" do | ||
option = described_class.new(:foo, short: "-f[string]") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-f[string]") | ||
expect(option.short_name).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq(nil) | ||
expect(option.long?).to eq(false) | ||
end | ||
end | ||
|
||
context "long setting" do | ||
it "extracts a long name without argument" do | ||
option = described_class.new(:foo, long: "--foo") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq(nil) | ||
expect(option.short?).to eq(false) | ||
expect(option.long).to eq("--foo") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
|
||
it "extracts a long name with argument" do | ||
option = described_class.new(:foo, long: "--foo string") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq(nil) | ||
expect(option.short_name).to eq("") | ||
expect(option.short?).to eq(false) | ||
expect(option.long).to eq("--foo string") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
|
||
it "extracts a long name with argument separted with =" do | ||
option = described_class.new(:foo, long: "--foo=string") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq(nil) | ||
expect(option.short_name).to eq("") | ||
expect(option.short?).to eq(false) | ||
expect(option.long).to eq("--foo=string") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
|
||
it "extracts a long name with an optional argument" do | ||
option = described_class.new(:foo, long: "--foo [string]") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq(nil) | ||
expect(option.short_name).to eq("") | ||
expect(option.short?).to eq(false) | ||
expect(option.long).to eq("--foo [string]") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
|
||
it "extracts a short name with an optional argument glued together" do | ||
option = described_class.new(:foo, long: "--foo[string]") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq(nil) | ||
expect(option.short_name).to eq("") | ||
expect(option.short?).to eq(false) | ||
expect(option.long).to eq("--foo[string]") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
end | ||
|
||
context "short & long setting" do | ||
it "extracts a short & long name with a required argument for long option" do | ||
option = described_class.new(:foo, short: "-f", long: "--foo string") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-f") | ||
expect(option.short_name).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq("--foo string") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
|
||
it "extracts a short & long name with a required argument for short option" do | ||
option = described_class.new(:foo, short: "-f string", long: "--foo") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-f string") | ||
expect(option.short_name).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq("--foo") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
|
||
it "extracts a short & long name with an optional argument for long option" do | ||
option = described_class.new(:foo, short: "-f", long: "--foo [string]") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-f") | ||
expect(option.short_name).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq("--foo [string]") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
|
||
it "extracts a short & long name with an optional argument for short option" do | ||
option = described_class.new(:foo, short: "-f [string]", long: "--foo") | ||
|
||
expect(option.name).to eq(:foo) | ||
expect(option.short).to eq("-f [string]") | ||
expect(option.short_name).to eq("-f") | ||
expect(option.short?).to eq(true) | ||
expect(option.long).to eq("--foo") | ||
expect(option.long_name).to eq("--foo") | ||
expect(option.long?).to eq(true) | ||
end | ||
end | ||
end |