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",
|
2021-12-08 00:17:37 +01:00
|
|
|
&lambda {|a, b, c:, d:, e:, f:, g:|
|
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").
|
2021-12-08 00:17:37 +01:00
|
|
|
opt( :d, '-dForD', "Option d", default: "something").
|
2021-11-30 23:09:06 +01:00
|
|
|
opt( :e, '-e', "Toggle e", default: false).
|
2021-12-08 00:17:37 +01:00
|
|
|
opt( :f, '--[no-]f', "Toggle f", default: false).
|
|
|
|
opt( :g, '--long-option=sth', "Long option, no short option", default: "nothing").
|
|
|
|
opt( :h, '-hsth', "No long option, only short option", default: "nothing")
|
2021-11-30 23:09:06 +01:00
|
|
|
|
|
|
|
cli.cmd( :example, "I have an example command") { STDERR.puts "This is an example" }
|
2021-12-08 23:52:05 +01:00
|
|
|
cli.cmd( :help, "An example for help", aliases: [nil, '-h', '--help'], &lambda {|*commands, full:|
|
2021-11-30 23:09:06 +01:00
|
|
|
if full
|
2021-12-08 23:52:05 +01:00
|
|
|
cli.help_full *commands, output: STDERR
|
2021-11-30 23:09:06 +01:00
|
|
|
else
|
2021-12-08 23:52:05 +01:00
|
|
|
cli.help *commands, output: STDERR
|
2021-11-30 23:09:06 +01:00
|
|
|
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-12-08 23:52:05 +01:00
|
|
|
sub2.cmd( :help, "", aliases: [nil, '-h', '--help'], &lambda {|*commands| sub2.help( *commands, 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
|