args_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ === 0.0.1 2012-05-09
2
+
3
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/args_parser.rb
6
+ lib/args_parser/parser.rb
7
+ samples/download_webpage.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ test/test_args_parser.rb
12
+ test/test_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ = args_parser
2
+
3
+ * http://github.com/shokai/args_parser
4
+
5
+ == DESCRIPTION:
6
+
7
+ ARGV parser - Parse args from command line.
8
+
9
+ == SYNOPSIS:
10
+
11
+ % ruby sample.rb -url http://example.com -o out.html
12
+
13
+ parse ARGV
14
+
15
+ require 'rubygems'
16
+ require 'args_parser'
17
+
18
+ parser = ArgsParser.parse ARGV do
19
+ arg :url, 'URL', :alias => :u
20
+ arg :output, 'output file', :alias => :o, :default => 'out.html'
21
+ arg :verbose, 'verbose mode'
22
+ arg :help, 'show help', :alias => :h
23
+ end
24
+
25
+ if parser.has_option? :help or !parser.has_param?(:url, :output)
26
+ STDERR.puts parser.help
27
+ exit 1
28
+ end
29
+
30
+ require 'open-uri'
31
+ puts 'download..' if parser[:verbose]
32
+ open(parser[:output], 'w+') do |f|
33
+ f.write open(parser[:url]).read
34
+ end
35
+ puts "saved! => #{parser[:output]}"
36
+
37
+
38
+ == REQUIREMENTS:
39
+
40
+ * Ruby 1.8.7+
41
+ * Ruby 1.9.3+
42
+
43
+ == INSTALL:
44
+
45
+ * gem install args_parser
46
+
47
+ == LICENSE:
48
+
49
+ (The MIT License)
50
+
51
+ Copyright (c) 2012 Sho Hashimoto
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining
54
+ a copy of this software and associated documentation files (the
55
+ 'Software'), to deal in the Software without restriction, including
56
+ without limitation the rights to use, copy, modify, merge, publish,
57
+ distribute, sublicense, and/or sell copies of the Software, and to
58
+ permit persons to whom the Software is furnished to do so, subject to
59
+ the following conditions:
60
+
61
+ The above copyright notice and this permission notice shall be
62
+ included in all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
65
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
66
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
67
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
68
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
69
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
70
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/args_parser'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'args_parser' do
14
+ self.developer 'Sho Hashimoto', 'hashimoto@shokai.org'
15
+ self.rubyforge_name = self.name # TODO this is default value
16
+ # self.extra_deps = [['newgem','>= 1.5.3']]
17
+
18
+ end
19
+
20
+ require 'newgem/tasks'
21
+ Dir['tasks/**/*.rake'].each { |t| load t }
22
+
23
+ # TODO - want other tests/tasks run by default? Add them to the list
24
+ # remove_task :default
25
+ # task :default => [:spec, :features]
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'args_parser/parser'
5
+
6
+ module ArgsParser
7
+ VERSION = '0.0.1'
8
+ end
@@ -0,0 +1,134 @@
1
+
2
+ module ArgsParser
3
+ def self.parse(argv=[], &block)
4
+ Parser.new(argv, &block)
5
+ end
6
+
7
+ class Parser
8
+ attr_reader :first
9
+
10
+
11
+ private
12
+ def params
13
+ @params ||=
14
+ Hash.new{|h,k|
15
+ h[k] = {
16
+ :default => nil,
17
+ :description => nil,
18
+ :value => nil,
19
+ :alias => nil,
20
+ :index => -1
21
+ }
22
+ }
23
+ end
24
+
25
+ def aliases
26
+ @aliases ||= Hash.new
27
+ end
28
+
29
+ public
30
+ def initialize(argv=[], &block)
31
+ unless block_given?
32
+ raise ArgumentError, 'initialize block was not given'
33
+ end
34
+ instance_eval &block
35
+ parse argv
36
+ end
37
+
38
+ def arg(name, description, opts={})
39
+ params[name][:default] = opts[:default]
40
+ params[name][:description] = description
41
+ params[name][:index] = params.keys.size
42
+ params[name][:alias] = opts[:alias]
43
+ aliases[opts[:alias]] = name if opts[:alias]
44
+ end
45
+
46
+ def args
47
+ params.keys
48
+ end
49
+
50
+ def parse(argv)
51
+ k = nil
52
+ argv.each_with_index do |arg, index|
53
+ unless k
54
+ if arg =~ /^-+[^-\s]+$/
55
+ k = arg.scan(/^-+([^-\s]+)$/)[0][0].strip.to_sym
56
+ k = aliases[k] if aliases[k]
57
+ elsif index == 0
58
+ @first = arg
59
+ end
60
+ else
61
+ if arg =~ /^-+[^-\s]+$/
62
+ params[k][:value] = true
63
+ k = arg.scan(/^-+([^-\s]+)$/)[0][0].strip.to_sym
64
+ k = aliases[k] if aliases[k]
65
+ else
66
+ params[k][:value] = arg
67
+ k = nil
68
+ end
69
+ end
70
+ end
71
+ if k
72
+ params[k][:value] = true
73
+ end
74
+ end
75
+
76
+ def [](key)
77
+ params[key][:value] || params[key][:default]
78
+ end
79
+
80
+ def []=(key, value)
81
+ params[key][:value] = value
82
+ end
83
+
84
+ def has_option?(*opt)
85
+ !(opt.flatten.map{|i|
86
+ self[i] == true
87
+ }.include? false)
88
+ end
89
+
90
+ def has_param?(*param_)
91
+ !(param_.flatten.map{|i|
92
+ v = self[i]
93
+ (v and v.kind_of? String) ? true : false
94
+ }.include? false)
95
+ end
96
+
97
+ def inspect
98
+ h = Hash.new
99
+ params.each do |k,v|
100
+ h[k] = v[:value]
101
+ end
102
+ h.inspect
103
+ end
104
+
105
+ def help
106
+ params_ = Array.new
107
+ params.each do |k,v|
108
+ v[:name] = k
109
+ params_ << v
110
+ end
111
+ params_ = params_.delete_if{|i|
112
+ i[:index] < 0
113
+ }.sort{|a,b|
114
+ a[:index] <=> b[:index]
115
+ }
116
+
117
+ len = params_.map{|i|
118
+ line = " -#{i[:name]}"
119
+ line += " (-#{i[:alias]})" if i[:alias]
120
+ line.size
121
+ }.max
122
+
123
+ "options:\n" + params_.map{|i|
124
+ line = " -#{i[:name]}"
125
+ line += " (-#{i[:alias]})" if i[:alias]
126
+ line = line.ljust(len+2)
127
+ line += i[:description].to_s
128
+ line += " : default - #{i[:default]}" if i[:default]
129
+ line
130
+ }.join("\n")
131
+ end
132
+
133
+ end
134
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'args_parser'
4
+ ## require File.dirname(__FILE__)+'/../lib/args_parser'
5
+
6
+ parser = ArgsParser.parse ARGV do
7
+ arg :url, 'URL', :alias => :u
8
+ arg :output, 'output file', :alias => :o, :default => 'out.html'
9
+ arg :verbose, 'verbose mode'
10
+ arg :help, 'show help', :alias => :h
11
+ end
12
+
13
+ if parser.has_option? :help or !parser.has_param?(:url, :output)
14
+ STDERR.puts "Download WebPage\n=="
15
+ STDERR.puts parser.help
16
+ STDERR.puts "e.g. ruby #{$0} -url http://example.com -o out.html"
17
+ exit 1
18
+ end
19
+
20
+ p parser
21
+
22
+ require 'open-uri'
23
+
24
+ puts 'download..' if parser[:verbose]
25
+ data = open(parser[:url]).read
26
+ puts data if parser[:verbose]
27
+
28
+ open(parser[:output], 'w+') do |f|
29
+ f.write data
30
+ end
31
+ puts "saved! => #{parser[:output]}"
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/args_parser.rb'}"
9
+ puts "Loading args_parser gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestArgsParser < Test::Unit::TestCase
4
+ def setup
5
+ @argv = 'test --input ~/tmp -a --o ./out -h'.split(/\s+/)
6
+ @parser = ArgsParser.parse @argv do
7
+ arg :input, 'input dir', :alias => :i
8
+ arg :output, 'output dir', :alias => :o
9
+ arg :help, 'show help', :alias => :h
10
+ end
11
+ end
12
+
13
+ def test_first
14
+ assert @parser.first == 'test'
15
+ end
16
+
17
+ def test_arg
18
+ assert @parser[:input] == '~/tmp'
19
+ end
20
+
21
+ def test_alias
22
+ assert @parser[:output] == './out'
23
+ end
24
+
25
+ def test_missing_arg
26
+ assert @parser[:a] == true
27
+ end
28
+
29
+ def test_switch
30
+ assert @parser[:help] == true
31
+ end
32
+
33
+ def test_has_param?
34
+ assert (@parser.has_param? :input and @parser.has_param? :output)
35
+ end
36
+
37
+ def test_has_params?
38
+ assert @parser.has_param? :input, :output
39
+ end
40
+
41
+ def test_has_not_param?
42
+ assert !@parser.has_param?(:a)
43
+ end
44
+
45
+ def test_has_option?
46
+ assert (@parser.has_option? :help and @parser.has_option? :a)
47
+ end
48
+
49
+ def test_has_options?
50
+ assert @parser.has_option? :help, :a
51
+ end
52
+
53
+ def test_has_not_option?
54
+ assert !@parser.has_option?(:b)
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/args_parser'
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: args_parser
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sho Hashimoto
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rdoc
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 19
29
+ segments:
30
+ - 3
31
+ - 10
32
+ version: "3.10"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: newgem
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 5
44
+ segments:
45
+ - 1
46
+ - 5
47
+ - 3
48
+ version: 1.5.3
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: hoe
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 29
60
+ segments:
61
+ - 2
62
+ - 15
63
+ version: "2.15"
64
+ type: :development
65
+ version_requirements: *id003
66
+ description: ARGV parser - Parse args from command line.
67
+ email:
68
+ - hashimoto@shokai.org
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - History.txt
75
+ - Manifest.txt
76
+ - README.rdoc
77
+ files:
78
+ - History.txt
79
+ - Manifest.txt
80
+ - README.rdoc
81
+ - Rakefile
82
+ - lib/args_parser.rb
83
+ - lib/args_parser/parser.rb
84
+ - samples/download_webpage.rb
85
+ - script/console
86
+ - script/destroy
87
+ - script/generate
88
+ - test/test_args_parser.rb
89
+ - test/test_helper.rb
90
+ - .gemtest
91
+ homepage: http://github.com/shokai/args_parser
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options:
96
+ - --main
97
+ - README.rdoc
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project: args_parser
121
+ rubygems_version: 1.8.17
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: ARGV parser - Parse args from command line.
125
+ test_files:
126
+ - test/test_args_parser.rb
127
+ - test/test_helper.rb