Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :stopped to opts when stopped by on_stop #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/optimist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ def method_missing(m, *_args)
self[m] || self[m.to_s]
end
end
vals[:_stopped] = @stopped || false unless @stop_words.empty?
vals
end

Expand Down Expand Up @@ -448,7 +449,10 @@ def each_arg(args)
i = 0

until i >= args.length
return remains += args[i..-1] if @stop_words.member? args[i]
if @stop_words.member? args[i]
@stopped = true
return remains += args[i..-1]
end
case args[i]
when /^--$/ # arg terminator
return remains += args[(i + 1)..-1]
Expand Down
13 changes: 13 additions & 0 deletions test/optimist/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,19 @@ def test_stopwords_with_short_args
assert_equal @q.leftovers, []
end

def test_stopwords_stopped
@p.stop_on %w(sub-command-1)
opts = @p.parse %w(sub-command-1)

assert_equal true, opts[:_stopped]

@q = Parser.new
@q.stop_on %w(sub-command-1)
opts = @q.parse %w(sub-command-2)

assert_equal false, opts[:_stopped]
end

def test_unknown_subcommand
@p.opt :global_flag, "Global flag", :short => "-g", :type => :flag
@p.opt :global_param, "Global parameter", :short => "-p", :default => 5
Expand Down