From 421d9089621ba76c6a128b7391928105c8040c1a Mon Sep 17 00:00:00 2001 From: John W Higgins Date: Thu, 18 Apr 2019 14:54:24 -0700 Subject: [PATCH 1/2] Add :stopped to opts when stopped by on_stop --- lib/optimist.rb | 6 +++++- test/optimist/parser_test.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/optimist.rb b/lib/optimist.rb index 6ce667d..c4b8e0c 100644 --- a/lib/optimist.rb +++ b/lib/optimist.rb @@ -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 @@ -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] diff --git a/test/optimist/parser_test.rb b/test/optimist/parser_test.rb index 99d5c7b..e839b5c 100644 --- a/test/optimist/parser_test.rb +++ b/test/optimist/parser_test.rb @@ -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 From aeac344cd218bdeb551874e8d8f9806faf491911 Mon Sep 17 00:00:00 2001 From: John W Higgins Date: Thu, 23 May 2019 08:53:28 -0700 Subject: [PATCH 2/2] Change to :_stopped --- lib/optimist.rb | 2 +- test/optimist/parser_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/optimist.rb b/lib/optimist.rb index c4b8e0c..54165d3 100644 --- a/lib/optimist.rb +++ b/lib/optimist.rb @@ -351,7 +351,7 @@ def method_missing(m, *_args) self[m] || self[m.to_s] end end - vals[:stopped] = @stopped || false unless @stop_words.empty? + vals[:_stopped] = @stopped || false unless @stop_words.empty? vals end diff --git a/test/optimist/parser_test.rb b/test/optimist/parser_test.rb index e839b5c..b5cfb8f 100644 --- a/test/optimist/parser_test.rb +++ b/test/optimist/parser_test.rb @@ -880,13 +880,13 @@ def test_stopwords_stopped @p.stop_on %w(sub-command-1) opts = @p.parse %w(sub-command-1) - assert_equal true, opts[:stopped] + 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] + assert_equal false, opts[:_stopped] end def test_unknown_subcommand