dencli/bin/example.rb

48 lines
1.8 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( :example, "I have an example command") { STDERR.puts "This is an example" }
cli.cmd( :help, "", aliases: [nil, '-h', '--help']) {|*args| STDERR.puts cli.help(*args) }
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)
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| STDERR.puts sub2.help(*args) })
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