Class: Bovem::Command

Inherits:
Object
  • Object
show all
Includes:
Bovem::CommandMethods::Children, Bovem::CommandMethods::Help, Lazier::I18n
Defined in:
lib/bovem/command.rb

Overview

This class represent a command (action) for Bovem.

Every command has the execution block and a set of option. Optionally, it also has before and after hooks.

Direct Known Subclasses

Application

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Bovem::CommandMethods::Children

#argument, #clear_commands, #clear_options, #command, #get_options, #has_commands?, #has_options?, #option

Methods included from Bovem::CommandMethods::Help

#show_help

Constructor Details

- (Command) initialize(options = {}, &block)

Creates a new command.

Parameters:

  • options (Hash) (defaults to: {})

    The settings to initialize the command with.



339
340
341
342
# File 'lib/bovem/command.rb', line 339

def initialize(options = {}, &block)
  setup_with(options)
  instance_eval(&block) if block_given?
end

Instance Attribute Details

- (Proc|Symbol|NilClass) action(method = nil, &hook)

Reads and optionally sets the action of this command.

A command action is only executed if no subcommand is executed.

Parameters:

  • method (String|Symbol|NilClass) (defaults to: nil)

    The method of the application to hookup.

  • hook (Proc)

    The block to hookup if method is not provided.

Returns:

  • (Proc|Symbol|NilClass)

    The action of this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (Proc|Symbol|NilClass) after(method = nil, &hook)

Sets the after hook, that is a block executed after the action of this command.

This hook is only executed if no subcommand is executed.

Parameters:

  • method (String|Symbol|NilClass) (defaults to: nil)

    The method of the application to hookup.

  • hook (Proc)

    The block to hookup if method is not provided.

Returns:

  • (Proc|Symbol|NilClass)

    The after hook of this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (Application) application

Returns the application this command belongs to.

Returns:

  • (Application)

    The application this command belongs to or self, if the command is an Application.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (Array) arguments (readonly)

Returns The arguments provided to this command.

Returns:

  • (Array)

    The arguments provided to this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

Reads and optionally sets the description of this command.

Parameters:

  • value (NilClass|Object) (defaults to: nil)

    The new description of this command.

Returns:

  • (String)

    The description of this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (Proc|Symbol|NilClass) before(method = nil, &hook)

Reads and optionally sets the before hook, that is a block executed before the action of this command.

This hook is only executed if no subcommand is executed.

Parameters:

  • method (String|Symbol|NilClass) (defaults to: nil)

    The method of the application to hookup.

  • hook (Proc)

    The block to hookup if method is not provided.

Returns:

  • (Proc|Symbol|NilClass)

    The before hook of this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (Array) commands (readonly)

Returns The subcommands associated to this command.

Returns:

  • (Array)

    The subcommands associated to this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (String) description(value = nil)

Reads and optionally sets the short description of this command.

Parameters:

  • value (NilClass|Object) (defaults to: nil)

    The new short description of this command.

Returns:

  • (String)

    The short description of this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (String) name(value = nil)

Reads and optionally sets the name of this command.

Parameters:

  • value (NilClass|Object) (defaults to: nil)

    The new name of this command.

Returns:

  • (String)

    The name of this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (Array) options (readonly)

Returns The options available for this command.

Returns:

  • (Array)

    The options available for this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (Command) parent

Returns The parent of this command.

Returns:

  • (Command)

    The parent of this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

- (String) synopsis(value = nil)

Reads and optionally sets the synopsis of this command.

Parameters:

  • value (NilClass|Object) (defaults to: nil)

    The new synopsis of this command.

Returns:

  • (String)

    The synopsis of this command.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/bovem/command.rb', line 321

class Command
  attr_accessor :name
  attr_accessor :description
  attr_accessor :banner
  attr_accessor :synopsis
  attr_accessor :before
  attr_accessor :action
  attr_accessor :after
  attr_accessor :application
  attr_accessor :parent

  include Lazier::I18n
  include Bovem::CommandMethods::Help
  include Bovem::CommandMethods::Children

  # Creates a new command.
  #
  # @param options [Hash] The settings to initialize the command with.
  def initialize(options = {}, &block)
    setup_with(options)
    instance_eval(&block) if block_given?
  end

  # Reads and optionally sets the name of this command.
  #
  # @param value [NilClass|Object] The new name of this command.
  # @return [String] The name of this command.
  def name(value = nil)
    @name = value if !value.nil?
    @name
  end

  # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
  #
  # @param suffix [String] A suffix to append.
  # @param separator [String] The separator to use for components.
  # @return [String] The full name.
  def full_name(suffix = nil, separator = ":")
    if is_application? then
      nil
    else
      [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
    end
  end

  # Reads and optionally sets the short description of this command.
  #
  # @param value [NilClass|Object] The new short description of this command.
  # @return [String] The short description of this command.
  def description(value = nil)
    @description = value if !value.nil?
    @description
  end

  # Reads and optionally sets the description of this command.
  #
  # @param value [NilClass|Object] The new description of this command.
  # @return [String] The description of this command.
  def banner(value = nil)
    @banner = value if !value.nil?
    @banner
  end

  # Reads and optionally sets the synopsis of this command.
  #
  # @param value [NilClass|Object] The new synopsis of this command.
  # @return [String] The synopsis of this command.
  def synopsis(value = nil)
    @synopsis = value if !value.nil?
    @synopsis
  end

  # Reads and optionally sets the before hook, that is a block executed before the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The before hook of this command.
  def before(method = nil, &hook)
    @before = assign_hook(method, &hook) if method || hook
    @before
  end

  # Reads and optionally sets the action of this command.
  #
  # A command action is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The action of this command.
  def action(method = nil, &hook)
    @action = assign_hook(method, &hook) if method || hook
    @action
  end

  # Sets the after hook, that is a block executed after the action of this command.
  #
  # This hook is only executed if no subcommand is executed.
  #
  # @param method [String|Symbol|NilClass] The method of the application to hookup.
  # @param hook [Proc] The block to hookup if method is not provided.
  # @return [Proc|Symbol|NilClass] The after hook of this command.
  def after(method = nil, &hook)
    @after = assign_hook(method, &hook) if method || hook
    @after
  end

  # Returns the application this command belongs to.
  #
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
  def application
    is_application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def is_application?
    is_a?(Bovem::Application)
  end

  # Check if this command has a description.
  #
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
  def has_description?
    description.present?
  end

  # Check if this command has a banner.
  #
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
  def has_banner?
    banner.present?
  end

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} if !options.is_a?(::Hash)
    setup_i18n(options)

    options.each_pair do |option, value|
      method = option.to_s

      if respond_to?(method) && self.method(method).arity != 0 then
        send(method, value)
      elsif respond_to?(method + "=") then
        send(method + "=", value)
      end
    end

    self
  end

  # Executes this command, running its action or a subcommand.
  #
  # @param args [Array] The arguments to pass to the command.
  def execute(args)
    subcommand = Bovem::Parser.parse(self, args)

    if subcommand.present? then # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action then # Run our action
      # Run the before hook
      execute_hook(before)

      # Run the action
      execute_hook(action)

      # Run the after hook
      execute_hook(after)
    else # Show the help
      show_help
    end
  end

  private
    # Setups the application localization.
    #
    # @param options [Hash] The settings for this command.
    def setup_i18n(options)
      i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
      self.i18n = (options[:locale]).ensure_string
    end

    # Assigns a hook to a command.
    #
    # @param method [String|Symbol|NilClass] The method of the application to hookup.
    # @param block [Proc] The block to hookup if method is not provided.
    def assign_hook(method, &hook)
      assigned = nil
      assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
      assigned = hook if !assigned && hook && hook.arity == 1
      assigned
    end

    # Executes a hook.
    #
    # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
    def execute_hook(hook)
      if hook then
        hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
      end
    end
end

Instance Method Details

- (Object) execute(args)

Executes this command, running its action or a subcommand.

Parameters:

  • args (Array)

    The arguments to pass to the command.



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/bovem/command.rb', line 481

def execute(args)
  subcommand = Bovem::Parser.parse(self, args)

  if subcommand.present? then # We have a subcommand to call
    commands[subcommand[:name]].execute(subcommand[:args])
  elsif action then # Run our action
    # Run the before hook
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  else # Show the help
    show_help
  end
end

- (String) full_name(suffix = nil, separator = ":")

Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix

Parameters:

  • suffix (String) (defaults to: nil)

    A suffix to append.

  • separator (String) (defaults to: ":")

    The separator to use for components.

Returns:

  • (String)

    The full name.



358
359
360
361
362
363
364
# File 'lib/bovem/command.rb', line 358

def full_name(suffix = nil, separator = ":")
  if is_application? then
    nil
  else
    [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
  end
end

- (Boolean) has_banner?

Check if this command has a banner.

Returns:

  • (Boolean)

    true if this command has a banner, false otherwise.



453
454
455
# File 'lib/bovem/command.rb', line 453

def has_banner?
  banner.present?
end

- (Boolean) has_description?

Check if this command has a description.

Returns:

  • (Boolean)

    true if this command has a description, false otherwise.



446
447
448
# File 'lib/bovem/command.rb', line 446

def has_description?
  description.present?
end

- (Boolean) is_application?

Checks if the command is an application.

Returns:

  • (Boolean)

    true if command is an application, false otherwise.



439
440
441
# File 'lib/bovem/command.rb', line 439

def is_application?
  is_a?(Bovem::Application)
end

- (Command) setup_with(options = {})

Setups the command.

Parameters:

  • options (Hash) (defaults to: {})

    The settings for this command.

Returns:



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/bovem/command.rb', line 461

def setup_with(options = {})
  options = {} if !options.is_a?(::Hash)
  setup_i18n(options)

  options.each_pair do |option, value|
    method = option.to_s

    if respond_to?(method) && self.method(method).arity != 0 then
      send(method, value)
    elsif respond_to?(method + "=") then
      send(method + "=", value)
    end
  end

  self
end