dencli/bin/example.rb

59 lines
2.2 KiB
Ruby
Raw Normal View History

2020-12-13 17:06:00 +01:00
#!/usr/bin/env ruby
require 'pathname'
$:.unshift Pathname.new(__FILE__).dirname.dirname.join('lib').to_s
2020-12-13 17:06:00 +01:00
require 'dencli'
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:|
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").
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")
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)
2020-12-13 17:06:00 +01:00
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) }
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"}
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|
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" })
2020-12-13 17:06:00 +01:00
sub2.sub( :'sub-commands', "Endless Sub-Sub- ...") do |sub3|
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
begin
cli.call *ARGV
rescue DenCli::UsageError
STDERR.puts $!
exit 1
end