Usage of short options without `=`

master
Denis Knauf 2021-12-08 00:17:37 +01:00
parent ffd24c02f8
commit d2158e08ea
2 changed files with 8 additions and 6 deletions

View File

@ -6,13 +6,15 @@ require 'dencli'
cli = DenCli.new :example, "This is an example for generate a DenCli-API"
cli.cmd( :args, "Expects and prints given arguments",
&lambda {|a, b, c:, d:, e:|
&lambda {|a, b, c:, d:, e:, f:, g:|
p a: a, b: b, c: c, d: d, e: e
}).
opt( :c, '-c=ForC', "Option c").
opt( :d, '-d=ForD', "Option d", default: "something").
opt( :d, '-dForD', "Option d", default: "something").
opt( :e, '-e', "Toggle e", default: false).
opt( :f, '--[no-]f', "Toggle f", default: false)
opt( :f, '--[no-]f', "Toggle f", default: false).
opt( :g, '--long-option=sth', "Long option, no short option", default: "nothing").
opt( :h, '-hsth', "No long option, only short option", default: "nothing")
cli.cmd( :example, "I have an example command") { STDERR.puts "This is an example" }
cli.cmd( :help, "", aliases: [nil, '-h', '--help'], &lambda {|*args, full:|

View File

@ -72,7 +72,7 @@ class DenCli::CMD
def _usage output
output << full_cmd.join( ' ')
@options.each do |_, o|
s = "#{o.short||o.long}#{o.val ? ?= : ''}#{o.val}"
s = "#{o.short||o.long}#{(!o.short && o.val) ? ?= : ''}#{o.val}"
output << (o.required? ? " #{s}" : " [#{s}]")
end
if @exe.lambda?
@ -161,9 +161,9 @@ class DenCli::CMD
@long, @val = $1, $2 || @val
when /\A(--[^=]+)\z/
@long, @val = $1, nil
when /\A(-[^=-]+)=(.+)\z/
when /\A(-[^=-])=?(.+)\z/
@short, @val = $1, $2 || @val
when /\A(-[^=-]+)\z/
when /\A(-[^=-])\z/
@short, @val = $1, nil
else
raise ArgumentError, "Unexpected format for option: #{opt.inspect}"