Class: CLASP::FlagSpecification

Inherits:
SpecificationBase
  • Object
show all
Defined in:
lib/clasp/specifications.rb

Overview

A class that represents the specification for a command-line flag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, aliases, help, extras = nil, &blk) ⇒ FlagSpecification

Creates a FlagSpecification instance from the given name, aliases, and help

Signature

  • Parameters

    • name (String) The name, or long-form, of the flag

    • aliases (Array) 0 or more strings specifying short-form or option-value aliases

    • help (String) The help string, which may be nil

    • extras An application-defined additional parameter. If nil, it is assigned an empty Hash

  • Block An optional block that is called when a matching flag argument is found

NOTE: Users should prefer the CLASP::Flag() method



109
110
111
112
113
114
115
116
117
118
# File 'lib/clasp/specifications.rb', line 109

def initialize(name, aliases, help, extras = nil, &blk)

    check_arity_(blk, 0..3, "flag")

    @name       =   name
    @aliases    =   (aliases || []).select { |a| a and not a.empty? }
    @help       =   help
    @extras     =   extras || {}
    @action     =   blk
end

Instance Attribute Details

#actionObject

(Proc) The procedure



130
131
132
# File 'lib/clasp/specifications.rb', line 130

def action
  @action
end

#aliasesObject (readonly)

The flag’s aliases array



123
124
125
# File 'lib/clasp/specifications.rb', line 123

def aliases
  @aliases
end

#extrasObject (readonly)

The flag’s extras



127
128
129
# File 'lib/clasp/specifications.rb', line 127

def extras
  @extras
end

#helpObject (readonly)

The flag’s help string



125
126
127
# File 'lib/clasp/specifications.rb', line 125

def help
  @help
end

#nameObject (readonly)

The flag’s name string



121
122
123
# File 'lib/clasp/specifications.rb', line 121

def name
  @name
end

Class Method Details

.Help(extras = nil, &blk) ⇒ Object

An instance of FlagSpecification that provides default ‘–help’ information

If you wish to specify extras or attach a block, you may do so



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/clasp/specifications.rb', line 189

def self.Help(extras = nil, &blk)

    h = @@Help_

    if extras || blk

        return self.new(h.name, h.aliases, h.help, extras, &blk)
    end

    h
end

.Version(extras = nil, &blk) ⇒ Object

An instance of FlagSpecification that provides default ‘–version’ information

If you wish to specify extras or attach a block, you may do so



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/clasp/specifications.rb', line 204

def self.Version(extras = nil, &blk)

    h = @@Version_

    if extras || blk

        return self.new(h.name, h.aliases, h.help, extras, &blk)
    end

    h
end

Instance Method Details

#==(rhs) ⇒ Object

Compares instance against another FlagSpecification or against a name (String)



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/clasp/specifications.rb', line 167

def == rhs

    case rhs
    when self.class

        return self.eql? rhs
    when String

        return name == rhs
    else

        false
    end
end

#to_sObject

String form of the flag



141
142
143
144
# File 'lib/clasp/specifications.rb', line 141

def to_s

    "{#{name}; aliases=#{aliases.join(', ')}; help='#{help}'; extras=#{extras}}"
end