Module: Ramaze::Bin::Runner

Defined in:
lib/ramaze/bin/runner.rb

Overview

Module used for running a particular command based on the specified command line arguments.

Usage

 ramaze --help # Shows a help message
 ramaze -h     # Shows a help message as well
 ramaze -v     # Shows the version of Ramaze

 ramaze [COMMAND] # Runs [COMMAND]

Author:

Since:

Constant Summary

Commands =

Hash containing all the available commands, their names and their classes.

Since:

  • 21-07-2011

{
  :start   => Ramaze::Bin::Start,
  :stop    => Ramaze::Bin::Stop,
  :restart => Ramaze::Bin::Restart,
  :status  => Ramaze::Bin::Status,
  :create  => Ramaze::Bin::Create,
  :console => Ramaze::Bin::Console,
}
"Ramaze is a simple, light and modular open-source web application\nframework written in Ruby.\n\nUsage:\nramaze [COMMAND] [OPTIONS]\n\nExample:\nramaze create blog\n".strip

Class Method Summary (collapse)

Class Method Details

+ (Array) commands_info

Generates an array of "rows" where each row contains the name and description of a command. The descriptions of all commands are aligned based on the length of the longest command name.

Returns:

  • (Array)

Author:

  • Yorick Peterse

Since:

  • 21-07-2011



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ramaze/bin/runner.rb', line 116

def self.commands_info
    cmds    = []
    longest = Commands.map { |name, klass| name.to_s }.sort[0].size

    Commands.each do |name, klass|
      name = name.to_s
      desc = ''

      # Try to extract the command description
      if klass.respond_to?(:const_defined?) \
      and klass.const_defined?(:Description)
        desc = klass.const_get(:Description)
      end

      # Align the description based on the length of the name
      while name.size <= longest do
        name += ' '
      end

      cmds.push(["#{name}    #{desc}"])
    end

    return cmds
  end
end

+ (Object) run(argv = ARGV)

Runs a particular command based on the specified array.

Examples:

Ramaze::Bin::Runner.run(ARGV)
Ramaze::Bin::Runner.run(['start', '--help'])

Parameters:

  • argv (Array) (defaults to: ARGV)

    An array containing command line arguments, set to ARGV by default.

Author:

  • Yorick Peterse

Since:

  • 21-07-2011



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ramaze/bin/runner.rb', line 67

def self.run(argv=ARGV)
  op = OptionParser.new do |opt|
    opt.         = 
    opt.summary_indent = '  '

    opt.separator "\nCommands:\n  #{commands_info.join("\n  ")}"

    # Show all the common options
    opt.separator "\nOptions:\n"

    # Show the version of Ramaze
    opt.on('-v', '--version', 'Shows the version of Ramaze') do
      puts Ramaze::VERSION
      exit
    end

    opt.on('-h', '--help', 'Shows this help message') do
      puts op
      exit
    end
  end

  op.order!(argv)

  # Show a help message if no command has been specified
  if !argv[0]
    puts op.to_s
    exit
  end

  cmd = argv.delete_at(0).to_sym

  if Commands.key?(cmd)
    cmd = Commands[cmd].new
    cmd.run(argv)
  else
    abort 'The specified command is invalid'
  end
end