Skip to content

Allow arguments to be hardcoded strings with - and = characters #14

@AlexB52

Description

@AlexB52

Describe the problem

I'm looking to define options with arguments that can take a command to run in a process. Something like this

my-cli 'rake TEST=<test>'

arguments using = character aren't parsed as arguments and get set to nil.

Steps to reproduce the problem

require "bundler/inline"

gemfile do
  source "https://rubygems.org"

  gem "tty-option"
  gem "minitest"
end

require "tty-option"
require "minitest/autorun"

class MyOptions
  include TTY::Option

  argument :command
end

class TestHardcodedString < Minitest::Test
  def test_special_characters_as_arguments
    options = MyOptions.new
    options.parse(["rake TEST=<test>"])

    # ASSERTION FAILS
    assert_equal "rake TEST=<test>", options.params[:command]
  end

  def test_reference_without_equal_sign
    options = MyOptions.new
    options.parse(["rake test <test>"])

    # ASSERTION PASSES
    assert_equal "rake test <test>", options.params[:command]
  end
end

Result

# Running:

.F

Finished in 0.001068s, 1872.6592 runs/s, 1872.6592 assertions/s.

  1) Failure:
TestHardcodedString#test_special_characters_as_arguments [tty-option-hardcoded-string-for-command.rb:25]:
Expected: "rake TEST=<test>"
  Actual: nil

2 runs, 2 assertions, 1 failures, 0 errors, 0 skips

Actual behaviour
options.params[:command] => nil

Expected behaviour
options.params[:command] => 'rake TEST=<test>'

Context

It currently happens because the regex for arguments prevents the use of = and - characters
https://github.com/piotrmurach/tty-option/blob/master/lib/tty/option/parser/param_types.rb#L8

While the pull request #13 is not the right fix it does show tests that illustrate why the regex does not parse and = and - characters

Describe your environment

  • OS version: mac 14.2.1
  • Ruby version: 3.1
  • TTY::Option version: 0.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions