summary refs log tree commit
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2011-07-04 22:56:06 +0200
committerFlorian Frank <flori@ping.de>2011-07-04 22:56:06 +0200
commit7159be5a7cea4c2c070e221f0145b97bc5f92a55 (patch)
tree6cf4c578ee4bd948970831c80b321c485d538c27
parentbba04b64715a0c6ca0d281e89f4c56a309e96ecd (diff)
downloadruby-json-7159be5a7cea4c2c070e221f0145b97bc5f92a55.tar.gz
Appy patch by Laurent Arnoud <laurent@spkdev.net>
Use Spruz::GO.go for options parsing
-rw-r--r--Rakefile1
-rwxr-xr-xbin/prettify_json.rb50
-rw-r--r--json_pure.gemspec3
3 files changed, 14 insertions, 40 deletions
diff --git a/Rakefile b/Rakefile
index 29f2f3c..133119b 100644
--- a/Rakefile
+++ b/Rakefile
@@ -91,6 +91,7 @@ if defined?(Gem) and defined?(Gem::PackageTask)
     s.add_development_dependency 'bullshit'
     s.add_development_dependency 'sdoc'
     s.add_development_dependency 'rake', '~>0.9.2'
+    s.add_dependency 'spruz', '~>0.2.8'
 
     s.bindir = "bin"
     s.executables = [ "edit_json.rb", "prettify_json.rb" ]
diff --git a/bin/prettify_json.rb b/bin/prettify_json.rb
index 943546f..3c53183 100755
--- a/bin/prettify_json.rb
+++ b/bin/prettify_json.rb
@@ -3,37 +3,8 @@
 require 'json'
 require 'fileutils'
 include FileUtils
-
-# Parses the argument array _args_, according to the pattern _s_, to
-# retrieve the single character command line options from it. If _s_ is
-# 'xy:' an option '-x' without an option argument is searched, and an
-# option '-y foo' with an option argument ('foo').
-#
-# An option hash is returned with all found options set to true or the
-# found option argument.
-def go(s, args = ARGV)
-  b, v = s.scan(/(.)(:?)/).inject([{},{}]) { |t,(o,a)|
-    t[a.empty? ? 0 : 1][o] = a.empty? ? false : nil
-    t
-  }
-  while a = args.shift
-    a !~ /\A-(.+)/ and args.unshift a and break
-    p = $1
-    until p == ''
-      o = p.slice!(0, 1)
-      if v.key?(o)
-        v[o] = if p == '' then args.shift or break 1 else p end
-        break
-      elsif b.key?(o)
-        b[o] = true
-      else
-        args.unshift a
-        break 1
-      end
-    end and break
-  end
-  b.merge(v)
-end
+require 'spruz/go'
+include Spruz::GO
 
 opts = go 'slhi:', args = ARGV.dup
 if opts['h'] || opts['l'] && opts['s']
@@ -52,17 +23,16 @@ EOT
   exit 0
 end
 
-filename = nil
-str = nil
-json_opts = {:max_nesting => false, :create_additions => false}
+json_opts = { :max_nesting => false, :create_additions => false }
 
-if args.empty?
-  str = STDIN.read
-else
-  str = File.read(filename = args.first)
-end
+document =
+  if filename = args.first or filename == '-'
+    File.read(filename)
+  else
+    STDIN.read
+  end
 
-json = JSON[str, json_opts]
+json = JSON.parse document, json_opts
 
 output = if opts['s']
   JSON.fast_generate json, json_opts
diff --git a/json_pure.gemspec b/json_pure.gemspec
index 99eb61d..bb117cf 100644
--- a/json_pure.gemspec
+++ b/json_pure.gemspec
@@ -28,16 +28,19 @@ Gem::Specification.new do |s|
       s.add_development_dependency(%q<bullshit>, [">= 0"])
       s.add_development_dependency(%q<sdoc>, [">= 0"])
       s.add_development_dependency(%q<rake>, ["~> 0.9.2"])
+      s.add_runtime_dependency(%q<spruz>, ["~> 0.2.8"])
     else
       s.add_dependency(%q<permutation>, [">= 0"])
       s.add_dependency(%q<bullshit>, [">= 0"])
       s.add_dependency(%q<sdoc>, [">= 0"])
       s.add_dependency(%q<rake>, ["~> 0.9.2"])
+      s.add_dependency(%q<spruz>, ["~> 0.2.8"])
     end
   else
     s.add_dependency(%q<permutation>, [">= 0"])
     s.add_dependency(%q<bullshit>, [">= 0"])
     s.add_dependency(%q<sdoc>, [">= 0"])
     s.add_dependency(%q<rake>, ["~> 0.9.2"])
+    s.add_dependency(%q<spruz>, ["~> 0.2.8"])
   end
 end