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.
This commit is contained in:
parent
6e253b8b11
commit
3cc205cb02
|
@ -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|
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,7 +57,8 @@ class DenCli::Sub
|
|||
output
|
||||
end
|
||||
|
||||
def self._help_commands output, subs
|
||||
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
|
||||
|
@ -56,9 +68,9 @@ class DenCli::Sub
|
|||
end
|
||||
n = m+2
|
||||
prefix = nil
|
||||
c.description.split /\n/ do |l|
|
||||
c.description.split( /\n/).each do |l|
|
||||
c = 0
|
||||
l.split %r< > do |w|
|
||||
l.split( %r< >).each do |w|
|
||||
if prefix
|
||||
output << prefix
|
||||
prefix = nil
|
||||
|
@ -78,6 +90,7 @@ class DenCli::Sub
|
|||
end
|
||||
output
|
||||
end
|
||||
end
|
||||
|
||||
def goto *a
|
||||
return self if a.empty?
|
||||
|
|
Loading…
Reference in a new issue