Skip to content
Open
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
4 changes: 3 additions & 1 deletion lib/tty/option/parser/param_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Parser
module ParamTypes
# Positional argument pattern
ARGUMENT_PARAMETER = /^[^-][^=]*\z/.freeze
ARGUMENT_HARDCODED_STRING = /\s/.freeze

# Environment variable pattern
ENV_VAR_PARAMETER = /^[\p{Lu}_\-\d]+=/.freeze
Expand All @@ -24,7 +25,8 @@ module ParamTypes
#
# @api public
def argument?(value)
!value.match(ARGUMENT_PARAMETER).nil?
!value.match(ARGUMENT_PARAMETER).nil? ||
!value.match(ARGUMENT_HARDCODED_STRING).nil?
end

# Check if value is an environment variable
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

RSpec.describe TTY::Option do
context "argument" do
it "parses a hardcoded string with spaces as an argument" do
cmd = new_command do
argument :foo
end

cmd.parse(["bar TEST=something"])
expect(cmd.params[:foo]).to eq("bar TEST=something")
end

it "doesn't allow to register same name parameter" do
expect {
command do
Expand Down
56 changes: 30 additions & 26 deletions spec/unit/parser/param_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ def type

context "argument?" do
{
"--foo" => false,
"-f" => false,
"foo=bar" => false,
"foo" => true,
"f" => true,
"FOO=bar" => false
"--foo" => false,
"-f" => false,
"foo=bar" => false,
"foo" => true,
"f" => true,
"FOO=bar" => false,
"something FOO=bar --foo" => true
}.each do |input, result|
it "returns #{result} for #{input.inspect}" do
expect(type.argument?(input)).to eq(result)
Expand All @@ -24,13 +25,14 @@ def type

context "env_var?" do
{
"--foo" => false,
"-f" => false,
"foo=bar" => false,
"a=b" => false,
"foo" => false,
"FOO=bar" => true,
"A=b" => true
"--foo" => false,
"-f" => false,
"foo=bar" => false,
"a=b" => false,
"foo" => false,
"FOO=bar" => true,
"A=b" => true,
"something FOO=bar --foo" => false
}.each do |input, result|
it "returns #{result} for #{input.inspect}" do
expect(type.env_var?(input)).to eq(result)
Expand All @@ -40,13 +42,14 @@ def type

context "keyword?" do
{
"--foo" => false,
"-f" => false,
"-fa=b" => false,
"foo=bar" => true,
"a=b" => true,
"foo" => false,
"FOO=bar" => false
"--foo" => false,
"-f" => false,
"-fa=b" => false,
"foo=bar" => true,
"a=b" => true,
"foo" => false,
"FOO=bar" => false,
"something FOO=bar --foo" => false
}.each do |input, result|
it "returns #{result} for #{input.inspect}" do
expect(type.keyword?(input)).to eq(result)
Expand All @@ -56,12 +59,13 @@ def type

context "option?" do
{
"--foo" => true,
"-f" => true,
"foo=bar" => false,
"foo" => false,
"f" => false,
"FOO=bar" => false
"--foo" => true,
"-f" => true,
"foo=bar" => false,
"foo" => false,
"f" => false,
"FOO=bar" => false,
"something FOO=bar --foo" => false
}.each do |input, result|
it "returns #{result} for #{input.inspect}" do
expect(type.option?(input)).to eq(result)
Expand Down