Sha256: 4083a1438d86851b1bfec32e109354053149fc65a1a670af2f4022bd58d4edb5

Contents?: true

Size: 1.97 KB

Versions: 29

Compression:

Stored size: 1.97 KB

Contents

module Eco
  class CLI
    class Scripting
      module ArgsHelpers

        def argv
          @argv || ARGV
        end

        def is_modifier?(value)
          Argument.is_modifier?(value)
        end

        def arguments
          @arguments ||= Arguments.new(argv)
        end

        def stop_on_unknown!(exclude: [], only_options: false)
          # validate only those that are options
          unknown = arguments.unknown(exclude: exclude)
          if only_options
            unknown = unknown..select {|arg| is_modifier?(arg)}
          end

          unless unknown.empty?
            raise "There are unknown options in your command line arguments: #{unknown}"
          end
        end

        def get_arg_index(key)
          return nil if !argv.include?(key)
          argv.index(key)
        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 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
        end

        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 '#{key} file'"
            exit(1)
          elsif !file_exists?(filename) && should_exist && required
            puts "This file doesn't exist '#{filename}'"
            exit(1)
          end

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

        private

        def file_exists?(filename)
          File.exists?(filename) || File.exists?(File.expand_path(filename))
        end

      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
eco-helpers-2.0.16 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.15 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.14 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.13 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.12 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.11 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.10 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.9 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.8 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.7 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.6 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.5 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.4 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.3 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-2.0.2 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.5.15 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.5.14 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.5.13 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.5.12 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.5.11 lib/eco/cli/scripting/args_helpers.rb