[Groonga-commit] droonga/droonga-client-ruby at b5dc73e [master] Add droonga-groonga command as a shorthand

Back to archive index

Kouhei Sutou kou****@clear*****
Wed Apr 15 17:02:35 JST 2015


コマンドの実装なんですが、基本的に

> +      def run
...
> +        exit(true)
> +      end

というようにメソッドの中ではexitを呼ばずにtrue/falseだけを返
して

> +Droonga::Command::Groonga.new.run

呼び出し側で

exit(Droonga::Command::Groonga.new.run)

とする方がよいです。

これはあまり当てはまらないんですが、テストをするときにexitさ
れるとテストしずらいとか、exitするライブラリーは行儀が悪い
(終了するかどうかはライブラリーではなくアプリケーションが判
断するもの)という理由からです。


In <b5dc73edd6094a04699f9d0f6778154e4ac028b9 �� jenkins.clear-code.com>
  "[Groonga-commit] droonga/droonga-client-ruby �� b5dc73e [master] Add droonga-groonga command as a shorthand" on Wed, 15 Apr 2015 16:19:58 +0900,
  YUKI Hiroshi <null+groonga �� clear-code.com> wrote:

> YUKI Hiroshi	2015-04-15 16:19:58 +0900 (Wed, 15 Apr 2015)
> 
>   New Revision: b5dc73edd6094a04699f9d0f6778154e4ac028b9
>   https://github.com/droonga/droonga-client-ruby/commit/b5dc73edd6094a04699f9d0f6778154e4ac028b9
> 
>   Message:
>     Add droonga-groonga command as a shorthand
> 
>   Added files:
>     bin/droonga-groonga
> 
>   Added: bin/droonga-groonga (+78 -0) 100755
> ===================================================================
> --- /dev/null
> +++ bin/droonga-groonga    2015-04-15 16:19:58 +0900 (64dc46f)
> @@ -0,0 +1,78 @@
> +#!/usr/bin/env ruby
> +#
> +# Copyright (C) 2015 Droonga Project
> +#
> +# This library is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU Lesser General Public
> +# License version 2.1 as published by the Free Software Foundation.
> +#
> +# This library is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with this library; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
> +
> +require "json"
> +
> +require "droonga/command/base"
> +
> +module Droonga
> +  module Command
> +    class Groonga < Base
> +      def run
> +        parse_options do |option|
> +          option.banner("Usage: droonga-groonga [groonga-command-name] [options]")
> +          option.banner("   ex: droonga-groonga select --table=Users")
> +
> +          option.separator("Formatting:")
> +          option.on(:pretty,
> +                    "Output result as a pretty print JSON.",
> +                    :default => false)
> +        end
> +
> +        groonga_command_name = ARGV.shift
> +        groonga_message = {
> +          "dataset" => @options[:dataset],
> +          "type" => groonga_command_name,
> +          "body" => build_params(ARGV),
> +        }
> +
> +        response = client.request(groonga_message)
> +        body = response["body"]
> +
> +        if @options[:pretty]
> +          puts(JSON.pretty_generate(body))
> +        else
> +          puts(JSON.generate(body))
> +        end
> +
> +        exit(true)
> +      end
> +
> +      private
> +      def build_params(argv)
> +        params = {}
> +        option_name = nil
> +        argv.each do |arg|
> +          case arg
> +          when /\A--([^\s=]+)=(.+)\z/
> +            params[$1] = $2
> +          when /\A--([^\s=]+)\z/
> +            option_name = $1
> +          else
> +            if option_name
> +              params[option_name] = arg
> +              option_name = nil
> +            end
> +          end
> +        end
> +        params
> +      end
> +    end
> +  end
> +end
> +
> +Droonga::Command::Groonga.new.run




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