Sha256: e33f8b1a3a02ff4a8051ea2c6761385b964a7f5477a37aca06d0f18eb897b342

Contents?: true

Size: 1.78 KB

Versions: 15

Compression:

Stored size: 1.78 KB

Contents

module Eco
  class CLI
    class Scripting
      module ArgsHelpers

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

        def arguments
          @arguments ||= Arguments.new
        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(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

15 entries across 15 versions & 1 rubygems

Version Path
eco-helpers-1.0.13 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.12 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.11 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.10 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.9 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.8 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.7 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.6 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.5 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.4 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.3 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-0.9.5 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.0.2 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-0.9.4 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-0.9.3 lib/eco/cli/scripting/args_helpers.rb