lib/eco/cli/scripting/args_helpers.rb in eco-helpers-2.7.16 vs lib/eco/cli/scripting/args_helpers.rb in eco-helpers-2.7.17

- old
+ new

@@ -5,11 +5,11 @@ # @return [Array<String] the command line arguments. def argv @argv || ARGV end - def is_modifier?(value) + def is_modifier?(value) # rubocop:disable Naming/PredicateName Argument.is_modifier?(value) end # @return [Arguments] supported known arguments. def arguments @@ -19,56 +19,71 @@ # Registers an argument as a known one. def known_argument(key, with_param: false) arguments.add(key, with_param: with_param) end - # Validation to stop the `script` if among `argv` there's any **unknown** argument. - def stop_on_unknown!(exclude: [], only_options: false) + def stop_on_unknown!(exclude: [], only_options: false, all_available: nil) # validate only those that are options - unknown = arguments.unknown(exclude: exclude) - if only_options - unknown = unknown..select {|arg| is_modifier?(arg)} + suggestions = {} + args = { + exclude: exclude, + all_available: all_available + } + unknown = arguments.unknown(**args) do |key, correct| + suggestions[key] = correct unless correct.empty? end + unknown = unknown.select {|arg| is_modifier?(arg)} if only_options - unless unknown.empty? - msg = "There are unknown options in your command line arguments:\n" - msg += "#{unknown}\n" - msg += "Please, remember that use case specific options should come after the use case in the command line.\n" - msg += "Use 'ruby main.rb -org [-usecase] --help -options' for more information" - raise msg - end + return if unknown.empty? + + suggestions_str = suggestions.slice(*unknown).map do |key, correct| + str = "Unknown option '#{key}'." + str_corr = correct.map {|key| "'#{key}'"} + str << " Did you mean: #{str_corr.join(', ')}" unless correct.empty? + str + end.join("\n * ") + + msg = "\nThere are UNKNOWN OPTIONS in your command line arguments !!" + msg << "\n * #{suggestions_str}\n" + msg << "\nUse 'ruby main.rb -org [-usecase] --help -options' for more information" + msg << "\n - Please, remember that use case specific options " + msg << "should come after the use case in the command line.\n" + raise msg end # @return [Boolean] if `key` is in the command line. def arg?(key) argv.include?(key) end # @return [Integer, nil] the position of `key` in the command line. def get_arg_index(key) - return nil if !arg?(key) + return unless arg?(key) + argv.index(key) end # @return [Boolean] if `key1` precedes `key2` in the command line. - def arg_order?(key1, key2) - return false unless (k1 = get_arg_index(key1)) && k2 = get_arg_index(key2) - k1 < k2 + def arg_order?(key_1, key_2) + k_1 = get_arg_index(key_1) + k_2 = get_arg_index(key_2) + return false unless k_1 && k_2 + + k_1 < k_2 end # @return [String, Boolean] the argument value if `with_param` or a `Boolean` if not. def get_arg(key, with_param: false, valid: true) # track what a known option looks like known_argument(key, with_param: with_param) - return nil unless index = get_arg_index(key) - value = true - if with_param - value = argv[index + 1] - #puts "modifier argument: #{value}" - value = nil if valid && is_modifier?(value) - end - return value + return nil unless (index = get_arg_index(key)) + return true unless with_param + + value = argv[index + 1] + #puts "modifier argument: #{value}" + value = nil if valid && is_modifier?(value) + value end # @return [String] the filename. def get_file(key, required: false, should_exist: true) filename = get_arg(key, with_param: true)