Sha256: 92384745f24af5a3feed7dc337a18e9e4ff56b9856d6e1ebe59292cd3c70f738

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

module Eco
  class CLI
    class Scripting
      module ArgsHelpers

        def is_modifier?(value)
         value&.start_with?("-")
        end

        def arguments
          @arguments ||= Arguments.new
        end

        def stop_on_unknown!(exclude: [])
          if arguments.any_unkown?(exclude: exclude)
            raise "There are unknown options in your command line arguments: #{arguments.unknown(exclude: exclude)}"
          end
        end

        def get_arg(key, with_param: false, valid: true)
          # track what a known option looks like
          arguments.add(key, with_param: with_param)
          return nil if !ARGV.include?(key)
          value = true
          if with_param
            next_i = ARGV.index(key) + 1
            value = ARGV[next_i]
            #puts "modifier argument: #{value}"
            value = nil if valid && is_modifier?(value)
          end
          return value
        end

        def get_file(key, required: false, should_exist: true)
          filename = get_arg(key, with_param: true)
          if !filename
              if required
                puts "you need to specify a file '#{key} file'"
                exit
              end
          elsif  !(File.exists?(filename) || File.exists?(File.expand_path(filename)))
            if should_exist && required
              puts "file doesn't exist #{filename}"
              exit
            end
          end

          filename = File.expand_path(filename) if filename && should_exist
          filename
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-0.9.2 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-0.9.1 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-0.8.4 lib/eco/cli/scripting/args_helpers.rb