hg -> git
Sho Hashimoto [Sat, 26 Jun 2010 04:37:24 +0000 (13:37 +0900)]
17 files changed:
.gitignore [new file with mode: 0644]
History.txt [new file with mode: 0644]
Manifest.txt [new file with mode: 0644]
README.rdoc [new file with mode: 0644]
Rakefile [new file with mode: 0644]
examples/example.rb [new file with mode: 0644]
examples/math.rb [new file with mode: 0644]
examples/sum.rb [new file with mode: 0644]
lib/ArgsParser.rb [new file with mode: 0644]
lib/ArgsParser/Parser.rb [new file with mode: 0644]
script/console [new file with mode: 0755]
script/destroy [new file with mode: 0755]
script/generate [new file with mode: 0755]
spec/parser_spec.rb [new file with mode: 0644]
spec/spec.opts [new file with mode: 0644]
spec/spec_helper.rb [new file with mode: 0644]
tasks/rspec.rake [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..1b36f54
--- /dev/null
@@ -0,0 +1,9 @@
+DS_Store
+*~
+pkg/*
+db*
+db/*
+.fuse*
+config.yaml
+.\#*
+*\#*
diff --git a/History.txt b/History.txt
new file mode 100644 (file)
index 0000000..a7e1a1c
--- /dev/null
@@ -0,0 +1,16 @@
+=== 0.0.8 2010-06-26
+
+* fix my name
+
+=== 0.0.6 2010-03-15
+
+* add examples
+
+=== 0.0.5 2010-03-15
+
+* parse first arg
+
+=== 0.0.1 2010-02-23
+
+* 1 major enhancement:
+  * Initial release
diff --git a/Manifest.txt b/Manifest.txt
new file mode 100644 (file)
index 0000000..bd876ba
--- /dev/null
@@ -0,0 +1,16 @@
+History.txt
+Manifest.txt
+README.rdoc
+Rakefile
+lib/ArgsParser.rb
+lib/ArgsParser/Parser.rb
+examples/example.rb
+examples/math.rb
+examples/sum.rb
+script/console
+script/destroy
+script/generate
+tasks/rspec.rake
+spec/parser_spec.rb
+spec/spec.opts
+spec/spec_helper.rb
diff --git a/README.rdoc b/README.rdoc
new file mode 100644 (file)
index 0000000..7a11793
--- /dev/null
@@ -0,0 +1,61 @@
+= ArgsParser
+
+* http://bitbucket.org/shokai/argsparser-ruby
+
+== DESCRIPTION:
+
+* Parse args from command line.
+
+== FEATURES/PROBLEMS:
+
+
+
+== SYNOPSIS:
+
+parser = ArgsParser.parser
+parser.bind(:help, :h, "show help")
+parser.bind(:size, :s, "size")
+parser.comment(:min, "min size")
+parser.comment(:max, "max size")
+parser.comment(:debug, "debug mode")
+
+params = parser.parse(ARGV)
+
+if parser.has_option(:help) or !parser.has_params([:size, :max, :min])
+   puts parser.help
+   puts 'e.g.  ./example.rb -s 320x240 -min 100 -max 500'
+end
+
+puts params[:size]
+
+p params
+
+
+== INSTALL:
+
+* sudo gem install ArgsParser
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2010 Sho Hashimoto
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
diff --git a/Rakefile b/Rakefile
new file mode 100644 (file)
index 0000000..45a03cf
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,24 @@
+require 'rubygems'\r
+gem 'hoe', '>= 2.1.0'\r
+require 'hoe'\r
+require 'fileutils'\r
+require './lib/ArgsParser'\r
+\r
+Hoe.plugin :newgem\r
+# Hoe.plugin :website\r
+# Hoe.plugin :cucumberfeatures\r
+\r
+# Generate all the Rake tasks\r
+# Run 'rake -T' to see list of generated tasks (from gem root directory)\r
+$hoe = Hoe.spec 'ArgsParser' do\r
+  self.developer 'shokai', 'hashimoto@shokai.org'\r
+  self.rubyforge_name       = self.name # TODO this is default value\r
+end\r
+\r
+require 'newgem/tasks'\r
+Dir['tasks/**/*.rake'].each { |t| load t }\r
+\r
+# TODO - want other tests/tasks run by default? Add them to the list\r
+# remove_task :default\r
+# task :default => [:spec, :features]\r
+\r
diff --git a/examples/example.rb b/examples/example.rb
new file mode 100644 (file)
index 0000000..b3b32f3
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+require '../lib/ArgsParser'
+
+parser = ArgsParser.parser
+parser.bind(:help, :h, "show help")
+parser.bind(:frame, :f, "frame image (required)")
+parser.bind(:message, :m, "message (required)") # name, shortname, comment for help
+parser.bind(:size, :s, "size (required)")
+parser.comment(:min, "minimum size") # add comment for help
+parser.comment(:max, "maximum size")
+parser.comment(:debug, "debug mode")
+
+first, params = parser.parse(ARGV)
+required_params = [:frame, :message, :size]
+
+if parser.has_option(:help) or !parser.has_params(required_params)
+  puts parser.help
+  puts 'required params'
+  (required_params - params.keys).each{|param|
+    puts " -#{param}"
+  }
+  puts 'e.g.  ruby example.rb -f frame.png -m "hello world" -s 320x240 -debug'
+  puts '-'*30
+  exit 1
+end
+
+if first
+  puts 'first arg : ' + first
+end
+
+if parser.has_param(:size)
+  puts 'size : ' + params[:size]
+end
+
+# show all params
+p params
diff --git a/examples/math.rb b/examples/math.rb
new file mode 100644 (file)
index 0000000..47cef06
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+require '../lib/ArgsParser'
+
+parser = ArgsParser.parser
+parser.bind(:help, :h, "show help")
+parser.comment(:a, "a (add,sub,mul,div) b") # add comment for help
+parser.comment(:b, "a (add,sub,mul,div) b")
+
+first, params = parser.parse(ARGV)
+
+required_params = [:a, :b]
+if parser.has_option(:help) or !parser.has_params(required_params)
+  puts parser.help
+  puts 'e.g.  ruby math.rb add -a 5 -b 20'
+  exit 1
+end
+
+# calc
+a = params[:a].to_i
+b = params[:b].to_i
+
+if first == 'sub'
+  method = '-'
+  result = a - b 
+elsif first == 'mul'
+  method = '*'
+  result = a * b
+elsif first == 'div'
+  method = '/'
+  result = a.to_f / b
+else
+  method = '+'
+  result = a + b
+end
+
+puts "#{a} #{method} #{b} = #{result}"
+
+# show all params
+p params
diff --git a/examples/sum.rb b/examples/sum.rb
new file mode 100644 (file)
index 0000000..7b4256e
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+require '../lib/ArgsParser'
+
+parser = ArgsParser.parser
+parser.bind(:help, :h, "show help")
+parser.comment(:a, "a + b") # add comment for help
+parser.comment(:b, "a + b")
+
+parser.parse(ARGV)
+params = parser.params
+required_params = [:a, :b]
+
+if parser.has_option(:help) or !parser.has_params(required_params)
+  puts parser.help
+  puts 'e.g.  ruby sum.rb -a 5 -b 20'
+  exit 1
+end
+
+a = parser.params[:a].to_i
+b = parser.params[:b].to_i
+puts "#{a} + #{b} = #{a+b}"
+
+# show all params
+p parser.params
diff --git a/lib/ArgsParser.rb b/lib/ArgsParser.rb
new file mode 100644 (file)
index 0000000..b39745e
--- /dev/null
@@ -0,0 +1,14 @@
+$:.unshift(File.dirname(__FILE__)) unless
+  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+
+directory = File.expand_path(File.dirname(__FILE__))
+
+require File.join(directory, 'ArgsParser', 'Parser')
+
+module ArgsParser
+  VERSION = '0.0.8'
+
+  def ArgsParser.parser
+    Parser.new
+  end
+end
diff --git a/lib/ArgsParser/Parser.rb b/lib/ArgsParser/Parser.rb
new file mode 100644 (file)
index 0000000..016a9c0
--- /dev/null
@@ -0,0 +1,100 @@
+# -*- coding: utf-8 -*-
+# ARGSのparser
+
+class Parser
+
+  attr_reader :params, :comments, :first
+
+  def initialize
+    @binds = Hash.new
+    @comments = Hash.new
+  end
+
+  def parse(argv)
+    # parse
+    @first = nil
+    if argv.size > 0 and !(argv.first =~ /^-+.+/)
+      @first = argv.shift
+    end
+
+    @params = Hash.new
+    for i in 0...argv.size do
+      if argv[i].match(/^-+.+/)
+        term = argv[i].match(/^-+(.+)/)[1].to_sym
+        if i == argv.size-1 || argv[i+1].match(/^-+.+/)
+          @params[term] = true # option
+        else
+          @params[term] = argv[i+1] # param
+        end
+      end
+    end
+    
+    @binds.keys.each{|name|
+      fullname = @binds[name]
+      if @params[fullname] == nil && @params[name] != nil
+        @params[fullname] = @params[name]
+        @params.delete(name)
+      end
+    }
+    
+    return @first, @params
+  end
+
+  def bind(fullname, name, comment=nil)
+    @binds[name.to_sym] = fullname.to_sym
+    if comment != nil
+      comment(fullname, comment)
+    end
+  end
+
+  def has_param(name)
+    @params[name].class == String
+  end
+
+  def has_params(params_arr)
+    params_arr.each{|name|
+      return false if !has_param(name)
+    }
+    return true
+  end
+
+  def has_option(name)
+    @params[name] == true
+  end
+
+  def has_options(options_arr)
+    options_arr.each{|name|
+      return false if !has_option(name)
+    }
+    return true
+  end
+
+  def comment(name, comment)
+    @comments[name.to_sym] = comment.to_s
+  end
+  
+  def help
+    binds_reversed = Hash.new
+    most_longname_size = 0
+    @binds.keys.each{|key|
+      value = @binds[key]
+      binds_reversed[value] = key
+      if most_longname_size < len = key.to_s.size + value.to_s.size
+        most_longname_size = len
+      end
+    }
+
+    s = "options:\n"
+    @comments.keys.sort{|a,b| a.to_s <=> b.to_s }.each{|name|
+      comment = @comments[name]
+      if shortname = binds_reversed[name]
+        s += " -#{name} (-#{shortname})" +
+          " "*(most_longname_size-name.to_s.size-shortname.to_s.size+2) + "#{comment}\n"
+      else
+        s += " -#{name}" + " "*(most_longname_size-name.to_s.size+6) + "#{comment}\n"
+      end
+    }
+    return s
+  end
+
+end
diff --git a/script/console b/script/console
new file mode 100755 (executable)
index 0000000..4768dfe
--- /dev/null
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# File: script/console
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+
+libs =  " -r irb/completion"
+# Perhaps use a console_lib to store any extra methods I may want available in the cosole
+# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
+libs <<  " -r #{File.dirname(__FILE__) + '/../lib/ArgsParser.rb'}"
+puts "Loading ArgsParser gem"
+exec "#{irb} #{libs} --simple-prompt"
\ No newline at end of file
diff --git a/script/destroy b/script/destroy
new file mode 100755 (executable)
index 0000000..e48464d
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+  require 'rubigen'
+rescue LoadError
+  require 'rubygems'
+  require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
diff --git a/script/generate b/script/generate
new file mode 100755 (executable)
index 0000000..c27f655
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+  require 'rubigen'
+rescue LoadError
+  require 'rubygems'
+  require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb
new file mode 100644 (file)
index 0000000..cb6aa97
--- /dev/null
@@ -0,0 +1,119 @@
+require File.dirname(__FILE__) + '/spec_helper.rb'
+
+describe "test of parsing first-arg" do
+  before do
+    @argv = ['foo']
+    @parser = ArgsParser.parser
+    @parser.parse(@argv)
+  end
+
+  it "has first-arg" do
+    @parser.first.should == 'foo'
+  end
+
+  it "has not params" do
+    @parser.params.size.should == 0
+  end
+end
+
+describe "test of parsing params" do
+  before do
+    @argv = ['-x', '320',
+             '-yscale', '240',
+             '-debug']
+    @parser = ArgsParser.parser
+    @parser.comment(:debug, "debug mode")
+    @parser.bind(:xscale, :x, "width")
+    @parser.bind(:yscale, :y, "height")
+    @first, @params = @parser.parse(@argv)
+  end
+
+  it "has not first-arg" do
+    @first.should == nil
+  end
+
+  it "has params" do
+    @parser.has_params([:xscale, :yscale]).should == true
+    @params[:xscale].to_i.should == 320
+    @params[:yscale].to_i.should == 240
+  end
+
+  it "has an option" do
+    @parser.has_option(:debug).should == true
+  end
+end
+
+describe "test of parsing first-arg and params" do
+  before do
+    @argv = ['foo',
+             '-x', '320',
+             '-yscale', '240',
+             '-m', 'hello world',
+             '-h', 
+             '--output', '/path/to/output/',
+             '-debug']
+    @parser = ArgsParser.parser
+    @parser.bind(:message, :m, "message text")
+    @parser.bind(:output, :o, "path to output directory")
+    @parser.comment(:debug, "debug mode")
+    @parser.bind(:help, :h, "show help")
+    @parser.bind(:xscale, :x, "width")
+    @parser.bind(:yscale, :y, "height")
+    @first, @params = @parser.parse(@argv)
+  end
+
+  it "has the first-arg" do
+    @first.should == 'foo'
+  end
+
+  it "has param" do
+    @parser.has_param(:message).should == true
+    @parser.has_param(:output).should == true
+  end
+
+  it "has not param" do
+    @parser.has_param(:m).should == false
+    @parser.has_param(:o).should == false
+    @parser.has_param(:help).should == false
+    @parser.has_param(:name).should == false
+  end
+
+  it "has option" do
+    @parser.has_option(:debug).should == true
+    @parser.has_option(:help).should == true
+  end
+
+  it "has not option" do
+    @parser.has_option(:h).should == false
+    @parser.has_option(:m).should == false
+    @parser.has_option(:message).should == false
+    @parser.has_option(:output).should == false
+  end
+
+  it "has params" do
+    @parser.has_params([:message, :output]).should == true
+  end
+
+  it "has not params" do
+    @parser.has_params([:message, :output, :help]).should == false
+  end
+
+  it "has options" do
+    @parser.has_options([:help, :debug]).should == true
+  end
+
+  it "has not options" do
+    @parser.has_options([:help, :debug, :h]).should == false
+  end
+
+  it "has String in params" do
+    @params[:output].should == '/path/to/output/'
+  end
+
+  it "has Number in params" do
+    @params[:yscale].to_i.should == 240
+    @params[:xscale].to_i.should == 320
+  end
+
+end
+
diff --git a/spec/spec.opts b/spec/spec.opts
new file mode 100644 (file)
index 0000000..cf6add7
--- /dev/null
@@ -0,0 +1 @@
+--colour
\ No newline at end of file
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644 (file)
index 0000000..df7cd6c
--- /dev/null
@@ -0,0 +1,10 @@
+begin
+  require 'spec'
+rescue LoadError
+  require 'rubygems' unless ENV['NO_RUBYGEMS']
+  gem 'rspec'
+  require 'spec'
+end
+
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'ArgsParser'
diff --git a/tasks/rspec.rake b/tasks/rspec.rake
new file mode 100644 (file)
index 0000000..31a99b0
--- /dev/null
@@ -0,0 +1,21 @@
+begin
+  require 'spec'
+rescue LoadError
+  require 'rubygems' unless ENV['NO_RUBYGEMS']
+  require 'spec'
+end
+begin
+  require 'spec/rake/spectask'
+rescue LoadError
+  puts <<-EOS
+To use rspec for testing you must install rspec gem:
+    gem install rspec
+EOS
+  exit(0)
+end
+
+desc "Run the specs under spec/models"
+Spec::Rake::SpecTask.new do |t|
+  t.spec_opts = ['--options', "spec/spec.opts"]
+  t.spec_files = FileList['spec/**/*_spec.rb']
+end