Skip to content

Commit

Permalink
Add option parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Mar 29, 2020
1 parent 392c131 commit 30c65b9
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/tty/option/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative "parameter/argument"
require_relative "parameter/environment"
require_relative "parameter/keyword"
require_relative "parameter/option"
require_relative "parameters"

module TTY
Expand Down
55 changes: 55 additions & 0 deletions lib/tty/option/parameter/option.rb
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
212 changes: 212 additions & 0 deletions spec/unit/parameter/option_spec.rb
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

0 comments on commit 30c65b9

Please sign in to comment.