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")
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
cli.help_full *args, output: STDERR
cli.help_full *commands, output: STDERR
else
cli.help *args, output: STDERR
cli.help *commands, output: STDERR
end
}).
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")
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.sub( :'sub-commands', "Endless Sub-Sub- ...") do |sub3|

View File

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

View File

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