From d2158e08ea1a92c4d9bfbdb34a493c6230351405 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 8 Dec 2021 00:17:37 +0100 Subject: [PATCH] Usage of short options without `=` --- bin/example.rb | 8 +++++--- lib/dencli/cmd.rb | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bin/example.rb b/bin/example.rb index 4291c54..4651540 100755 --- a/bin/example.rb +++ b/bin/example.rb @@ -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:| diff --git a/lib/dencli/cmd.rb b/lib/dencli/cmd.rb index 48aff2c..e5d80ff 100644 --- a/lib/dencli/cmd.rb +++ b/lib/dencli/cmd.rb @@ -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}"