ruby2.5: @os removed - OptionParser does not expect any **options, so we need not it.
This commit is contained in:
parent
7078305256
commit
a00149ce6a
|
@ -1,10 +1,9 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
require 'pathname'
|
require 'pathname'
|
||||||
$:.unshift Pathname.new(__FILE__).dirname.dirname.join('lib').to_s
|
|
||||||
require 'dencli'
|
|
||||||
require 'shellwords'
|
require 'shellwords'
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
|
require_relative '../lib/dencli'
|
||||||
|
|
||||||
cli = DenCli.new :example, "This is an example for generate a DenCli-API"
|
cli = DenCli.new :example, "This is an example for generate a DenCli-API"
|
||||||
|
|
||||||
|
@ -33,8 +32,8 @@ class Capture
|
||||||
STDERR.printf "[% 4d] \e[1;35m? \e[0m %s tests %s\r", @counter, $0.shellescape, command.shelljoin
|
STDERR.printf "[% 4d] \e[1;35m? \e[0m %s tests %s\r", @counter, $0.shellescape, command.shelljoin
|
||||||
end
|
end
|
||||||
|
|
||||||
def logok command
|
def logok info, command
|
||||||
STDERR.printf "[% 4d] \e[1;32mok\e[0m %s tests %s\e[J\n", @counter, $0.shellescape, command.shelljoin
|
STDERR.printf "[% 4d] \e[1;32mok\e[0m %s | %s tests %s\e[J\n", @counter, info, $0.shellescape, command.shelljoin
|
||||||
end
|
end
|
||||||
|
|
||||||
def logfail command
|
def logfail command
|
||||||
|
@ -54,7 +53,7 @@ class Capture
|
||||||
logstart command
|
logstart command
|
||||||
$capture.capture { @cli.call 'tests', *command }
|
$capture.capture { @cli.call 'tests', *command }
|
||||||
if expect === @args
|
if expect === @args
|
||||||
logok command
|
logok @args, command
|
||||||
else
|
else
|
||||||
logfail command
|
logfail command
|
||||||
loginfo "expected args: #{expect.inspect}"
|
loginfo "expected args: #{expect.inspect}"
|
||||||
|
@ -63,7 +62,7 @@ class Capture
|
||||||
end
|
end
|
||||||
rescue SystemExit
|
rescue SystemExit
|
||||||
if 0 == $!.status
|
if 0 == $!.status
|
||||||
logok command
|
logok @args, command
|
||||||
else
|
else
|
||||||
logfail command
|
logfail command
|
||||||
end
|
end
|
||||||
|
@ -79,7 +78,7 @@ class Capture
|
||||||
logfail command
|
logfail command
|
||||||
rescue exception
|
rescue exception
|
||||||
if message === $!.message
|
if message === $!.message
|
||||||
logok command
|
logok exception, command
|
||||||
if @verbose
|
if @verbose
|
||||||
logexception "raised:", $!
|
logexception "raised:", $!
|
||||||
STDERR.puts
|
STDERR.puts
|
||||||
|
@ -96,6 +95,14 @@ class Capture
|
||||||
end
|
end
|
||||||
|
|
||||||
cli.sub :tests, "Some tests", noshortaliases: true do |tcli|
|
cli.sub :tests, "Some tests", noshortaliases: true do |tcli|
|
||||||
|
OptionParser.accept IPAddr do |arg|
|
||||||
|
begin
|
||||||
|
IPAddr.new arg
|
||||||
|
rescue IPAddr::InvalidAddressError
|
||||||
|
raise OptionParser::InvalidArgument, "#{$!.message}: #{arg}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
tcli.cmd( :'-', "No arguments no options expected", &lambda {|| $capture.args = [] })
|
tcli.cmd( :'-', "No arguments no options expected", &lambda {|| $capture.args = [] })
|
||||||
tcli.cmd( :'arg', "", &lambda {|one| $capture.args = [one] })
|
tcli.cmd( :'arg', "", &lambda {|one| $capture.args = [one] })
|
||||||
tcli.cmd( :'oar', "", &lambda {|one=nil| $capture.args = [one] })
|
tcli.cmd( :'oar', "", &lambda {|one=nil| $capture.args = [one] })
|
||||||
|
@ -109,6 +116,7 @@ cli.sub :tests, "Some tests", noshortaliases: true do |tcli|
|
||||||
tcli.cmd( :'str', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '-a=STR', '')
|
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( :'lstr', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '--astr=STR', '')
|
||||||
tcli.cmd( :'bstr', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '-a', '--astr=STR', '')
|
tcli.cmd( :'bstr', '', &lambda {|a:| $capture.args = [a] }).opt(:a, '-a', '--astr=STR', '')
|
||||||
|
tcli.cmd( :'ipaddr', '', &lambda {|a:| $capture&.args = [a] }).opt(:a, '-a', '--addr=ADDR', IPAddr, '')
|
||||||
|
|
||||||
tcli.cmd( :run, "Run all tests") do |verbose:|
|
tcli.cmd( :run, "Run all tests") do |verbose:|
|
||||||
$capture = Capture.new cli, verbose: verbose
|
$capture = Capture.new cli, verbose: verbose
|
||||||
|
@ -187,6 +195,9 @@ cli.sub :tests, "Some tests", noshortaliases: true do |tcli|
|
||||||
$capture.should_fail OptionParser::InvalidOption, //, 'bstr', '-b'
|
$capture.should_fail OptionParser::InvalidOption, //, 'bstr', '-b'
|
||||||
$capture.should_fail OptionParser::InvalidOption, //, 'bstr', '--unexpected'
|
$capture.should_fail OptionParser::InvalidOption, //, 'bstr', '--unexpected'
|
||||||
$capture.should_fail DenCli::UsageError, //, 'bstr', 'unexpected'
|
$capture.should_fail DenCli::UsageError, //, 'bstr', 'unexpected'
|
||||||
|
|
||||||
|
$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'
|
||||||
end.opt( :verbose, '-v', 'Prints additional information per test', default: false)
|
end.opt( :verbose, '-v', 'Prints additional information per test', default: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -160,7 +160,7 @@ class DenCli::CMD
|
||||||
end
|
end
|
||||||
|
|
||||||
class Opt
|
class Opt
|
||||||
attr_reader :name, :long, :short, :val, :desc, :os, :conv, :req
|
attr_reader :name, :long, :short, :type, :val, :desc, :conv, :req
|
||||||
def required?() @req end
|
def required?() @req end
|
||||||
def default?() NilClass != @default end
|
def default?() NilClass != @default end
|
||||||
def default() NilClass == @default ? nil : @default end
|
def default() NilClass == @default ? nil : @default end
|
||||||
|
@ -183,11 +183,12 @@ class DenCli::CMD
|
||||||
end
|
end
|
||||||
private :parse_opt_string
|
private :parse_opt_string
|
||||||
|
|
||||||
def initialize cmd, name, opt, *alts, desc, default: NilClass, **os, &conv
|
def initialize cmd, name, opt, *args, desc, default: NilClass, &conv
|
||||||
@name, @desc, @default, @os, @conv, @val =
|
@name, @desc, @default, @conv, @val, @type =
|
||||||
name.to_s.to_sym, desc, default, os, conv || lambda{|v|v}, nil
|
name.to_s.to_sym, desc, default, conv || lambda{|v|v}, nil, nil
|
||||||
parse_opt_string opt
|
parse_opt_string opt
|
||||||
alts.each &method( :parse_opt_string)
|
@type = args.pop if OptionParser.top.atype.has_key? args.last
|
||||||
|
args.each &method( :parse_opt_string)
|
||||||
@req =
|
@req =
|
||||||
if NilClass != default
|
if NilClass != default
|
||||||
false
|
false
|
||||||
|
@ -200,30 +201,32 @@ class DenCli::CMD
|
||||||
|
|
||||||
def on parser, store
|
def on parser, store
|
||||||
store[@name] = @default if default?
|
store[@name] = @default if default?
|
||||||
parser.on "#{@short}#{@val ? ?= : ''}#{@val}", "#{@long}#{@val ? ?= : ''}#{@val}", **@os do |val|
|
short = "#{@short}#{@val ? ?= : ''}#{@val}"
|
||||||
|
long = "#{@long}#{@val ? ?= : ''}#{@val}"
|
||||||
|
parser.on short, long, *[@type].compact do |val|
|
||||||
store[@name] = @conv[val]
|
store[@name] = @conv[val]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<%s:0x%016x %s %s %s %s (%p) %p os=%p conv=%s>" % [
|
"#<%s:0x%x %s %s %s %s (%p) %p conv=%s>" % [
|
||||||
self.class.name, object_id, @req ? "<#{@name}>" : "[#{@name}]",
|
self.class.name, object_id, @req ? "<#{@name}>" : "[#{@name}]",
|
||||||
@short, @long, @val, @default, @desc, @os,
|
@short, @long, @val, @default, @desc, @type ? " type=#{type}" : '',
|
||||||
@exe ? "<#{@exe.lambda? ? :lambda: :proc} ##{@exe.arity}>" : "nil"
|
@exe ? "<#{@exe.lambda? ? :lambda: :proc} ##{@exe.arity}>" : "nil"
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def opt name, opt, *alts, desc, **os, &conv
|
def opt name, opt, *args, desc, &conv
|
||||||
r = Opt.new( self, name, opt, *alts, desc, **os, &conv)
|
r = Opt.new( self, name, opt, *args, desc, &conv)
|
||||||
@options[r.name] = r
|
@options[r.name] = r
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<%s:0x%x %s @name=%p @description=%p @parent=<%s:0x%x %s> @exe=<arity=%d>>" % [
|
"#<%s:0x%x %s @name=%p @description=%p @options=%p @parent=<%s:0x%x %s> @exe=<arity=%d>>" % [
|
||||||
self.class.name, self.object_id, self.full_cmd,
|
self.class.name, self.object_id, self.full_cmd,
|
||||||
@name, @description, @parent.class.name, @parent.class.object_id, @parent.full_cmd,
|
@name, @description, @options.values, @parent.class.name, @parent.class.object_id, @parent.full_cmd,
|
||||||
@exe.arity
|
@exe.arity
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue