Sha256: 5fd4fcb0895bc0908aff8307c3e5c637650ff9d55ab876644a78d5de898b0028

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

module Eco
  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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-0.8.3 lib/eco/scripting/args_helpers.rb
eco-helpers-0.8.2 lib/eco/scripting/args_helpers.rb
eco-helpers-0.8.1 lib/eco/scripting/args_helpers.rb