dencli/bin/example.rb
Denis Knauf df15912e7a Sub#commands|CMD#commands should yield(name,self).
assert_type for debugging.  Commented out.
2021-12-07 23:48:28 +01:00

57 lines
2 KiB
Ruby
Executable file

#!/usr/bin/env ruby
require 'pathname'
$:.unshift Pathname.new(__FILE__).dirname.dirname.join('lib').to_s
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:|
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( :e, '-e', "Toggle e", default: false).
opt( :f, '--[no-]f', "Toggle f", default: false)
cli.cmd( :example, "I have an example command") { STDERR.puts "This is an example" }
cli.cmd( :help, "", aliases: [nil, '-h', '--help'], &lambda {|*args, full:|
if full
cli.help_full *args, output: STDERR
else
cli.help *args, output: STDERR
end
}).
opt( :full, '-f', '--[no-]full', "Print all commands and sub-commands.", default: false)
cli.sub( :more, "Sub-Commands are also possible with a new cli") do |sub|
sub.cmd( :help, "", aliases: [nil, '-h', '--help']) {|*args| STDERR.puts sub.help(*args) }
sub.cmd( :help, "") {|*args| STDERR.puts cli.help( 'more', *args) }
sub.cmd( :example, "Here is an example, too") { STDERR.puts "This is an other example" }
sub.cmd( :foo, "BAR") { STDERR.puts "FOO bar"}
sub.cmd( :args, "Expects and prints given arguments", &lambda {|a, b=1, c:, d: 5, e:|
p a: a, b: b, c: c, d: d, e: e
}).
opt( :c, '-c=ForC', "Option c").
opt( :d, '-d=ForD', "Option d (implicit default)").
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( :last, "The last example", &lambda { STDERR.puts "The last example" })
sub2.sub( :'sub-commands', "Endless Sub-Sub- ...") do |sub3|
sub3.cmd( :help, "") {|*args| STDERR.puts sub3.help( sub3, *args) }
sub3.cmd( :hehe, "The real last example", min: 2) { STDERR.puts "Trust me!" }
end
end
end
begin
cli.call *ARGV
rescue DenCli::UsageError
STDERR.puts $!
exit 1
end