Class: YAOP

Inherits:
Object
  • Object
show all
Defined in:
lib/yaop.rb

Overview

Yet anothet options parser. Very rubyfied declarative definitions support.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeYAOP

Constructor.



50
51
52
53
# File 'lib/yaop.rb', line 50

def initialize
    @options = [ ]
    @stack = nil
end

Class Method Details

.get(&block) ⇒ OptionsHash

Returns command line options.

Parameters:

  • block (Proc)

    arguments declaration

Returns:

  • (OptionsHash)

    hash with options



40
41
42
43
44
# File 'lib/yaop.rb', line 40

def self.get(&block)
    parser = YAOP::new
    parser.instance_eval(&block)
    return parser.result
end

Instance Method Details

#option(*args) ⇒ Object Also known as: options

Sets option names. Assigning of the new option names will cause setting the current one to assigned options.

Parameters:

  • args (*String)

    option names including leading character



62
63
64
65
66
67
68
69
# File 'lib/yaop.rb', line 62

def option(*args)
    if not @stack.nil?
        @options << @stack
    end
    
    __reset_stack
    @stack.options = args.map { |i| i.to_s.to_sym }
end

#resultYAOP::Result

Returns list of arguments and parameters according to definition.

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/yaop.rb', line 92

def result

    # Commits last stack
    if not @stack.nil?
        @options << @stack
        @stack = nil
    end
    
    # Creates options index
    index = { }
    @options.each do |i|
        i.options.each do |id|
            index[id] = i
        end
    end

    # Parses
    last = nil    # note: it's used below
    params = [ ]
    
    ARGV.each do |arg|
        if index.has_key? arg.to_sym
            last = arg.to_sym
            index[last].present = true
        elsif not last.nil?
            index[last].values << arg
        else
            params << arg
        end
    end
    
    # Converts datatypes
    result = Hash::new { |dict, key| dict[key] = [ ] }

    index.each_pair do |opt, spec|
    
        # If no types defines, sets true as symbol of presency
        if spec.types.empty?
            result[opt] = spec.present
            next
        end
    
        # If types defined, takes them
        spec.types.each_index do |i|
            type, default = spec.types[i]
            value = nil

            case type.hash
                when Integer.hash
                    value = i < spec.values.length ? spec.values[i].to_i : default
                when String.hash
                    value = i < spec.values.length ? spec.values[i].to_s : default
                when Boolean.hash
                    value = spec.present ? true : default
            end
            
            if not value.nil?
                result[opt] << value
            end
        end
        
    end
    
    # Converts single value arguments to single-ones
    result.each_pair do |opt, values|
        if index[opt].types.length == 1
            result[opt] = values.first
        end
    end

    # Takes parameters from last argument
    if not last.nil?
        parameters = index[last].values[index[last].types.length..-1]
    else
        parameters = params
    end
       
    parameters = [ ] if parameters.nil?
    
    
    return self.class::Result::new(
        result.map_keys! { |k| k.to_s },              # converts keys from symbols back to string
        parameters
    )
    
end

#type(type, default = nil) ⇒ Object

Defines type and default value specification for current option.

Can be used multiple times as indication, argument receives more values treated as separated Ruby arguments.

Parameters:

  • type (Class)

    type which can be Boolean, String or Integer



83
84
85
# File 'lib/yaop.rb', line 83

def type(type, default = nil)
    @stack.types << [type, default]
end