lib/bblib/cli/option.rb in bblib-2.0.3 vs lib/bblib/cli/option.rb in bblib-2.0.4

- old
+ new

@@ -17,48 +17,48 @@ attr_ary_of Proc, :validators attr_bool :singular, default: true attr_of [Integer, Range], :position, default: nil, allow_nil: true attr_hash :sub_commands, keys: String, values: OptsParser, aliases: [:sub_cmds, :subcommands], default: nil, allow_nil: true, pre_proc: proc { |hash| hash.is_a?(Hash) ? hash.keys_to_s : hash } - def to_s (flags.sort_by(&:size).join(', ') + " #{placeholder}").strip.ljust(40, ' ') + "\t#{description}" end def self.types descendants.flat_map(&:type) end def retrieve(args, parsed) - result = singular? ? nil : [] + result = multi_value? ? [] : nil index = 0 until index >= args.size begin - unless flag_match?(args[index].to_s, index) + if args[index].nil? || !flag_match?(args[index].to_s, index) index += 1 next end values = split(extract(index, args)) values.each do |value| valid!(value) - if singular? - result = value - index = args.size - else + if multi_value? result << value + else + result = value end + index = args.size if singular? end rescue OptsParserException => e raise e if raise_errors? end + index += 1 end raise MissingArgumentException, "A required argument is missing: #{name}" if required? && result.nil? result = processor.call(result) if !result.nil? && processor process_result(result.nil? ? default : result, args, parsed) end - def singular? - singular && !delimiter + def multi_value? + !singular || delimiter end def flag_match?(str, index = 0) text_match = if flags.empty? && position true