| 1 |
module Slop |
| 2 |
class MissingCommand < Error |
| 3 |
end |
| 4 |
|
| 5 |
class UnknownCommand < Error |
| 6 |
attr_reader :name |
| 7 |
|
| 8 |
def initialize(msg, name) |
| 9 |
super(msg) |
| 10 |
@name = name |
| 11 |
end |
| 12 |
end |
| 13 |
end |
| 14 |
|
| 15 |
module Groonga |
| 16 |
class CommandLineParser |
| 17 |
def initialize(program_name=nil) |
| 18 |
$0 ||= nil |
| 19 |
@program_name = program_name || $0 |
| 20 |
@commands = [] |
| 21 |
@action_runner = ActionRunner.new |
| 22 |
@options = Slop::Options.new |
| 23 |
setup_options |
| 24 |
end |
| 25 |
|
| 26 |
def options |
| 27 |
yield(@options) if block_given? |
| 28 |
@options |
| 29 |
end |
| 30 |
|
| 31 |
def add_command(name) |
| 32 |
command = Command.new(@program_name, name) |
| 33 |
setup_common_options(command.options) |
| 34 |
yield(command) |
| 35 |
@commands << command |
| 36 |
end |
| 37 |
|
| 38 |
def add_action(&action) |
| 39 |
@action_runner.add(&action) |
| 40 |
end |
| 41 |
|
| 42 |
def parse(command_line) |
| 43 |
if @commands.empty? |
| 44 |
result = @options.parse(command_line) |
| 45 |
apply_actions(result) |
| 46 |
return result |
| 47 |
end |
| 48 |
|
| 49 |
if command_line.empty? |
| 50 |
message = "Command is missing" |
| 51 |
raise Slop::MissingCommand.new(message) |
| 52 |
end |
| 53 |
|
| 54 |
first_argument = command_line.first |
| 55 |
if first_argument.start_with?("-") |
| 56 |
result = @options.parse(command_line) |
| 57 |
apply_actions(result) |
| 58 |
return result |
| 59 |
end |
| 60 |
|
| 61 |
command_name = first_argument |
| 62 |
command = find_command(command_name) |
| 63 |
if command.nil? |
| 64 |
message = "Unknown command: <#{command_name}>" |
| 65 |
raise Slop::UnknownCommand.new(message, command_name) |
| 66 |
end |
| 67 |
|
| 68 |
command.parse(command_line[1..-1]) |
| 69 |
end |
| 70 |
|
| 71 |
def help_message |
| 72 |
message = @options.to_s |
| 73 |
@commands.each do |command| |
| 74 |
message << "\n" |
| 75 |
indent = " " * 4 |
| 76 |
message << "#{command.description}:\n" if command.description |
| 77 |
message << "#{indent}#{command.options.banner}\n" |
| 78 |
end |
| 79 |
message |
| 80 |
end |
| 81 |
|
| 82 |
private |
| 83 |
def setup_options |
| 84 |
@options.banner = "Usage: #{@program_name} [OPTIONS]" |
| 85 |
setup_common_options(@options) |
| 86 |
@options.on("-h", "--help", "Display this help message.", |
| 87 |
:tail => true) do |
| 88 |
$stdout.puts(help_message) |
| 89 |
end |
| 90 |
end |
| 91 |
|
| 92 |
def setup_common_options(options) |
| 93 |
options.string("--log-path", |
| 94 |
"Change log path (#{Logger.default_path})", |
| 95 |
default: Logger.default_path) |
| 96 |
default_log_level = Logger.default_level |
| 97 |
options.string("--log-level", |
| 98 |
"Change log level (#{default_log_level.name})", |
| 99 |
default: default_log_level) |
| 100 |
end |
| 101 |
|
| 102 |
def find_command(name) |
| 103 |
@commands.find do |command| |
| 104 |
command.name == name |
| 105 |
end |
| 106 |
end |
| 107 |
|
| 108 |
def apply_actions(result) |
| 109 |
@action_runner.run(result) unless result.help? |
| 110 |
end |
| 111 |
|
| 112 |
class ActionRunner |
| 113 |
def initialize |
| 114 |
@actions = [] |
| 115 |
end |
| 116 |
|
| 117 |
def add(&action) |
| 118 |
@actions << action |
| 119 |
end |
| 120 |
|
| 121 |
def run(*args) |
| 122 |
@actions.each do |action| |
| 123 |
action.call(*args) |
| 124 |
end |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
class Command |
| 129 |
attr_reader :name |
| 130 |
attr_reader :options |
| 131 |
attr_accessor :description |
| 132 |
def initialize(program_name, name) |
| 133 |
@program_name = program_name |
| 134 |
@name = name |
| 135 |
@options = Slop::Options.new |
| 136 |
setup_options |
| 137 |
@description = nil |
| 138 |
@action_runner = ActionRunner.new |
| 139 |
end |
| 140 |
|
| 141 |
def add_action(&action) |
| 142 |
@action_runner.add(&action) |
| 143 |
end |
| 144 |
|
| 145 |
def parse(command_line) |
| 146 |
result = @options.parse(command_line) |
| 147 |
@action_runner.run(result) unless result.help? |
| 148 |
result |
| 149 |
end |
| 150 |
|
| 151 |
def help_message |
| 152 |
message = "" |
| 153 |
message << "Description: #{@description}\n" if @description |
| 154 |
message << @options.to_s |
| 155 |
message |
| 156 |
end |
| 157 |
|
| 158 |
private |
| 159 |
def setup_options |
| 160 |
@options.banner = "Usage: #{@program_name} #{@name} [OPTIONS]" |
| 161 |
@options.on("-h", "--help", "Display this help message.", |
| 162 |
:tail => true) do |
| 163 |
$stdout.puts(help_message) |
| 164 |
end |
| 165 |
end |
| 166 |
end |
| 167 |
end |
| 168 |
end |