Skip to content

Commit

Permalink
Merge pull request #155 from nanobowers/explicit_short_options
Browse files Browse the repository at this point in the history
added settings to only create short options explicitly
  • Loading branch information
Fryguy committed May 10, 2024
2 parents 6944cdb + 60f2f2e commit 31b1099
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/optimist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
implicit_short_opts: true,
suggestions: true
}

## Initializes the parser, and instance-evaluates any block given.
def initialize(*a, &b)
Expand Down Expand Up @@ -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! if @settings[:implicit_short_opts]

## resolve symbols
given_args = {}
Expand Down Expand Up @@ -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+
## * :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
Expand Down
33 changes: 33 additions & 0 deletions test/optimist/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(implicit_short_opts: false)
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 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
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
Expand Down

0 comments on commit 31b1099

Please sign in to comment.