Class: Consoler::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/consoler/application.rb

Overview

Consoler application

Examples:

A simple application

# create a application
app = Consoler::Application.new description: 'A simple app'

# define a command
app.build 'target [--clean]' do |target, clean|
  # clean contains a boolean
  clean_up if clean

  # target contains a string
  build_project target
end
app.run(['build', 'production', '--clean'])

# this does not match, nothing is executed and the usage message is printed
app.run(['deploy', 'production'])

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Application

Create a consoler application

Parameters:

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

    Options for the application

Options Hash (options):

  • :description (String)

    The description for the application (optional)



32
33
34
35
# File 'lib/consoler/application.rb', line 32

def initialize(options={})
  @description = options[:description]
  @commands = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command_name, input = nil) {|...| ... } ⇒ nil

Register a command for this app

Parameters:

  • command_name (Symbol)

    Name of the command

  • input (String, Consoler::Application) (defaults to: nil)

    Options definition or a complete subapp

Yields:

  • (...)

    Executed when the action is matched with parameters based on your options

Returns:

  • (nil)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/consoler/application.rb', line 43

def method_missing(command_name, input = nil, &block)
  action = nil
  options_def = ''

  unless block.nil? then
    action = block
    options_def = input

    if not options_def.nil? and not options_def.instance_of? String then
      raise 'Invalid options'
    end
  end

  if input.instance_of? Consoler::Application then
    action = input
    options_def = ''
  end

  if action.nil? then
    raise 'Invalid subapp/block'
  end

  command = command_name.to_s

  _add_command(command, options_def, action)

  return nil
end

Instance Method Details

#run(args = ARGV, disable_usage_message = false) ⇒ mixed

Run the application with a list of arguments

Parameters:

  • args (Array) (defaults to: ARGV)

    Arguments

  • disable_usage_message (Boolean) (defaults to: false)

    Disable the usage message when nothing it matched

Returns:

  • (mixed)

    Result of your matched command, nil otherwise



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/consoler/application.rb', line 77

def run(args = ARGV, disable_usage_message = false)
  # TODO signal handling of some kind?

  result, matched = _run(args.dup)

  if not matched and not disable_usage_message
    usage
  end

  return result
end

#usageObject

Show the usage message

Contains all commands and options, including subapps



92
93
94
95
96
97
# File 'lib/consoler/application.rb', line 92

def usage
  puts "#{@description}\n\n" unless @description.nil?
  puts 'Usage:'

  _commands_usage $0
end