[Groonga-commit] groonga/groonga at f377584 [master] Use Slop 4

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Jan 25 17:21:11 JST 2016


Kouhei Sutou	2016-01-25 17:21:11 +0900 (Mon, 25 Jan 2016)

  New Revision: f377584b8437b2519fccb9efb4ecdb78433a635b
  https://github.com/groonga/groonga/commit/f377584b8437b2519fccb9efb4ecdb78433a635b

  Message:
    Use Slop 4

  Added files:
    lib/mrb/scripts/command_line_parser.rb
  Modified files:
    lib/mrb/scripts/command_line/grndb.rb
    lib/mrb/scripts/initialize/post.rb
    lib/mrb/scripts/sources.am
    vendor/mruby/build_config.rb

  Modified: lib/mrb/scripts/command_line/grndb.rb (+33 -60)
===================================================================
--- lib/mrb/scripts/command_line/grndb.rb    2016-01-25 15:42:59 +0900 (919cb8a)
+++ lib/mrb/scripts/command_line/grndb.rb    2016-01-25 17:21:11 +0900 (9ae644d)
@@ -2,80 +2,66 @@ module Groonga
   module CommandLine
     class Grndb
       def initialize(argv)
-        @command, *@arguments = argv
+        @program_path, *@arguments = argv
         @succeeded = true
-        @executed = false
         @database_path = nil
       end
 
       def run
-        slop = create_slop
-        rest = nil
+        command_line_parser = create_command_line_parser
+        options = nil
         begin
-          rest = slop.parse(@arguments)
-        rescue Slop::Error
-          $stderr.puts($!.message)
-          return false
-        end
-
-        if slop.help?
-          $stdout.puts(slop.help)
-          return true
-        end
-
-        unless @executed
-          if rest.empty?
-            $stderr.puts("No command is specified.")
-          else
-            $stderr.puts("Unknown command: <#{rest.first}>")
-          end
+          options = command_line_parser.parse(@arguments)
+        rescue Slop::Error => error
+          $stderr.puts(error.message)
+          $stderr.puts
+          $stderr.puts(command_line_parser.help_message)
           return false
         end
-
         @succeeded
       end
 
       private
-      def create_slop
-        slop = Slop.new
-        command_name = File.basename(@command)
-        slop.banner = "Usage: #{command_name} COMMAND [OPTIONS] DB_PATH"
-        slop_enable_help(slop)
+      def create_command_line_parser
+        program_name = File.basename(@program_path)
+        parser = CommandLineParser.new(program_name)
 
-        slop.command "check" do |command|
-          command.description "Check database"
-          slop_enable_help(command)
+        parser.add_command("check") do |command|
+          command.description = "Check database"
 
-          command.on("--target=", "Check only the target object.")
+          options = command.options
+          options.banner += " DB_PATH"
+          options.string("--target", "Check only the target object.")
 
-          command.run do |options, arguments|
-            run_command(options, arguments) do |database, new_arguments|
-              check(database, options, new_arguments)
+          command.add_action do |options|
+            open_database(command, options) do |database, rest_arguments|
+              check(database, options, rest_arguments)
             end
           end
         end
 
-        slop.command "recover" do |command|
-          command.description "Recover database"
-          slop_enable_help(command)
+        parser.add_command("recover") do |command|
+          command.description = "Recover database"
+
+          options = command.options
+          options.banner += " DB_PATH"
 
-          command.run do |options, arguments|
-            run_command(options, arguments) do |database, new_arguments|
-              recover(database, options, new_arguments)
+          command.add_action do |options|
+            open_database(command, options) do |database, rest_arguments|
+              recover(database, options, rest_arguments)
             end
           end
         end
 
-        slop
+        parser
       end
 
-      def slop_enable_help(slop)
-        slop.on("-h", "--help", "Display this help message.", :tail => true)
-      end
-
-      def open_database(arguments)
+      def open_database(command, options)
+        arguments = options.arguments
         if arguments.empty?
           $stderr.puts("Database path is missing")
+          $stderr.puts
+          $stderr.puts(command.help_message)
           @succeesed = false
           return
         end
@@ -98,19 +84,6 @@ module Groonga
         end
       end
 
-      def run_command(options, arguments)
-        @executed = true
-
-        if options.help?
-          $stdout.puts(options.help)
-          return
-        end
-
-        open_database(arguments) do |database|
-          yield(database)
-        end
-      end
-
       def failed(*messages)
         messages.each do |message|
           $stderr.puts(message)
@@ -151,7 +124,7 @@ module Groonga
           message =
             "[#{object.name}] Index column is locked. " +
             "It may be broken. " +
-            "Re-create index by '#{@command} recover #{@database_path}'."
+            "Re-create index by '#{@program_path} recover #{@database_path}'."
           failed(message)
         when Column
           return unless object.locked?

  Added: lib/mrb/scripts/command_line_parser.rb (+156 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/command_line_parser.rb    2016-01-25 17:21:11 +0900 (0e35ed5)
@@ -0,0 +1,156 @@
+module Slop
+  class MissingCommand < Error
+  end
+
+  class UnknownCommand < Error
+    attr_reader :name
+
+    def initialize(msg, name)
+      super(msg)
+      @name = name
+    end
+  end
+end
+
+module Groonga
+  class CommandLineParser
+    def initialize(program_name=nil)
+      $0 ||= nil
+      @program_name = program_name || $0
+      @commands = []
+      @action_runner = ActionRunner.new
+      @options = Slop::Options.new
+      setup_options
+    end
+
+    def options
+      yield(@options) if block_given?
+      @options
+    end
+
+    def add_command(name)
+      command = Command.new(@program_name, name)
+      yield(command)
+      @commands << command
+    end
+
+    def add_action(&action)
+      @action_runner.add(&action)
+    end
+
+    def parse(command_line)
+      if****@comma*****?
+        result =****@optio*****(command_line)
+        apply_actions(result)
+        return result
+      end
+
+      if command_line.empty?
+        message = "Command is missing"
+        raise Slop::MissingCommand.new(message)
+      end
+
+      first_argument = command_line.first
+      if first_argument.start_with?("-")
+        result =****@optio*****(command_line)
+        apply_actions(result)
+        return result
+      end
+
+      command_name = first_argument
+      command = find_command(command_name)
+      if command.nil?
+        message = "Unknown command: <#{command_name}>"
+        raise Slop::UnknownCommand.new(message, command_name)
+      end
+
+      command.parse(command_line[1..-1])
+    end
+
+    def help_message
+      message =****@optio*****_s
+      @commands.each do |command|
+        message << "\n"
+        indent = " " * 4
+        message << "#{command.description}:\n" if command.description
+        message << "#{indent}#{command.options.banner}\n"
+      end
+      message
+    end
+
+    private
+    def setup_options
+      @options.banner = "Usage: #{@program_name} [OPTIONS]"
+      @options.on("-h", "--help", "Display this help message.",
+                  :tail => true) do
+        $stdout.puts(help_message)
+      end
+    end
+
+    def find_command(name)
+      @commands.find do |command|
+        command.name == name
+      end
+    end
+
+    def apply_actions(result)
+      @action_runner.run(result) unless result.help?
+    end
+
+    class ActionRunner
+      def initialize
+        @actions = []
+      end
+
+      def add(&action)
+        @actions << action
+      end
+
+      def run(*args)
+        @actions.each do |action|
+          action.call(*args)
+        end
+      end
+    end
+
+    class Command
+      attr_reader :name
+      attr_reader :options
+      attr_accessor :description
+      def initialize(program_name, name)
+        @program_name = program_name
+        @name = name
+        @options = Slop::Options.new
+        setup_options
+        @description = nil
+        @action_runner = ActionRunner.new
+      end
+
+      def add_action(&action)
+        @action_runner.add(&action)
+      end
+
+      def parse(command_line)
+        result =****@optio*****(command_line)
+        @action_runner.run(result) unless result.help?
+        result
+      end
+
+      def help_message
+        message = ""
+        message << "Description: #{@description}\n" if @description
+        message << @options.to_s
+        message
+      end
+
+      private
+      def setup_options
+        @options.banner = "Usage: #{@program_name} #{@name} [OPTIONS]"
+        @options.on("-h", "--help", "Display this help message.",
+                    :tail => true) do
+          $stdout.puts(help_message)
+        end
+      end
+    end
+  end
+end

  Modified: lib/mrb/scripts/initialize/post.rb (+2 -0)
===================================================================
--- lib/mrb/scripts/initialize/post.rb    2016-01-25 15:42:59 +0900 (2ccda4d)
+++ lib/mrb/scripts/initialize/post.rb    2016-01-25 17:21:11 +0900 (25ee828)
@@ -20,3 +20,5 @@ require "expression"
 require "plugin_loader"
 
 require "eval_context"
+
+require "command_line_parser"

  Modified: lib/mrb/scripts/sources.am (+1 -0)
===================================================================
--- lib/mrb/scripts/sources.am    2016-01-25 15:42:59 +0900 (4fd38a2)
+++ lib/mrb/scripts/sources.am    2016-01-25 17:21:11 +0900 (5631058)
@@ -1,6 +1,7 @@
 RUBY_SCRIPT_FILES =				\
 	backtrace_entry.rb			\
 	command.rb				\
+	command_line_parser.rb			\
 	context.rb				\
 	database.rb				\
 	error.rb				\

  Modified: vendor/mruby/build_config.rb (+1 -1)
===================================================================
--- vendor/mruby/build_config.rb    2016-01-25 15:42:59 +0900 (5f6e19c)
+++ vendor/mruby/build_config.rb    2016-01-25 17:21:11 +0900 (f487e08)
@@ -39,5 +39,5 @@ MRuby::Build.new do |conf|
   conf.gem :github => "iij/mruby-env"
   conf.gem :github => "iij/mruby-io"
   conf.gem :github => "kou/mruby-pp"
-  conf.gem :github => "kou/mruby-slop", :branch => "3.6.1"
+  conf.gem :github => "kou/mruby-slop"
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index