diff --git a/bin/example.rb b/bin/example.rb index 4651540..557cbff 100755 --- a/bin/example.rb +++ b/bin/example.rb @@ -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| diff --git a/lib/dencli/cmd.rb b/lib/dencli/cmd.rb index e5d80ff..b60c895 100644 --- a/lib/dencli/cmd.rb +++ b/lib/dencli/cmd.rb @@ -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 diff --git a/lib/dencli/sub.rb b/lib/dencli/sub.rb index 15207d5..16683d5 100644 --- a/lib/dencli/sub.rb +++ b/lib/dencli/sub.rb @@ -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 << " [ ...]" + else + output << " [...]" + 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 <).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