Module: Eco::CLI::Scripting::ArgsHelpers
- Included in:
- Eco::CLI::Scripting
- Defined in:
- lib/eco/cli/scripting/args_helpers.rb
Defined Under Namespace
Classes: UnknownArgument
Instance Method Summary collapse
-
#arg?(key) ⇒ Boolean
If
key
is in the command line. -
#arg_order?(key_1, key_2) ⇒ Boolean
If
key1
precedeskey2
in the command line. -
#arguments ⇒ Arguments
Supported known arguments.
-
#argv ⇒ Array<String] the command line arguments.
Array<String] the command line arguments.
-
#get_arg(key, with_param: false, valid: true) ⇒ String, Boolean
The argument value if
with_param
or aBoolean
if not. -
#get_arg_index(key) ⇒ Integer?
The position of
key
in the command line. -
#get_file(key, required: false, should_exist: true) ⇒ String
The filename.
-
#is_modifier?(value) ⇒ Boolean
rubocop:disable Naming/PredicateName.
-
#known_argument(key, with_param: false) ⇒ Object
Registers an argument as a known one.
-
#stop_on_unknown!(exclude: [], only_options: false, all_available: nil) ⇒ Object
Validation to stop the
script
if amongargv
there's any unknown argument.
Instance Method Details
#arg?(key) ⇒ Boolean
Returns if key
is in the command line.
59 60 61 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 59 def arg?(key) argv.include?(key) end |
#arg_order?(key_1, key_2) ⇒ Boolean
Returns if key1
precedes key2
in the command line.
71 72 73 74 75 76 77 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 71 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 |
#arguments ⇒ Arguments
Returns supported known arguments.
17 18 19 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 17 def arguments @arguments ||= Arguments.new(argv) end |
#argv ⇒ Array<String] the command line arguments.
Returns Array<String] the command line arguments.
8 9 10 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 8 def argv @argv || ARGV end |
#get_arg(key, with_param: false, valid: true) ⇒ String, Boolean
Returns the argument value if with_param
or a Boolean
if not.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 80 def get_arg(key, with_param: false, valid: true) case key when Array key.reduce(nil) do |value, k| next value unless value.nil? get_arg(k, with_param: with_param, valid: valid) end else # track what a known option looks like known_argument(key, with_param: with_param) 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 end |
#get_arg_index(key) ⇒ Integer?
Returns the position of key
in the command line.
64 65 66 67 68 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 64 def get_arg_index(key) return unless arg?(key) argv.index(key) end |
#get_file(key, required: false, should_exist: true) ⇒ String
Returns the filename.
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 102 def get_file(key, required: false, should_exist: true) filename = get_arg(key, with_param: true) if !filename && required puts "You need to specify a file or folder '#{key} file_or_folder'" exit(1) elsif !file_exists?(filename) && should_exist && required puts "This file/folder doesn't exist '#{filename}'" exit(1) end filename = File.(filename) if filename && should_exist filename end |
#is_modifier?(value) ⇒ Boolean
rubocop:disable Naming/PredicateName
12 13 14 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 12 def is_modifier?(value) # rubocop:disable Naming/PredicateName Argument.is_modifier?(value) end |
#known_argument(key, with_param: false) ⇒ Object
Registers an argument as a known one.
22 23 24 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 22 def known_argument(key, with_param: false) arguments.add(key, with_param: with_param) end |
#stop_on_unknown!(exclude: [], only_options: false, all_available: nil) ⇒ Object
Validation to stop the script
if among argv
there's any unknown argument.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/eco/cli/scripting/args_helpers.rb', line 27 def stop_on_unknown!(exclude: [], only_options: false, all_available: nil) # validate only those that are options suggestions = {} args = { exclude: exclude, all_available: all_available } unknown = arguments.unknown(**args) do |key, correct| next suggestions[key] = correct unless correct.empty? suggestions[key] = ['-- not known similar options! --'] end unknown = unknown.select {|arg| is_modifier?(arg)} if return if unknown.empty? suggestions_str = suggestions.slice(*unknown).map do |key, correct| str = "Unknown option '#{key}'." str_corr = correct.map {|ky| "'#{ky}'"} 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 UnknownArgument, msg end |