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.0.1'
- 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]
- Alias =
######################################################################## # backwards-compatible
AliasSpecification
- Flag =
FlagSpecification
- FlagAlias =
FlagSpecification
- Option =
OptionSpecification
- OptionAlias =
OptionSpecification
Class Method Summary collapse
- .Alias(name, *args) ⇒ Object
-
.Flag(name, options = {}, &blk) ⇒ Object
Generator method that obtains a CLASP::FlagSpecification according to the given parameters.
-
.Option(name, options = {}, &blk) ⇒ Object
Generator method that obtains a CLASP::OptionSpecification according to the given parameters.
-
.parse(argv = ARGV, specifications = nil, options = {}) ⇒ Object
TBC (but is a shorthand for calling Arguments.new().
-
.show_usage(specifications, options = {}) ⇒ Object
Displays usage for the program according to the given specifications and options.
-
.show_version(specifications, options = {}) ⇒ Object
Displays version for the program according to the given specifications and options.
Class Method Details
.Alias(name, *args) ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/clasp/specifications.rb', line 513 def CLASP.Alias(name, *args) = args.pop if args[-1].is_a?(::Hash) ||= {} if [:alias] aliases = [ [:alias] ] elsif [:aliases] aliases = [:aliases] else aliases = args end CLASP::AliasSpecification.new name, aliases end |
.Flag(name, options = {}, &blk) ⇒ Object
Generator method that obtains a CLASP::FlagSpecification according to the given parameters
Signature
-
Parameters:
-
name
(::String) The flag name, e.g. ‘–verbose’ -
options
(::Hash) An options hash, containing any of the following options:
-
-
Options:
-
:alias
(::String) An alias, e.g. ‘-v’ -
:aliases
(::Array) An array of aliases, e.g. [ ‘-v’, ‘-verb’ ]. Ignored if:alias
is specified -
:extras
An application-defined object, usually a hash of custom attributes -
:help
(::String) A help string
-
-
Block An optional block that is called when a matching flag argument is found
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/clasp/specifications.rb', line 397 def CLASP.Flag(name, = {}, &blk) aliases = nil help = nil extras = nil .each do |k, v| case k when Symbol case k when :alias aliases = [ v ] if v when :aliases aliases = v unless aliases when :help help = v when :extras extras = v else raise ArgumentError, "invalid option for flag: '#{k}' => '#{v}'" end else raise ArgumentError, "invalid option type for flag: '#{k}' (#{k.class}) => '#{v}'" end end CLASP::FlagSpecification.new(name, aliases, help, extras, &blk) end |
.Option(name, options = {}, &blk) ⇒ Object
Generator method that obtains a CLASP::OptionSpecification according to the given parameters
Signature
-
Parameters:
-
name
(::String) The flag name, e.g. ‘–verbose’ -
options
(::Hash) An options hash, containing any of the following options:
-
-
Options:
-
:alias
(::String) An alias, e.g. ‘-v’ -
:aliases
(::Array) An array of aliases, e.g. [ ‘-v’, ‘-verb’ ]. Ignored if:alias
is specified -
:default_value
The default value for the option -
:default
[DEPRECATED] Alternative to:default_value
-
:extras
An application-defined object, usually a hash of custom attributes -
:help
(::String) A help string -
required
(boolean) Whether the option is required. May benil
-
required_message
(::String) Message to be used when reporting that a required option is missing. May benil
in which case a message of the form “<option-name> not specified; use –help for usage”. If begins with the nul character (“0”), then is used in the place of the <option-name> and placed into the rest of the standard form message -
extras
An application-defined additional parameter. Ifnil
, it is assigned an emptyHash
. -
constraint
(Hash) Constraint to be applied to the parsed values of options matching this specification. NOTE: only integer constraints are supported in the current version -
:values_range
(::Array) An array defining the accepted values for the option -
:values
[DEPRECATED] Alternative to:values_range
-
-
Block An optional block that is called when a matching option argument is found
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/clasp/specifications.rb', line 457 def CLASP.Option(name, = {}, &blk) aliases = nil help = nil values_range = nil default_value = nil required = false = nil constraint = nil extras = nil .each do |k, v| case k when Symbol case k when :alias aliases = [ v ] if v when :aliases aliases = v unless aliases when :help help = v when :values_range, :values values_range = v when :default_value, :default default_value = v when :required required = v when :required_message = v when :extras extras = v when :constraint constraint = v else raise ArgumentError, "invalid option for option: '#{k}' => '#{v}'" end else raise ArgumentError, "invalid option type for option: '#{k}' (#{k.class}) => '#{v}'" end end CLASP::OptionSpecification.new(name, aliases, help, values_range, default_value, required, , constraint, extras, &blk) end |
.parse(argv = ARGV, specifications = nil, options = {}) ⇒ Object
TBC (but is a shorthand for calling Arguments.new()
56 57 58 59 |
# File 'lib/clasp/clasp.rb', line 56 def self.parse(argv = ARGV, specifications = nil, = {}) return Arguments.new(argv, specifications, ) 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 benil
. Defaults toARGV
. -
options
An options hash, containing any of the following options.
-
-
Options:
-
:exit
a program exit code;exit()
not called if not specified (ornil
). -
: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)”. Ifnil
default is used. If empty string no indication given
-
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 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 |
# File 'lib/clasp/cli.rb', line 133 def self.show_usage specifications, ={} ||= {} 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 = [:suppress_blank_lines_between_options] || ENV['SUPPRESS_BLANK_LINES_BETWEEN_OPTIONS'] stream = [:stream] || $stdout program_name = [:program_name] || File.basename($0) info_lines = [: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_ else line end end values = [:values] || '' values = " #{values}" if !values.empty? && ' ' != values[0] = [:flags_and_options] || ' [ ... flags and options ... ]' = " #{}" if !.empty? && ' ' != [0] default_indicator = [: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}#{}#{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 = [:exit_code] || [: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 benil
. Defaults toARGV
. -
options
An options hash, containing any of the following options.
-
-
Options:
-
:exit
a program exit code;exit()
not called if not specified (ornil
). -
: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).
-
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/clasp/cli.rb', line 290 def self.show_version specifications, = {} ||= {} 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 = [:stream] || $stdout version_string = CLI_helpers_.generate_version_string_ stream.puts version_string exit_code = [:exit_code] || [:exit] exit exit_code if exit_code end |