@@ -124,11 +124,13 @@ def initialize(symbol: nil, on: nil, datatype: nil, control: nil,
124124 @symbol , @on , @datatype , @control , @description , @use , @callback = symbol . to_sym , Array ( on ) , datatype , control , description , use , block
125125 end
126126
127- def call ( arg , options )
127+ def call ( arg , options = { } )
128128 if @callback
129129 case @callback . arity
130+ when 0 then @callback . call
130131 when 1 then @callback . call ( arg )
131132 when 2 then @callback . call ( arg , options )
133+ else arg
132134 end
133135 else
134136 arg
@@ -305,7 +307,7 @@ def to_hash
305307 on_args = cli_opt . on || [ ]
306308 on_args << cli_opt . description if cli_opt . description
307309 options . on ( *on_args ) do |opt_arg |
308- options . options [ cli_opt . symbol ] = cli_opt . call ( opt_arg )
310+ options . options [ cli_opt . symbol ] = cli_opt . call ( opt_arg , options )
309311 end
310312 end if reader
311313 arg . downcase . to_sym
@@ -327,7 +329,7 @@ def to_hash
327329 on_args = cli_opt . on || [ ]
328330 on_args << cli_opt . description if cli_opt . description
329331 options . on ( *on_args ) do |opt_arg |
330- options . options [ cli_opt . symbol ] = cli_opt . call ( opt_arg )
332+ options . options [ cli_opt . symbol ] = cli_opt . call ( opt_arg , options )
331333 end
332334 end if writer
333335 arg . downcase . to_sym
@@ -358,7 +360,7 @@ def self.basename() File.basename($0) end
358360 # Returns discovered options
359361 def self . options ( argv , format : nil )
360362 options = OptionParser . new
361- cli_opts = OPTIONS . dup
363+ cli_opts = OPTIONS . map ( & : dup)
362364 logger = Logger . new ( $stderr)
363365 logger . level = Logger ::WARN
364366 logger . formatter = lambda { |severity , datetime , progname , msg | "#{ severity } #{ msg } \n " }
@@ -428,7 +430,7 @@ def self.options(argv, format: nil)
428430
429431 ##
430432 # Output usage message
431- def self . usage ( options , cmd_opts = { } , banner : nil )
433+ def self . usage ( options , cmd_opts : { } , banner : nil )
432434 options . banner = banner if banner
433435 $stdout. puts options
434436 $stdout. puts "Note: available commands and options may be different depending on selected --input-format and/or --output-format."
@@ -505,6 +507,17 @@ def self.exec(args, output: $stdout, option_parser: nil, messages: {}, **options
505507 COMMANDS [ command . to_sym ] [ :lambda ] . call ( args , output : output , **options . merge ( messages : messages ) )
506508 end
507509
510+ # Normalize messages
511+ messages . each do |kind , term_messages |
512+ case term_messages
513+ when Hash
514+ when Array
515+ messages [ kind ] = { result : term_messages }
516+ else
517+ messages [ kind ] = { result : [ term_messages ] }
518+ end
519+ end
520+
508521 if options [ :statistics ]
509522 options [ :statistics ] [ :reader ] = @readers . first unless ( @readers || [ ] ) . empty?
510523 options [ :statistics ] [ :count ] = @repository . count
0 commit comments