Kouhei Sutou
null+****@clear*****
Fri Jan 6 16:24:06 JST 2017
Kouhei Sutou 2017-01-06 16:24:06 +0900 (Fri, 06 Jan 2017) New Revision: 96db83f96138aeadc40f7115622173870edf89fb https://github.com/groonga/groonga-command/commit/96db83f96138aeadc40f7115622173870edf89fb Message: Support pretty print in command format Modified files: lib/groonga/command/base.rb lib/groonga/command/format/command.rb test/command/test-base.rb Modified: lib/groonga/command/base.rb (+4 -3) =================================================================== --- lib/groonga/command/base.rb 2016-12-08 21:49:55 +0900 (3dac2cf) +++ lib/groonga/command/base.rb 2017-01-06 16:24:06 +0900 (c00a76a) @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2016 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2017 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -148,8 +148,9 @@ module Groonga Format::URI.new(@path_prefix, @command_name, normalized_arguments).path end - def to_command_format - Format::Command.new(@command_name, normalized_arguments).command_line + def to_command_format(options={}) + format = Format::Command.new(@command_name, normalized_arguments) + format.command_line(options) end private Modified: lib/groonga/command/format/command.rb (+9 -7) =================================================================== --- lib/groonga/command/format/command.rb 2016-12-08 21:49:55 +0900 (d19eb2b) +++ lib/groonga/command/format/command.rb 2017-01-06 16:24:06 +0900 (89858f1) @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2017 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -42,16 +40,20 @@ module Groonga @arguments = arguments end - def command_line + def command_line(options={}) + pretty_print = options[:pretty_print] components = [@name] sorted_arguments =****@argum*****_by do |name, _| name.to_s end sorted_arguments.each do |name, value| - components << "--#{name}" - components << self.class.escape_value(value) + components << "--#{name} #{self.class.escape_value(value)}" + end + if pretty_print + components.join(" \\\n ") + else + components.join(" ") end - components.join(" ") end end end Modified: test/command/test-base.rb (+15 -3) =================================================================== --- test/command/test-base.rb 2016-12-08 21:49:55 +0900 (9fb9801) +++ test/command/test-base.rb 2017-01-06 16:24:06 +0900 (8df4fe1) @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2011-2014 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2011-2017 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -108,6 +106,20 @@ class BaseCommandTest < Test::Unit::TestCase assert_equal("select --output_type \"json\" --table \"Users\"", select.to_command_format) end + + def test_pretty_print + select = Groonga::Command::Base.new("select", + :table => "Users", + :filter => "_key == 29", + :output_type => "json") + command = select.to_command_format(:pretty_print => true) + assert_equal(<<-COMMAND.chomp, command) +select \\ + --filter "_key == 29" \\ + --output_type "json" \\ + --table "Users" + COMMAND + end end sub_test_case("#[]") do -------------- next part -------------- HTML����������������������������...Download