2020-12-13 17:06:00 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2021-11-30 15:23:05 +01:00
|
|
|
require 'pathname'
|
|
|
|
$:.unshift Pathname.new(__FILE__).dirname.dirname.join('lib').to_s
|
2020-12-13 17:06:00 +01:00
|
|
|
require 'dencli'
|
|
|
|
|
2021-12-07 23:48:28 +01:00
|
|
|
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:|
|
2021-11-30 15:23:05 +01:00
|
|
|
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").
|
2021-11-30 23:09:06 +01:00
|
|
|
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)
|
2021-11-30 15:23:05 +01:00
|
|
|
|
2020-12-13 17:06:00 +01:00
|
|
|
cli.sub( :more, "Sub-Commands are also possible with a new cli") do |sub|
|
2021-11-30 15:23:05 +01:00
|
|
|
sub.cmd( :help, "", aliases: [nil, '-h', '--help']) {|*args| STDERR.puts sub.help(*args) }
|
2020-12-13 17:06:00 +01:00
|
|
|
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"}
|
|
|
|
|
2021-11-30 15:23:05 +01:00
|
|
|
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")
|
|
|
|
|
2020-12-13 17:06:00 +01:00
|
|
|
sub.sub( :deeper, "You want to have Sub-Sub-Commands?") do |sub2|
|
2021-11-30 23:09:06 +01:00
|
|
|
sub2.cmd( :help, "", aliases: [nil, '-h', '--help'], &lambda {|*args| sub2.help( *args, output: STDERR) })
|
2021-11-30 15:23:05 +01:00
|
|
|
sub2.cmd( :last, "The last example", &lambda { STDERR.puts "The last example" })
|
2020-12-13 17:06:00 +01:00
|
|
|
|
|
|
|
sub2.sub( :'sub-commands', "Endless Sub-Sub- ...") do |sub3|
|
2021-11-30 15:23:05 +01:00
|
|
|
sub3.cmd( :help, "") {|*args| STDERR.puts sub3.help( sub3, *args) }
|
2020-12-13 17:06:00 +01:00
|
|
|
sub3.cmd( :hehe, "The real last example", min: 2) { STDERR.puts "Trust me!" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-30 15:23:05 +01:00
|
|
|
begin
|
|
|
|
cli.call *ARGV
|
|
|
|
rescue DenCli::UsageError
|
|
|
|
STDERR.puts $!
|
|
|
|
exit 1
|
|
|
|
end
|