Class: LibCLImate::Climate
- Inherits:
-
Object
- Object
- LibCLImate::Climate
- Includes:
- Xqsr3::Quality::ParameterChecking
- Defined in:
- lib/libclimate/climate.rb
Overview
Class used to gather together the CLI specification, and execute it
The standard usage pattern is as follows:
PROGRAM_VERSION = [ 0, 1, 2 ]
= {}
climate = LibCLImate::Climate.new do |cl|
cl.add_flag('--verbose', alias: '-v', help: 'Makes program output verbose') { [:verbose] = true }
cl.add_option('--flavour', alias: '-f', help: 'Specifies the flavour') do |o, a|
[:flavour] = check_flavour(o.value) or cl.abort "Invalid flavour '#{o.value}'; use --help for usage"
end
cl.usage_value = '<value-1> [ ... <value-N> ]'
cl.info_lines = [
'ACME CLI program (using libCLImate)',
:version,
'An example program',
]
end
Instance Attribute Summary collapse
-
#aliases ⇒ Array
readonly
An array of aliases attached to the climate instance, whose contents should be modified by adding (or removing) CLASP aliases.
-
#exit_on_missing ⇒ Boolean
Indicates whether exit will be called (with non-zero exit code) when a required command-line option is missing.
-
#exit_on_unknown ⇒ Boolean
Indicates whether exit will be called (with non-zero exit code) when an unknown command-line flag or option is encountered.
-
#exit_on_usage ⇒ Boolean
Indicates whether exit will be called (with zero exit code) when usage/version is requested on the command-line.
-
#info_lines ⇒ Array
Optional array of string of program-information that will be written before the rest of the usage block when usage is requested on the command-line.
-
#program_name ⇒ String
A program name; defaults to the name of the executing script.
-
#stderr ⇒ IO
The output stream for contingent output; defaults to $stderr.
-
#stdout ⇒ IO
The output stream for normative output; defaults to $stdout.
-
#usage_values ⇒ String
Optional string to describe the program values, eg <xyz “[ { <<directory> | <file> } ]”.
-
#version ⇒ String, Array
A version string or an array of integers representing the version component(s).
Instance Method Summary collapse
-
#abort(message, options = {}) ⇒ Object
Calls abort() with the given message prefixed by the program_name.
-
#add_alias(name, *aliases) ⇒ Object
Adds a flag to
aliases
. -
#add_flag(name, options = {}, &block) ⇒ Object
Adds a flag to
aliases
. -
#add_option(name, options = {}, &block) ⇒ Object
- Options -
:alias
:aliases
:help
:values_range
:default_value
-
:extras
::.
-
- Options -
-
#initialize(options = {}) {|_self| ... } ⇒ Climate
constructor
Creates an instance of the Climate class.
-
#run(argv = ARGV) ⇒ Object
Executes the prepared Climate instance.
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Climate
Creates an instance of the Climate class.
Signature
-
Parameters:
options:
-
An options hash, containing any of the following options.
-
Options:
:no_help_flag
-
Prevents the use of the CLASP::Flag.Help flag-alias
:no_version_flag
-
Prevents the use of the CLASP::Version.Help flag-alias
:version
-
A version specification. If not specified, this is
inferred
- Block
-
An optional block which receives the constructing instance, allowing the user to modify the attributes.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/libclimate/climate.rb', line 267 def initialize(={}) # :yields: climate ||= {} program_name = File.basename($0) program_name = (program_name =~ /\.rb$/) ? "#$`(#$&)" : program_name if defined? Colcon program_name = "#{::Colcon::Decorations::Bold}#{program_name}#{::Colcon::Decorations::Unbold}" end @aliases = [] @exit_on_unknown = true @exit_on_missing = true @exit_on_usage = true @info_lines = nil @program_name = program_name @stdout = $stdout @stderr = $stderr @usage_values = usage_values version_context = [:version_context] @version = [:version] || infer_version_(version_context) @aliases << CLASP::Flag.Help(handle: proc { show_usage_ }) unless [:no_help_flag] @aliases << CLASP::Flag.Version(handle: proc { show_version_ }) unless [:no_version_flag] yield self if block_given? end |
Instance Attribute Details
#aliases ⇒ Array (readonly)
An array of aliases attached to the climate instance, whose contents should be modified by adding (or removing) CLASP aliases
299 300 301 |
# File 'lib/libclimate/climate.rb', line 299 def aliases @aliases end |
#exit_on_missing ⇒ Boolean
Indicates whether exit will be called (with non-zero exit code) when a required command-line option is missing
305 306 307 |
# File 'lib/libclimate/climate.rb', line 305 def exit_on_missing @exit_on_missing end |
#exit_on_unknown ⇒ Boolean
Indicates whether exit will be called (with non-zero exit code) when an unknown command-line flag or option is encountered
310 311 312 |
# File 'lib/libclimate/climate.rb', line 310 def exit_on_unknown @exit_on_unknown end |
#exit_on_usage ⇒ Boolean
Returns Indicates whether exit will be called (with zero exit code) when usage/version is requested on the command-line.
312 313 314 |
# File 'lib/libclimate/climate.rb', line 312 def exit_on_usage @exit_on_usage end |
#info_lines ⇒ Array
Returns Optional array of string of program-information that will be written before the rest of the usage block when usage is requested on the command-line.
314 315 316 |
# File 'lib/libclimate/climate.rb', line 314 def info_lines @info_lines end |
#program_name ⇒ String
Returns A program name; defaults to the name of the executing script.
316 317 318 |
# File 'lib/libclimate/climate.rb', line 316 def program_name @program_name end |
#stderr ⇒ IO
Returns The output stream for contingent output; defaults to $stderr.
320 321 322 |
# File 'lib/libclimate/climate.rb', line 320 def stderr @stderr end |
#stdout ⇒ IO
Returns The output stream for normative output; defaults to $stdout.
318 319 320 |
# File 'lib/libclimate/climate.rb', line 318 def stdout @stdout end |
#usage_values ⇒ String
Returns Optional string to describe the program values, eg <xyz “[ { <<directory> | <file> } ]”.
322 323 324 |
# File 'lib/libclimate/climate.rb', line 322 def usage_values @usage_values end |
#version ⇒ String, Array
Returns A version string or an array of integers representing the version component(s).
324 325 326 |
# File 'lib/libclimate/climate.rb', line 324 def version @version end |
Instance Method Details
#abort(message, options = {}) ⇒ Object
Calls abort() with the given message prefixed by the program_name
Signature
-
Parameters:
message
-
The message string
options
-
An option hash, containing any of the following options
-
Options:
:stream
-
optional The output stream to use. Defaults to the value of the attribute
stderr
.
:program_name
-
optional Uses the given value rather than the
program_name
attribute; does not prefix if the empty string
:exit
-
optional The exit code. Defaults to 1. Does not exit if
nil
specified.
-
Return: The combined message string, if
exit()
not called.
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
# File 'lib/libclimate/climate.rb', line 556 def abort , ={} prog_name = [:program_name] prog_name ||= program_name prog_name ||= '' stream = [:stream] stream ||= stderr stream ||= $stderr exit_code = .has_key?(:exit) ? [:exit] : 1 if prog_name.empty? msg = else msg = "#{prog_name}: #{}" end stream.puts msg exit(exit_code) if exit_code msg end |
#add_alias(name, *aliases) ⇒ Object
Adds a flag to aliases
Signature
-
Parameters
name
-
The flag/option name or the valued option
aliases
-
One or more aliases
Examples
Alias(es) of a flag (single statement)
climate.add_flag(‘–mark-missing’, alias: ‘-x’)
climate.add_flag(‘–absolute-path’, aliases: [ ‘-abs’, ‘-p’ ])
Alias(es) of a flag (multiple statements)
climate.add_flag(‘–mark-missing’) climate.add_alias(‘–mark-missing’, ‘-x’)
climate.add_flag(‘–absolute-path’) climate.add_alias(‘–absolute-path’, ‘-abs’, ‘-p’)
Alias(es) of an option (single statement)
climate.add_option(‘–add-patterns’, alias: ‘-p’)
Alias(es) of an option (multiple statements)
climate.add_option(‘–add-patterns’) climate.add_alias(‘–add-patterns’, ‘-p’)
Alias of a valued option (which has to be multiple statements)
climate.add_option(‘–verbosity’) climate.add_alias(‘–verbosity=succinct’, ‘-s’) climate.add_alias(‘–verbosity=verbose’, ‘-v’)
657 658 659 660 661 662 663 |
# File 'lib/libclimate/climate.rb', line 657 def add_alias(name, *aliases) check_parameter name, 'name', allow_nil: false, types: [ ::String, ::Symbol ] raise ArgumentError, "must supply at least one alias" if aliases.empty? self.aliases << CLASP.Option(name, aliases: aliases) end |
#add_flag(name, options = {}, &block) ⇒ Object
Adds a flag to aliases
Signature
-
Parameters
name
-
The flag name
options
-
An options hash, containing any of the following options.
-
Options
:help
:alias
:aliases
:extras
597 598 599 600 601 602 |
# File 'lib/libclimate/climate.rb', line 597 def add_flag(name, ={}, &block) check_parameter name, 'name', allow_nil: false, types: [ ::String, ::Symbol ] aliases << CLASP.Flag(name, **, &block) end |
#add_option(name, options = {}, &block) ⇒ Object
-
Options
:alias
:aliases
:help
:values_range
:default_value
:extras
612 613 614 615 616 617 |
# File 'lib/libclimate/climate.rb', line 612 def add_option(name, ={}, &block) check_parameter name, 'name', allow_nil: false, types: [ ::String, ::Symbol ] aliases << CLASP.Option(name, **, &block) end |
#run(argv = ARGV) ⇒ Object
Executes the prepared Climate instance
Signature
-
Parameters:
argv
-
The array of arguments; defaults to
ARGV
-
Returns: an instance of a type derived from
::Hash
with the additional attributesflags
,options
,values
, andargv
.
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/libclimate/climate.rb', line 337 def run argv = ARGV raise ArgumentError, "argv may not be nil" if argv.nil? arguments = CLASP::Arguments.new argv, aliases flags = arguments.flags = arguments. values = arguments.values.to_a results = { flags: { given: flags, handled: [], unhandled: [], unknown: [], }, options: { given: , handled: [], unhandled: [], unknown: [], }, values: values, missing_option_aliases: [], } flags.each do |f| al = aliases.detect do |a| a.kind_of?(::CLASP::Flag) && f.name == a.name end if al selector = :unhandled # see if it has an :action attribute (which will have been # monkey-patched to CLASP.Flag() if al.respond_to?(:action) && !al.action.nil? al.action.call(f, al) selector = :handled else ex = al.extras case ex when ::Hash if ex.has_key? :handle ex[:handle].call(f, al) selector = :handled end end end results[:flags][selector] << f else = "unrecognised flag '#{f}'; use --help for usage" if exit_on_unknown self.abort else if program_name && !program_name.empty? = "#{program_name}: #{}" end stderr.puts end results[:flags][:unknown] << f end end .each do |o| al = aliases.detect do |a| a.kind_of?(::CLASP::Option) && o.name == a.name end if al selector = :unhandled # see if it has an :action attribute (which will have been # monkey-patched to CLASP.Option() if al.respond_to?(:action) && !al.action.nil? al.action.call(o, al) selector = :handled else ex = al.extras case ex when ::Hash if ex.has_key? :handle ex[:handle].call(o, al) selector = :handled end end end results[:options][selector] << o else = "unrecognised option '#{o}'; use --help for usage" if exit_on_unknown self.abort else if program_name && !program_name.empty? = "#{program_name}: #{}" end stderr.puts end results[:options][:unknown] << o end end # now police any required options required_aliases = aliases.select do |a| a.kind_of?(::CLASP::Option) && a.required? end required_aliases = Hash[required_aliases.map { |a| [ a.name, a ] }] = Hash[results[:options][:given].map { |o| [ o.name, o ]}] required_aliases.each do |k, a| unless .has_key? k = a. if exit_on_missing self.abort else if program_name && !program_name.empty? = "#{program_name}: #{}" end stderr.puts end results[:missing_option_aliases] << a end end def results.flags self[:flags] end def results. self[:options] end def results.values self[:values] end results.define_singleton_method(:argv) do argv end results end |