Module: CLASP

Defined in:
lib/clasp/doc_.rb,
lib/clasp/cli.rb,
lib/clasp/clasp.rb,
lib/clasp/version.rb,
lib/clasp/arguments.rb,
lib/clasp/specifications.rb,
lib/clasp/util/exceptions.rb,
lib/clasp/util/value_parser.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Exceptions, Util Classes: AliasSpecification, Arguments, FlagSpecification, OptionSpecification

Constant Summary collapse

VERSION =

Current version of the CLASP.Ruby library

'0.23.3'
VERSION_MAJOR =

Major version of the CLASP.Ruby library

VERSION_PARTS_[0]
VERSION_MINOR =

Minor version of the CLASP.Ruby library

VERSION_PARTS_[1]
VERSION_REVISION =

Revision version of the CLASP.Ruby library

VERSION_PARTS_[2]
FlagAlias =
FlagSpecification
OptionAlias =
OptionSpecification

Class Method Summary collapse

Class Method Details

.parse(argv = ARGV, specifications = nil, options = {}) ⇒ Object

TBC (but is a shorthand for calling Arguments.new()



60
61
62
63
# File 'lib/clasp/clasp.rb', line 60

def self.parse(argv = ARGV, specifications = nil, options = {})

  return Arguments.new(argv, specifications, options)
end

.show_usage(specifications, options = {}) ⇒ Object

Displays usage for the program according to the given specifications and options

Signature

  • Parameters:

    • specifications (+Array+) The arguments array. May not be nil. Defaults to ARGV.
    • options An options hash, containing any of the following options.
  • Options:

    • :exit a program exit code; exit() not called if not specified (or nil).
    • :program_name program name; inferred from $0 if not specified.
    • :stream output stream; $stdout if not specified.
    • :suppress_blank_lines_between_options does exactly what it says on the tin.
    • :values appends this string to USAGE line if specified.
    • :flags_and_options inserts a custom string instead of the default string '[ ... flags and options ... ]'.
    • :info_lines inserts 0+ information lines prior to the usage.
    • :default_indicator (String) a string placed after the matching value in the listing of an option's range of values. Defaults to "(default)". If nil default is used. If empty string no indication given

Raises:

  • (ArgumentError)


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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/clasp/cli.rb', line 136

def self.show_usage specifications, options={}

  options ||= {}

  raise ArgumentError, "specifications may not be nil" if specifications.nil?
  raise TypeError, "specifications must be an array or must respond to each, reject and select" unless ::Array === specifications || (specifications.respond_to?(:each) && specifications.respond_to?(:reject) && specifications.respond_to?(:select))

  constants = CLI_helpers_::Constants

  specifications.each { |s| raise ::TypeError, "each element in specifications array must be one of the types #{constants::VALID_ALIAS_TYPES_STRING}" unless constants::VALID_ALIAS_TYPES.any? { |c| c === s } }

  alias_dups = {}
  specifications.each { |s| s.aliases.each { |aa| warn "WARNING: alias '#{aa}' is already used for specification '#{s}'" if alias_dups.has_key? aa; alias_dups[aa] = s; } }

  suppress_blanks = options[:suppress_blank_lines_between_options] || ENV['SUPPRESS_BLANK_LINES_BETWEEN_OPTIONS']

  stream        = options[:stream] || $stdout
  program_name  = options[:program_name] || File.basename($0)

  info_lines    = options[:info_lines]
  case info_lines
  when ::Array

    ;
  when ::NilClass

    info_lines  = []
  else

    info_lines  = [ info_lines ] unless [ :each, :empty? ].all? { |m| info_lines.respond_to? m }
  end
  info_lines  = info_lines.map do |line|

    case line
    when :version

      CLI_helpers_.generate_version_string_ options
    else

      line
    end
  end

  values            = options[:values] || ''
  values            = " #{values}" if !values.empty? && ' ' != values[0]

  flags_and_options = options[:flags_and_options] || ' [ ... flags and options ... ]'
  flags_and_options = " #{flags_and_options}" if !flags_and_options.empty? && ' ' != flags_and_options[0]

  default_indicator = options[:default_indicator] || '(default)'
  default_indicator = nil if default_indicator.empty?

  # sift the specifications to sort out which are value-option
  # specifications (VOAs)

  voas = {}

  specifications.select { |s| s.name =~ /^-+[a-zA-Z0-3_-]+[=:].+/ }.each do |s|

    s.name =~ /^(-+[a-zA-Z0-3_-]+)[=:](.+)$/

    voas[$1]  =   [] unless voas.has_key? $1
    voas[$1]  <<  [ s, $2 ]
  end

  fas = {}

  specifications.select { |s| AliasSpecification === s }.each do |s|

    fas[s.name] =   [] unless fas.has_key? $1
    fas[s.name] <<  s
  end

  specifications  = specifications.reject { |s| s.name =~ /^-+[a-zA-Z0-3_-]+[=:].+/ }

  info_lines.each { |info_line| stream.puts info_line } unless info_lines.empty?

  stream.puts "USAGE: #{program_name}#{flags_and_options}#{values}"
  stream.puts

  unless specifications.empty?

    stream.puts "flags/options:"
    stream.puts
    specifications.each do |s|

      case s
      when AliasSpecification

        next
      when FlagSpecification

        if fas.has_key? s.name

          fas[s.name].each do |fa|

            fa.aliases.each { |al| stream.puts "\t#{al}" }
          end
        end
        s.aliases.each { |al| stream.puts "\t#{al}" }
        stream.puts "\t#{s.name}"
        stream.puts "\t\t#{s.help}"
      when OptionSpecification

        if voas.has_key? s.name

          voas[s.name].each do |ar|

            ar[0].aliases.each { |al| stream.puts "\t#{al} #{ar[0].name}" }
          end
        end
        s.aliases.each { |al| stream.puts "\t#{al} <value>" }
        stream.puts "\t#{s.name}=<value>"
        stream.puts "\t\t#{s.help}"
        unless s.values_range.empty?

          d = s.default_value

          stream.puts "\t\twhere <value> one of:"
          s.values_range.each do |v|

            if default_indicator && v == d

              stream.puts "\t\t\t#{v}\t#{default_indicator}"
            else

              stream.puts "\t\t\t#{v}"
            end
          end
        end
      end
      stream.puts unless suppress_blanks
    end
  end

  exit_code = options[:exit_code] || options[:exit]

  exit exit_code if exit_code
end

.show_version(specifications, options = {}) ⇒ Object

Displays version for the program according to the given specifications and options

Signature

  • Parameters:

    • specifications (+Array+) The arguments array. May not be nil. Defaults to ARGV.
    • options An options hash, containing any of the following options.
  • Options:

    • :exit a program exit code; exit() not called if not specified (or nil).
    • :program_name program name; inferred from $0 if not specified.
    • :stream output stream; $stdout if not specified.
    • :version an array (of N elements, each of which will be separated by a period '.'), or a string. Must be specified if not :version_major.
    • :version_major a number or string. Only considered and must be specified if :version is not.
    • :version_minor a number or string. Only considered if :version is not.
    • :version_revision a number or string. Only considered if :version is not.
    • :version_build a number or string. Only considered if :version is not.
    • :version_prefix optional string to prefix the version number(s).

Raises:

  • (ArgumentError)


294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/clasp/cli.rb', line 294

def self.show_version specifications, options = {}

  options ||= {}

  raise ArgumentError, "specifications may not be nil" if specifications.nil?
  raise TypeError, "specifications must be an array or must respond to each, reject and select" unless ::Array === specifications || (specifications.respond_to?(:each) && specifications.respond_to?(:reject) && specifications.respond_to?(:select))

  constants = CLI_helpers_::Constants

  specifications.each { |s| raise ::TypeError, "each element in specifications array must be one of the types #{constants::VALID_ALIAS_TYPES_STRING}" unless constants::VALID_ALIAS_TYPES.any? { |c| c === s } }

  stream = options[:stream] || $stdout

  version_string = CLI_helpers_.generate_version_string_ options

  stream.puts version_string

  exit_code = options[:exit_code] || options[:exit]

  exit exit_code if exit_code
end