| 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 | |
| 14 | class TimeOption < Option |
| 15 | def call(value) |
| 16 | case value |
| 17 | when /\A\-/ |
| 18 | Time.now - parse_relative_time(value[1..-1]) |
| 19 | else |
| 20 | parse_time(value) |
| 21 | end |
| 22 | end |
| 23 | |
| 24 | private |
| 25 | def parse_relative_time(value) |
| 26 | match_data = /\A |
| 27 | (\d+(?:\.\d+)?)\ * |
| 28 | (s|secs?|seconds?| |
| 29 | m|mins?|minutes?| |
| 30 | h|hours?| |
| 31 | d|days?| |
| 32 | w|weeks?| |
| 33 | months?| |
| 34 | y|years?) |
| 35 | \z/x.match(value) |
| 36 | unless match_data |
| 37 | message = "must be one of them: " |
| 38 | message << "<-Nseconds>, <-Nminutes>, <-Nhours>, <-Ndays>, " |
| 39 | message << "<-Nweeks>, <-Nmonths>, <-Nyears>: " |
| 40 | message << "<#{value}>" |
| 41 | raise ArgumentError, message |
| 42 | end |
| 43 | base = Float(match_data[1]) |
| 44 | case match_data[2] |
| 45 | when "s", "sec", "secs", "second", "seconds" |
| 46 | unit = 1 |
| 47 | when "m", "min", "mins", "minute", "minutes" |
| 48 | unit = 60 |
| 49 | when "h", "hour", "hours" |
| 50 | unit = 60 * 60 |
| 51 | when "d", "day", "days" |
| 52 | unit = 60 * 60 * 24 |
| 53 | when "w", "week", "weeks" |
| 54 | unit = 60 * 60 * 24 * 7 |
| 55 | when "month", "months" |
| 56 | unit = 60 * 60 * 24 * 30 |
| 57 | else |
| 58 | unit = 60 * 60 * 24 * 365 |
| 59 | end |
| 60 | base * unit |
| 61 | end |
| 62 | |
| 63 | def parse_time(value) |
| 64 | case value |
| 65 | when /\A(\d{4})\z/ |
| 66 | Time.local(Integer($1, 10)) |
| 67 | when /\A(\d{4})-(\d{1,2})\z/ |
| 68 | Time.local(Integer($1, 10), |
| 69 | Integer($2, 10)) |
| 70 | when /\A(\d{4})-(\d{1,2})-(\d{1,2})\z/ |
| 71 | Time.local(Integer($1, 10), |
| 72 | Integer($2, 10), |
| 73 | Integer($3, 10)) |
| 74 | when /\A(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2})\z/ |
| 75 | Time.local(Integer($1, 10), |
| 76 | Integer($2, 10), |
| 77 | Integer($3, 10), |
| 78 | Integer($4, 10)) |
| 79 | when /\A(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2})\z/ |
| 80 | Time.local(Integer($1, 10), |
| 81 | Integer($2, 10), |
| 82 | Integer($3, 10), |
| 83 | Integer($4, 10), |
| 84 | Integer($5, 10)) |
| 85 | when /\A(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})\z/ |
| 86 | Time.local(Integer($1, 10), |
| 87 | Integer($2, 10), |
| 88 | Integer($3, 10), |
| 89 | Integer($4, 10), |
| 90 | Integer($5, 10), |
| 91 | Integer($6, 10)) |
| 92 | when /\A(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d+)\z/ |
| 93 | Time.local(Integer($1, 10), |
| 94 | Integer($2, 10), |
| 95 | Integer($3, 10), |
| 96 | Integer($4, 10), |
| 97 | Integer($5, 10), |
| 98 | Integer($6, 10), |
| 99 | (Integer($7, 10) / 1_000_000.0 * 1_000_000).floor) |
| 100 | else |
| 101 | raise ArgumentError, "must be ISO 8601 format: <#{value}>" |
| 102 | end |
| 103 | end |
| 104 | end |
| 105 | end |
| 106 | |
| 107 | module Groonga |
| 108 | class CommandLineParser |
| 109 | def initialize(program_name=nil) |
| 110 | $0 ||= nil |
| 111 | @program_name = program_name || $0 |
| 112 | @commands = [] |
| 113 | @action_runner = ActionRunner.new |
| 114 | @options = Slop::Options.new |
| 115 | setup_options |
| 116 | end |
| 117 | |
| 118 | def options |
| 119 | yield(@options) if block_given? |
| 120 | @options |
| 121 | end |
| 122 | |
| 123 | def add_command(name) |
| 124 | command = Command.new(@program_name, name) |
| 125 | setup_common_options(command.options) |
| 126 | yield(command) |
| 127 | @commands << command |
| 128 | end |
| 129 | |
| 130 | def add_action(&action) |
| 131 | @action_runner.add(&action) |
| 132 | end |
| 133 | |
| 134 | def parse(command_line) |
| 135 | if @commands.empty? |
| 136 | result = @options.parse(command_line) |
| 137 | apply_actions(result) |
| 138 | return result |
| 139 | end |
| 140 | |
| 141 | if command_line.empty? |
| 142 | message = "Command is missing" |
| 143 | raise Slop::MissingCommand.new(message) |
| 144 | end |
| 145 | |
| 146 | first_argument = command_line.first |
| 147 | if first_argument.start_with?("-") |
| 148 | result = @options.parse(command_line) |
| 149 | apply_actions(result) |
| 150 | return result |
| 151 | end |
| 152 | |
| 153 | command_name = first_argument |
| 154 | command = find_command(command_name) |
| 155 | if command.nil? |
| 156 | message = "Unknown command: <#{command_name}>" |
| 157 | raise Slop::UnknownCommand.new(message, command_name) |
| 158 | end |
| 159 | |
| 160 | command.parse(command_line[1..-1]) |
| 161 | end |
| 162 | |
| 163 | def help_message |
| 164 | message = @options.to_s |
| 165 | @commands.each do |command| |
| 166 | message << "\n" |
| 167 | indent = " " * 4 |
| 168 | message << "#{command.description}:\n" if command.description |
| 169 | message << "#{indent}#{command.options.banner}\n" |
| 170 | end |
| 171 | message |
| 172 | end |
| 173 | |
| 174 | private |
| 175 | def setup_options |
| 176 | @options.banner = "Usage: #{@program_name} [OPTIONS]" |
| 177 | setup_common_options(@options) |
| 178 | @options.on("-h", "--help", "Display this help message.", |
| 179 | :tail => true) do |
| 180 | $stdout.puts(help_message) |
| 181 | end |
| 182 | end |
| 183 | |
| 184 | def setup_common_options(options) |
| 185 | options.string("--log-path", |
| 186 | "Change log path (#{Logger.default_path})", |
| 187 | default: Logger.default_path) |
| 188 | default_log_level = Logger.default_level |
| 189 | options.string("--log-level", |
| 190 | "Change log level (#{default_log_level.name})", |
| 191 | default: default_log_level) |
| 192 | default_log_flags = Logger.default_flags |
| 193 | options.string("--log-flags", |
| 194 | "Change log flags (#{default_log_flags.to_s})", |
| 195 | default: default_log_flags) |
| 196 | end |
| 197 | |
| 198 | def find_command(name) |
| 199 | @commands.find do |command| |
| 200 | command.name == name |
| 201 | end |
| 202 | end |
| 203 | |
| 204 | def apply_actions(result) |
| 205 | @action_runner.run(result) unless result.help? |
| 206 | end |
| 207 | |
| 208 | class ActionRunner |
| 209 | def initialize |
| 210 | @actions = [] |
| 211 | end |
| 212 | |
| 213 | def add(&action) |
| 214 | @actions << action |
| 215 | end |
| 216 | |
| 217 | def run(*args) |
| 218 | @actions.each do |action| |
| 219 | action.call(*args) |
| 220 | end |
| 221 | end |
| 222 | end |
| 223 | |
| 224 | class Command |
| 225 | attr_reader :name |
| 226 | attr_reader :options |
| 227 | attr_accessor :description |
| 228 | def initialize(program_name, name) |
| 229 | @program_name = program_name |
| 230 | @name = name |
| 231 | @options = Slop::Options.new |
| 232 | setup_options |
| 233 | @description = nil |
| 234 | @action_runner = ActionRunner.new |
| 235 | end |
| 236 | |
| 237 | def add_action(&action) |
| 238 | @action_runner.add(&action) |
| 239 | end |
| 240 | |
| 241 | def parse(command_line) |
| 242 | result = @options.parse(command_line) |
| 243 | @action_runner.run(result) unless result.help? |
| 244 | result |
| 245 | end |
| 246 | |
| 247 | def help_message |
| 248 | message = "" |
| 249 | message << "Description: #{@description}\n" if @description |
| 250 | message << @options.to_s |
| 251 | message |
| 252 | end |
| 253 | |
| 254 | private |
| 255 | def setup_options |
| 256 | @options.banner = "Usage: #{@program_name} #{@name} [OPTIONS]" |
| 257 | @options.on("-h", "--help", "Display this help message.", |
| 258 | :tail => true) do |
| 259 | $stdout.puts(help_message) |
| 260 | end |
| 261 | end |
| 262 | end |
| 263 | end |
| 264 | end |