From 45b472544d60ee026a120816518e69e2395f8746 Mon Sep 17 00:00:00 2001 From: Ben Bowers Date: Sun, 5 May 2024 09:00:53 -0400 Subject: [PATCH 1/2] added settings to only create short options explicitly --- lib/optimist.rb | 9 +++++++-- test/optimist/parser_test.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/optimist.rb b/lib/optimist.rb index 4f617a9..80e4aef 100644 --- a/lib/optimist.rb +++ b/lib/optimist.rb @@ -84,7 +84,11 @@ def self.registry_getopttype(type) ## ignore options that it does not recognize. attr_accessor :ignore_invalid_options - DEFAULT_SETTINGS = { suggestions: true, exact_match: true } + DEFAULT_SETTINGS = { + exact_match: true, + explicit_short_opts: false, + suggestions: true + } ## Initializes the parser, and instance-evaluates any block given. def initialize(*a, &b) @@ -313,7 +317,7 @@ def parse(cmdline = ARGV) vals[sym] = [] if opts.multi && !opts.default # multi arguments default to [], not nil end - resolve_default_short_options! + resolve_default_short_options! unless @settings[:explicit_short_opts] ## resolve symbols given_args = {} @@ -1027,6 +1031,7 @@ def multi_arg? ; true ; end ## +settings+ include: ## * :exact_match : (default=true) Allow minimum unambigous number of characters to match a long option ## * :suggestions : (default=true) Enables suggestions when unknown arguments are given and DidYouMean is installed. DidYouMean comes standard with Ruby 2.3+ +## * :explicit_short_opts : (default=false) Short options will only be created where explicitly defined. If you do not like short-options, this will prevent having to define :short=> :none for all of your options. ## Because Optimist::options uses a default argument for +args+, you must pass that argument when using the settings feature. ## ## See more examples at https://www.manageiq.org/optimist diff --git a/test/optimist/parser_test.rb b/test/optimist/parser_test.rb index c07adda..8c00b0a 100644 --- a/test/optimist/parser_test.rb +++ b/test/optimist/parser_test.rb @@ -1177,6 +1177,39 @@ def test_default_shorts_assigned_only_after_user_shorts assert opts[:ccd] end + def test_short_opts_not_implicitly_created + newp = Parser.new(explicit_short_opts: true) + newp.opt :user1, "user1" + newp.opt :bag, "bag", :short => 'b' + assert_raises(CommandlineError) do + newp.parse %w(-u) + end + opts = newp.parse %w(--user1) + assert opts[:user1] + opts = newp.parse %w(-b) + assert opts[:bag] + end + + def test_short_opts_not_implicit_help_ver + # When explicit_short_opts is enabled this extends + # to the built-in help/version also. + newp = Parser.new(explicit_short_opts: true) + newp.opt :abc, "abc" + newp.version "3.4.5" + assert_raises(CommandlineError) do + newp.parse %w(-h) + end + assert_raises(CommandlineError) do + newp.parse %w(-v) + end + assert_raises(HelpNeeded) do + newp.parse %w(--help) + end + assert_raises(VersionNeeded) do + newp.parse %w(--version) + end + end + def test_inexact_match newp = Parser.new(exact_match: false) newp.opt :liberation, "liberate something", :type => :int From 60f2f2e97391ceed4230685bb26d0a837d147917 Mon Sep 17 00:00:00 2001 From: Ben Bowers Date: Fri, 10 May 2024 07:25:40 -0400 Subject: [PATCH 2/2] Flip polarity of explicit_short_opts: false to implicit_short_opts: true --- lib/optimist.rb | 6 +++--- test/optimist/parser_test.rb | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/optimist.rb b/lib/optimist.rb index 80e4aef..8ac4f6c 100644 --- a/lib/optimist.rb +++ b/lib/optimist.rb @@ -86,7 +86,7 @@ def self.registry_getopttype(type) DEFAULT_SETTINGS = { exact_match: true, - explicit_short_opts: false, + implicit_short_opts: true, suggestions: true } @@ -317,7 +317,7 @@ def parse(cmdline = ARGV) vals[sym] = [] if opts.multi && !opts.default # multi arguments default to [], not nil end - resolve_default_short_options! unless @settings[:explicit_short_opts] + resolve_default_short_options! if @settings[:implicit_short_opts] ## resolve symbols given_args = {} @@ -1031,7 +1031,7 @@ def multi_arg? ; true ; end ## +settings+ include: ## * :exact_match : (default=true) Allow minimum unambigous number of characters to match a long option ## * :suggestions : (default=true) Enables suggestions when unknown arguments are given and DidYouMean is installed. DidYouMean comes standard with Ruby 2.3+ -## * :explicit_short_opts : (default=false) Short options will only be created where explicitly defined. If you do not like short-options, this will prevent having to define :short=> :none for all of your options. +## * :implicit_short_opts : (default=true) Short options will only be created where explicitly defined. If you do not like short-options, this will prevent having to define :short=> :none for all of your options. ## Because Optimist::options uses a default argument for +args+, you must pass that argument when using the settings feature. ## ## See more examples at https://www.manageiq.org/optimist diff --git a/test/optimist/parser_test.rb b/test/optimist/parser_test.rb index 8c00b0a..dce588f 100644 --- a/test/optimist/parser_test.rb +++ b/test/optimist/parser_test.rb @@ -1178,7 +1178,7 @@ def test_default_shorts_assigned_only_after_user_shorts end def test_short_opts_not_implicitly_created - newp = Parser.new(explicit_short_opts: true) + newp = Parser.new(implicit_short_opts: false) newp.opt :user1, "user1" newp.opt :bag, "bag", :short => 'b' assert_raises(CommandlineError) do @@ -1191,9 +1191,9 @@ def test_short_opts_not_implicitly_created end def test_short_opts_not_implicit_help_ver - # When explicit_short_opts is enabled this extends - # to the built-in help/version also. - newp = Parser.new(explicit_short_opts: true) + # When implicit_short_opts is false this implies the short options + # for the built-in help/version are also not created. + newp = Parser.new(implicit_short_opts: false) newp.opt :abc, "abc" newp.version "3.4.5" assert_raises(CommandlineError) do