class KXI::CLI::ArgumentValues

Manages values of arguments

Public Class Methods

new(args) click to toggle source

Instantiates the {KXI::CLI::ArgumentValues} class @param args [Array<KXI::CLI::Argument>] Expected arguments

# File lib/kxi/cli/argument_values.rb, line 9
def initialize(args)
        @args = args
        @vals = {}
        args.each do |a|
                if a.is_a?(KXI::CLI::FlagArgument)
                        @vals[a.name] = { :argument => a, :value => false }
                end
        end
end

Public Instance Methods

[](index) click to toggle source

Gets value of argument @param index [String, Symbol, KXI::CLI::Argument] Name of argument @return [String] Value of argument

# File lib/kxi/cli/argument_values.rb, line 45
def [](index)
        if index.is_a?(Symbol)
                index = index.to_s
        elsif index.is_a?(KXI::CLI::Argument)
                index = index.name
        end
        raise(Exception.new("Undefined argument '#{index}'!")) unless @vals.include?(index)
        return @vals[index][:value]
end
finish() click to toggle source

Validates variadic arguments and checks for minimal argument requirements

# File lib/kxi/cli/argument_values.rb, line 66
def finish
        @args.each do |arg|
                if @vals.include?(arg.name)
                        unless arg.is_a?(KXI::CLI::FlagArgument)
                                begin
                                        arg.validate(@vals[arg.name][:value])
                                rescue Exception => ex
                                        raise(KXI::Exceptions::ArgumentException.new(arg.name, ex.message))
                                end
                        end
                else
                        raise(KXI::Exceptions::ArgumentException.new(arg.name, 'Argument is mandatory!')) if arg.required?
                        @vals[arg.name] = { :argument => arg, :value => arg.default }
                end
        end
end
set(arg, val) click to toggle source

Assigns (or adds) a value to argument @param arg [KXI::CLI::Argument] Argument to set @param val [String] Value to assign

# File lib/kxi/cli/argument_values.rb, line 22
def set(arg, val)
        if arg.is_a?(KXI::CLI::FlagArgument)
                raise(KXI::Exceptions::ArgumentException.new(arg.name, 'Flag set multiple times!')) if @vals[arg.name][:value]
                @vals[arg.name][:value] = val
        else
                if arg.variadic?
                        @vals[arg.name] = { :argument => arg, :value => [] } if @vals[arg.name] == nil
                        @vals[arg.name][:value].push(val)
                else
                        raise(KXI::Exceptions::ArgumentException.new(arg.name, 'Argument set multiple times!')) if @vals[arg.name] != nil
                        begin
                                arg.validate(val)
                        rescue Exception => ex
                                raise(KXI::Exceptions::ArgumentException.new(arg.name, ex.message))
                        end
                        @vals[arg.name] = { :argument => arg, :value => val }
                end
        end
end
to_h() click to toggle source

Converts class to hash @return [Hash] Equivalent hash

# File lib/kxi/cli/argument_values.rb, line 57
def to_h
        ret = {}
        @vals.each_pair do |k, v|
                ret[k] = v[:value]
        end
        return ret
end