ruby-2.5-support fix. usage with rest-arguments.

ruby-2.5 does not support `String#split() {|t|}`.  splits are replaced
by `String#split().each {|t|}`.

Usage supports printing rest-arguments.  It uses the name with three dots:

  command [<argname> ...]

Sub-commands has a generic `<command> ...` or `[<command> ...]`.
The last will be used if a nil-aliases exists.
master
Denis Knauf 2021-12-08 23:52:05 +01:00
parent 6e253b8b11
commit 3cc205cb02
3 changed files with 58 additions and 37 deletions

View File

@ -17,11 +17,11 @@ cli.cmd( :args, "Expects and prints given arguments",
opt( :h, '-hsth', "No long option, only 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( :example, "I have an example command") { STDERR.puts "This is an example" }
cli.cmd( :help, "", aliases: [nil, '-h', '--help'], &lambda {|*args, full:| cli.cmd( :help, "An example for help", aliases: [nil, '-h', '--help'], &lambda {|*commands, full:|
if full if full
cli.help_full *args, output: STDERR cli.help_full *commands, output: STDERR
else else
cli.help *args, output: STDERR cli.help *commands, output: STDERR
end end
}). }).
opt( :full, '-f', '--[no-]full', "Print all commands and sub-commands.", default: false) opt( :full, '-f', '--[no-]full', "Print all commands and sub-commands.", default: false)
@ -40,7 +40,7 @@ cli.sub( :more, "Sub-Commands are also possible with a new cli") do |sub|
opt( :e, '-e', "Toggle e") opt( :e, '-e', "Toggle e")
sub.sub( :deeper, "You want to have Sub-Sub-Commands?") do |sub2| sub.sub( :deeper, "You want to have Sub-Sub-Commands?") do |sub2|
sub2.cmd( :help, "", aliases: [nil, '-h', '--help'], &lambda {|*args| sub2.help( *args, output: STDERR) }) sub2.cmd( :help, "", aliases: [nil, '-h', '--help'], &lambda {|*commands| sub2.help( *commands, output: STDERR) })
sub2.cmd( :last, "The last example", &lambda { STDERR.puts "The last example" }) sub2.cmd( :last, "The last example", &lambda { STDERR.puts "The last example" })
sub2.sub( :'sub-commands', "Endless Sub-Sub- ...") do |sub3| sub2.sub( :'sub-commands', "Endless Sub-Sub- ...") do |sub3|

View File

@ -76,8 +76,16 @@ class DenCli::CMD
output << (o.required? ? " #{s}" : " [#{s}]") output << (o.required? ? " #{s}" : " [#{s}]")
end end
if @exe.lambda? if @exe.lambda?
required.each {|s| output << " <#{s}>" } parameters.each do |(type, name)|
output << " [#{additional.map{|s|"<#{s}>"}.join " "}]" unless additional.empty? case type
when :req
output << " <#{name}>"
when :opt
output << " [<#{name}>]"
when :rest
output << " [<#{name}> ...]"
end
end
else else
output << ' ...' output << ' ...'
end end
@ -130,7 +138,7 @@ class DenCli::CMD
@options.map do |_, o| @options.map do |_, o|
s, l, v, y = o.short, o.long, o.val, ',' s, l, v, y = o.short, o.long, o.val, ','
if l.nil? if l.nil?
s += "=#{v}" if v s += "#{v}" if v
y = ' ' y = ' '
elsif s.nil? elsif s.nil?
l += "=#{v}" if v l += "=#{v}" if v

View File

@ -14,8 +14,19 @@ class DenCli::Sub
def full_cmd() _full_cmd [] end def full_cmd() _full_cmd [] end
def []( k) @aliases[k] end def []( k) @aliases[k] end
def usage def usage output: nil
"#{full_cmd.join ' '} ..." output ||= ''
_usage output
output
end
def _usage output
output << full_cmd.join( ' ')
if @aliases.has_key? nil
output << " [<command> ...]"
else
output << " <command> [...]"
end
end end
def help n = nil, *a, output: nil def help n = nil, *a, output: nil
@ -46,37 +57,39 @@ class DenCli::Sub
output output
end end
def self._help_commands output, subs class <<self
m = subs.map {|_name,c| x = c.usage.length; 25 < x ? 0 : x }.max def _help_commands output, subs
subs.each do |_name, c| m = subs.map {|_name,c| x = c.usage.length; 25 < x ? 0 : x }.max
if 25 < c.usage.length subs.each do |_name, c|
output << "% -#{m}s\n#{' ' * m} " % [c.usage] if 25 < c.usage.length
else output << "% -#{m}s\n#{' ' * m} " % [c.usage]
output << "% -#{m}s " % [c.usage] else
end output << "% -#{m}s " % [c.usage]
n = m+2
prefix = nil
c.description.split /\n/ do |l|
c = 0
l.split %r< > do |w|
if prefix
output << prefix
prefix = nil
end
wl = w.length
if 75 < c+wl
output << "\n#{' ' * n}#{w}"
c = n+2+wl
else
output << " #{w}"
c += 1 + wl
end
end end
prefix = "\n#{' ' * n}" n = m+2
prefix = nil
c.description.split( /\n/).each do |l|
c = 0
l.split( %r< >).each do |w|
if prefix
output << prefix
prefix = nil
end
wl = w.length
if 75 < c+wl
output << "\n#{' ' * n}#{w}"
c = n+2+wl
else
output << " #{w}"
c += 1 + wl
end
end
prefix = "\n#{' ' * n}"
end
output << "\n"
end end
output << "\n" output
end end
output
end end
def goto *a def goto *a