Class: Bovem::Command

Inherits:
Object
  • Object
show all
Includes:
Bovem::CommandMethods::Children, Bovem::CommandMethods::Help
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, #commands?, #get_options, #option, #options?

Methods included from Bovem::CommandMethods::Help

#show_help

Constructor Details

#initialize(options = {}, &block) ⇒ Command

Creates a new command.

Parameters:

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

    The settings to initialize the command with.



327
328
329
330
# File 'lib/bovem/command.rb', line 327

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

Instance Attribute Details

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

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.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

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

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.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#applicationApplication

Returns the application this command belongs to.

Returns:

  • (Application)

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



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#argumentsArray (readonly)

Returns The arguments provided to this command.

Returns:

  • (Array)

    The arguments provided to this command.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  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.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

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

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.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#commandsArray (readonly)

Returns The subcommands associated to this command.

Returns:

  • (Array)

    The subcommands associated to this command.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#description(value = nil) ⇒ String

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.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#i18nI18n (readonly)

Returns A i18n helper.

Returns:

  • (I18n)

    A i18n helper.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#name(value = nil) ⇒ String

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.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#optionsArray (readonly)

Returns The options available for this command.

Returns:

  • (Array)

    The options available for this command.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#parentCommand

Returns The parent of this command.

Returns:

  • (Command)

    The parent of this command.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

#synopsis(value = nil) ⇒ String

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.



309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/bovem/command.rb', line 309

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
  attr_reader :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 unless 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 = ":")
    return nil if application?
    [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
  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 unless 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 unless 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 unless 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
    application? ? self : @application
  end

  # Checks if the command is an application.
  #
  # @return [Boolean] `true` if command is an application, `false` otherwise.
  def 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 description?
    description.present?
  end

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

  # Setups the command.
  #
  # @param options [Hash] The settings for this command.
  # @return [Command] The command.
  def setup_with(options = {})
    options = {} unless 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
        send(method, value)
      elsif respond_to?(method + "=")
        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? # We have a subcommand to call
      commands[subcommand[:name]].execute(subcommand[:args])
    elsif action # Run our action
      # Run the before hook
      perform_action
    else # Show the help
      show_help
    end
  end

  private

  # :nodoc:
  def setup_i18n(options)
    @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
  end

  # :nodoc:
  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

  # :nodoc:
  def execute_hook(hook)
    return unless hook
    hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
  end

  # :nodoc:
  def perform_action
    execute_hook(before)

    # Run the action
    execute_hook(action)

    # Run the after hook
    execute_hook(after)
  end
end

Instance Method Details

#application?Boolean

Checks if the command is an application.

Returns:

  • (Boolean)

    true if command is an application, false otherwise.



424
425
426
# File 'lib/bovem/command.rb', line 424

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

#banner?Boolean

Check if this command has a banner.

Returns:

  • (Boolean)

    true if this command has a banner, false otherwise.



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

def banner?
  banner.present?
end

#description?Boolean

Check if this command has a description.

Returns:

  • (Boolean)

    true if this command has a description, false otherwise.



431
432
433
# File 'lib/bovem/command.rb', line 431

def description?
  description.present?
end

#execute(args) ⇒ Object

Executes this command, running its action or a subcommand.

Parameters:

  • args (Array)

    The arguments to pass to the command.



466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/bovem/command.rb', line 466

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

  if subcommand.present? # We have a subcommand to call
    commands[subcommand[:name]].execute(subcommand[:args])
  elsif action # Run our action
    # Run the before hook
    perform_action
  else # Show the help
    show_help
  end
end

#full_name(suffix = nil, separator = ":") ⇒ String

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.



346
347
348
349
# File 'lib/bovem/command.rb', line 346

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

#setup_with(options = {}) ⇒ Command

Setups the command.

Parameters:

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

    The settings for this command.

Returns:



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/bovem/command.rb', line 446

def setup_with(options = {})
  options = {} unless 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
      send(method, value)
    elsif respond_to?(method + "=")
      send(method + "=", value)
    end
  end

  self
end