Sha256: dc9e62f33ab16fdd5ca11ec995a48690c0871243f918662f266753a127832c1c

Contents?: true

Size: 1.78 KB

Versions: 35

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(1)
              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

35 entries across 35 versions & 1 rubygems

Version Path
eco-helpers-1.5.1 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.5.0 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.4.2 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.19 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.4.1 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.4.0 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.18 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.17 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.16 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.15 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.14 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.13 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.12 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.11 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.10 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.9 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.8 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.7 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.6 lib/eco/cli/scripting/args_helpers.rb
eco-helpers-1.3.5 lib/eco/cli/scripting/args_helpers.rb