2020-12-13 17:06:00 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2021-11-30 15:23:05 +01:00
|
|
|
require 'pathname'
|
2021-12-09 13:02:12 +01:00
|
|
|
require 'shellwords'
|
|
|
|
require 'stringio'
|
2021-12-31 13:55:27 +01:00
|
|
|
require 'ipaddr'
|
2021-12-31 13:47:54 +01:00
|
|
|
require_relative '../lib/dencli'
|
2020-12-13 17:06:00 +01:00
|
|
|
|
2021-12-07 23:48:28 +01:00
|
|
|
cli = DenCli.new :example, "This is an example for generate a DenCli-API"
|
2021-12-09 13:02:12 +01:00
|
|
|
|
|
|
|
class Capture
|
|
|
|
def initialize cli, verbose:
|
|
|
|
@cli, @counter, @verbose = cli, 0, verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def capture
|
|
|
|
@args = NilClass
|
|
|
|
@counter += 1
|
|
|
|
stdout, stderr = $stdout, $stderr
|
|
|
|
$stdout = $stderr = StringIO.new
|
|
|
|
begin
|
|
|
|
yield stdout, stderr
|
|
|
|
ensure
|
|
|
|
$stderr, $stdout = stderr, stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def args= args
|
|
|
|
@args = args
|
|
|
|
end
|
|
|
|
|
|
|
|
def logstart command
|
|
|
|
STDERR.printf "[% 4d] \e[1;35m? \e[0m %s tests %s\r", @counter, $0.shellescape, command.shelljoin
|
|
|
|
end
|
|
|
|
|
2021-12-31 13:47:54 +01:00
|
|
|
def logok info, command
|
|
|
|
STDERR.printf "[% 4d] \e[1;32mok\e[0m %s | %s tests %s\e[J\n", @counter, info, $0.shellescape, command.shelljoin
|
2021-12-09 13:02:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def logfail command
|
|
|
|
STDERR.printf "[% 4d] \e[1;31mer\e[0m %s tests %s\e[J\n", @counter, $0.shellescape, command.shelljoin
|
|
|
|
end
|
|
|
|
|
2021-12-09 14:11:45 +01:00
|
|
|
def logexception prefix, exception
|
|
|
|
loginfo "#{prefix} (#{exception.class.name}) #{exception}"
|
|
|
|
exception.backtrace[0...-Kernel.caller.length].each {|l| loginfo " #{l}" }
|
|
|
|
end
|
|
|
|
|
2021-12-09 13:02:12 +01:00
|
|
|
def loginfo text
|
|
|
|
STDERR.printf " %s\n", text
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_ok expect, *command
|
|
|
|
logstart command
|
|
|
|
$capture.capture { @cli.call 'tests', *command }
|
|
|
|
if expect === @args
|
2021-12-31 13:47:54 +01:00
|
|
|
logok @args, command
|
2021-12-09 13:02:12 +01:00
|
|
|
else
|
|
|
|
logfail command
|
|
|
|
loginfo "expected args: #{expect.inspect}"
|
|
|
|
loginfo "given args: #{@args.inspect}"
|
|
|
|
STDERR.puts
|
|
|
|
end
|
|
|
|
rescue SystemExit
|
|
|
|
if 0 == $!.status
|
2021-12-31 13:47:54 +01:00
|
|
|
logok @args, command
|
2021-12-09 13:02:12 +01:00
|
|
|
else
|
|
|
|
logfail command
|
|
|
|
end
|
|
|
|
rescue Object
|
|
|
|
logfail command
|
2021-12-09 14:11:45 +01:00
|
|
|
logexception "unexpected raise:", $!
|
2021-12-09 13:02:12 +01:00
|
|
|
STDERR.puts
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_fail exception, message, *command
|
|
|
|
logstart command
|
|
|
|
$capture.capture { @cli.call 'tests', *command }
|
|
|
|
logfail command
|
|
|
|
rescue exception
|
|
|
|
if message === $!.message
|
2021-12-31 13:47:54 +01:00
|
|
|
logok exception, command
|
2021-12-09 13:02:12 +01:00
|
|
|
if @verbose
|
2021-12-09 14:11:45 +01:00
|
|
|
logexception "raised:", $!
|
2021-12-09 13:02:12 +01:00
|
|
|
STDERR.puts
|
|
|
|
end
|
|
|
|
else
|
2021-12-09 14:11:45 +01:00
|
|
|
logexception "unexpected message:", $!
|
2021-12-09 13:02:12 +01:00
|
|
|
STDERR.puts
|
|
|
|
end
|
|
|
|
rescue Object
|
|
|
|
logfail command
|
2021-12-09 14:11:45 +01:00
|
|
|
logexception "unexpected raised:", $!
|
2021-12-09 13:02:12 +01:00
|
|
|
STDERR.puts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-09 13:17:13 +01:00
|
|
|
cli.sub :tests, "Some tests", noshortaliases: true do |tcli|
|
2021-12-31 13:47:54 +01:00
|
|
|
OptionParser.accept IPAddr do |arg|
|
|
|
|
begin
|
|
|
|
IPAddr.new arg
|
|
|
|
rescue IPAddr::InvalidAddressError
|
|
|
|
raise OptionParser::InvalidArgument, "#{$!.message}: #{arg}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-09 13:02:12 +01:00
|
|
|
tcli.cmd( :'-', "No arguments no options expected", &lambda {|| $capture.args = [] })
|
|
|
|
tcli.cmd( :'arg', "", &lambda {|one| $capture.args = [one] })
|
|
|
|
tcli.cmd( :'oar', "", &lambda {|one=nil| $capture.args = [one] })
|
|
|
|
tcli.cmd( :'arg-arg', "", &lambda {|one, two| $capture.args = [one, two] })
|
|
|
|
tcli.cmd( :'oar-oar', "", &lambda {|one=nil, two=nil| $capture.args = [one, two] })
|
|
|
|
tcli.cmd( :'arg-oar', "expected", &lambda {|one, two=nil| $capture.args = [one, two] })
|
|
|
|
tcli.cmd( :'oar-arg', "expected", &lambda {|one=nil, two| $capture.args = [one, two] })
|
|
|
|
tcli.cmd( :'bool', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '-a', '')
|
|
|
|
tcli.cmd( :'optbool', '', &lambda {|a: nil| $capture.args = [a] }).opt(:a, '-a', '')
|
|
|
|
tcli.cmd( :'defbool', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '-a', '', default: 'default')
|
|
|
|
tcli.cmd( :'str', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '-a=STR', '')
|
|
|
|
tcli.cmd( :'lstr', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '--astr=STR', '')
|
|
|
|
tcli.cmd( :'bstr', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '-a', '--astr=STR', '')
|
2021-12-31 13:47:54 +01:00
|
|
|
tcli.cmd( :'ipaddr', '', &lambda {|a:| $capture&.args = [a] }).opt(:a, '-a', '--addr=ADDR', IPAddr, '')
|
2021-12-09 13:02:12 +01:00
|
|
|
|
|
|
|
tcli.cmd( :run, "Run all tests") do |verbose:|
|
|
|
|
$capture = Capture.new cli, verbose: verbose
|
|
|
|
|
|
|
|
$capture.should_fail DenCli::UnknownCommand, //, 'unknown-command'
|
|
|
|
|
|
|
|
$capture.should_ok [], '-'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, '-', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok %w[first], 'arg', 'first'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'arg'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'arg', 'first', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok %w[first], 'oar', 'first'
|
|
|
|
$capture.should_ok [nil], 'oar'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'oar', 'first', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok %w[first two], 'oar-oar', 'first', 'two'
|
|
|
|
$capture.should_ok ['first', nil], 'oar-oar', 'first'
|
|
|
|
$capture.should_ok [nil,nil], 'oar-oar'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'oar-oar', 'first', 'two', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok %w[first two], 'arg-oar', 'first', 'two'
|
|
|
|
$capture.should_ok ['first', nil], 'arg-oar', 'first'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'arg-oar'
|
|
|
|
|
|
|
|
$capture.should_ok [nil, 'first'], 'oar-arg', 'first'
|
|
|
|
$capture.should_ok ['first', 'second'], 'oar-arg', 'first', 'second'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'oar-arg'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'oar-arg', 'first', 'two', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok [true], 'bool', '-a'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'bool'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'bool', '-a', 'unexpected'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'bool', '-b'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'bool', '--unexpected'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'bool', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok [true], 'optbool', '-a'
|
|
|
|
$capture.should_ok [nil], 'optbool'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'optbool', '-a', 'unexpected'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'optbool', '-b'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'optbool', '--unexpected'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'optbool', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok [true], 'defbool', '-a'
|
|
|
|
$capture.should_ok ['default'], 'defbool'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'defbool', '-a', 'unexpected'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'defbool', '-b'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'defbool', '--unexpected'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'defbool', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok %w[first], 'str', '-a', 'first'
|
|
|
|
$capture.should_ok %w[first], 'str', '-afirst'
|
|
|
|
$capture.should_fail OptionParser::MissingArgument, //, 'str', '-a'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'str'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'str', '-b'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'str', '--unexpected'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'str', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok %w[first], 'lstr', '--astr', 'first'
|
|
|
|
$capture.should_ok %w[first], 'lstr', '--astr=first'
|
|
|
|
$capture.should_fail OptionParser::MissingArgument, //, 'lstr', '--astr'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'lstr'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'lstr', '-b'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'lstr', '--unexpected'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'lstr', 'unexpected'
|
|
|
|
|
|
|
|
$capture.should_ok %w[first], 'bstr', '-a', 'first'
|
|
|
|
$capture.should_ok %w[first], 'bstr', '-afirst'
|
|
|
|
$capture.should_ok %w[first], 'bstr', '--astr', 'first'
|
|
|
|
$capture.should_ok %w[first], 'bstr', '--astr=first'
|
|
|
|
$capture.should_fail OptionParser::MissingArgument, //, 'bstr', '--astr'
|
|
|
|
$capture.should_fail OptionParser::MissingArgument, //, 'bstr', '-a'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'bstr'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'bstr', '-b'
|
|
|
|
$capture.should_fail OptionParser::InvalidOption, //, 'bstr', '--unexpected'
|
|
|
|
$capture.should_fail DenCli::UsageError, //, 'bstr', 'unexpected'
|
2021-12-31 13:47:54 +01:00
|
|
|
|
|
|
|
$capture.should_ok [IPAddr.new('1.2.3.4')], 'ipaddr', '-a', '1.2.3.4'
|
|
|
|
$capture.should_fail OptionParser::InvalidArgument, /invalid address/, 'ipaddr', '-a', '1.2.3.400'
|
2021-12-09 13:02:12 +01:00
|
|
|
end.opt( :verbose, '-v', 'Prints additional information per test', default: false)
|
|
|
|
end
|
|
|
|
|
2021-12-07 23:48:28 +01:00
|
|
|
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
|
|
|
|
2021-12-09 13:02:12 +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-09 13:02:12 +01:00
|
|
|
cli.help_full *commands, output: $stderr
|
2021-11-30 23:09:06 +01:00
|
|
|
else
|
2021-12-09 13:02:12 +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-12-09 13:02:12 +01:00
|
|
|
sub.cmd( :help, "", aliases: [nil, '-h', '--help']) {|*args| $stderr.puts sub.help(*args) }
|
|
|
|
sub.cmd( :example, "Here is an example, too") { $stderr.puts "This is an other example" }
|
|
|
|
sub.cmd( :foo, "BAR") { $stderr.puts "FOO bar"}
|
2020-12-13 17:06:00 +01:00
|
|
|
|
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-09 13:02:12 +01:00
|
|
|
sub2.cmd( :help, "", aliases: [nil, '-h', '--help'], &lambda {|*commands| sub2.help( *commands, 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|
|
2021-12-09 13:02:12 +01:00
|
|
|
sub3.cmd( :help, "") {|*args| $stderr.puts sub3.help( sub3, *args) }
|
|
|
|
sub3.cmd( :hehe, "The real last example", min: 2) { $stderr.puts "Trust me!" }
|
2020-12-13 17:06:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-30 15:23:05 +01:00
|
|
|
begin
|
|
|
|
cli.call *ARGV
|
|
|
|
rescue DenCli::UsageError
|
2021-12-09 13:02:12 +01:00
|
|
|
$stderr.puts $!
|
2021-11-30 15:23:05 +01:00
|
|
|
exit 1
|
|
|
|
end
|