args_parser 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd18f8f99cc8aab89d1ae2e1f03caa13c9f7acda
4
- data.tar.gz: 36ad2bd87477a51ad8741c8c5eb78bdc7dac9201
3
+ metadata.gz: 8706a4d57149332f2b0483eec8d6c7c90c19f962
4
+ data.tar.gz: fb8a7fbaf3e694abc577082eaafeaa4f16662f04
5
5
  SHA512:
6
- metadata.gz: e105284a49feb4fbc5ade8f6f1eaa8c6355319526c6ffb0341ded36e460e808996d937d4c4fa42bf22cfbacfee3c4ff5b15b8df32c8a342b285b92ffde3f7009
7
- data.tar.gz: 8d198872228f2e349e830798916aa569b79f66ccb48f810e67112dabda4a0882bd863f8d771870acd0392f7b656cb9db1a58d4dd5f282d56071352a46dc6cdd5
6
+ metadata.gz: fd315c4245616b89b3f8494f6d4f35a8869ad3308a9c94a3b4e4411d39627c319aad8c7bce9e8828b418fdb27e4c7491090792486e90266ee524c1013016b866
7
+ data.tar.gz: f379f7430584f20152ba143072be5b485f1e3dd61c08f87e133a75874f6a142401c68e60f3afb9c0351e4bc8f0354e6d89c0452c7603831e1b7c3d7300895a3d
@@ -1,3 +1,10 @@
1
+ === 0.2.0 2013-08-26
2
+
3
+ * add "on" event
4
+ * fix equal_style sample
5
+ * refactoring
6
+ * specify license in gemspec
7
+
1
8
  === 0.1.6 2013-06-08
2
9
 
3
10
  * fix gem description
data/README.md CHANGED
@@ -7,9 +7,7 @@ args_parser
7
7
 
8
8
  Requirements
9
9
  ------------
10
- * Ruby 1.8.7+
11
- * Ruby 1.9.3+
12
- * JRuby 1.6.7+
10
+ - Ruby 1.8.7 ~ 2.0.0
13
11
 
14
12
 
15
13
  Install
@@ -57,9 +55,10 @@ end
57
55
  puts "saved! => #{args[:output]}"
58
56
  ```
59
57
 
60
- equal style
58
+ ### equal style
61
59
 
62
- % ruby samples/twitter_timeline.rb --user=shokai --fav --rt
60
+ % ruby samples/equal_style.rb --help
61
+ % ruby samples/equal_style.rb --user=shokai hello world --a=1234
63
62
 
64
63
  parse equal style ARGV
65
64
  ```ruby
@@ -74,7 +73,7 @@ Test
74
73
 
75
74
  % gem install bundler
76
75
  % bundle install
77
- % rake test
76
+ % bundle exec rake test
78
77
 
79
78
 
80
79
  Contributing
@@ -10,6 +10,7 @@ Gem::Specification.new do |gem|
10
10
  gem.description = %q{Parse/Filter/Validate ARGV from command line with DSL.}
11
11
  gem.summary = gem.description
12
12
  gem.homepage = "http://shokai.github.com/args_parser"
13
+ gem.license = "MIT"
13
14
 
14
15
  gem.files = `git ls-files`.split($/).reject{|i| i=="Gemfile.lock" }
15
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -1,6 +1,4 @@
1
- $:.unshift(File.dirname(__FILE__)) unless
2
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
-
1
+ require 'args_parser/main'
4
2
  require 'args_parser/parser'
5
3
  require 'args_parser/styles/default'
6
4
  require 'args_parser/styles/equal'
@@ -1,9 +1,9 @@
1
1
  module ArgsParser
2
2
  class Config
3
- def self.default
4
- {
5
- :style => :default
6
- }
7
- end
3
+
4
+ DEFAULT = {
5
+ :style => :default
6
+ }
7
+
8
8
  end
9
9
  end
@@ -1,8 +1,10 @@
1
-
2
1
  module ArgsParser
3
2
  class Filter
4
- def filters
5
- @filters ||= []
3
+
4
+ attr_reader :filters
5
+
6
+ def initialize
7
+ @filters = []
6
8
  end
7
9
 
8
10
  def add(name, filter)
@@ -0,0 +1,12 @@
1
+ module ArgsParser
2
+
3
+ def self.parse(argv=[], config={}, &block)
4
+ Config::DEFAULT.each do |k,v|
5
+ config[k] = v unless config[k]
6
+ end
7
+ parser = Parser.new(config, &block)
8
+ parser.parse argv
9
+ parser
10
+ end
11
+
12
+ end
@@ -1,13 +1,4 @@
1
-
2
1
  module ArgsParser
3
- def self.parse(argv=[], config={}, &block)
4
- Config.default.each do |k,v|
5
- config[k] = v unless config[k]
6
- end
7
- parser = Parser.new(config, &block)
8
- parser.parse argv
9
- parser
10
- end
11
2
 
12
3
  class Parser
13
4
  attr_reader :argv, :params, :aliases
@@ -18,9 +9,8 @@ module ArgsParser
18
9
  end
19
10
 
20
11
  def initialize(config, &block)
21
- unless block_given?
22
- raise ArgumentError, 'initialize block was not given'
23
- end
12
+ raise ArgumentError, 'initialize block was not given' unless block_given?
13
+
24
14
  @config = config
25
15
  @argv = []
26
16
  @params = Hash.new{|h,k|
@@ -35,6 +25,7 @@ module ArgsParser
35
25
  @aliases = {}
36
26
  @filter = Filter.new
37
27
  @validator = Validator.new
28
+ @ons = []
38
29
 
39
30
  filter do |v|
40
31
  (v.kind_of? String and v =~ /^-?\d+$/) ? v.to_i : v
@@ -50,7 +41,7 @@ module ArgsParser
50
41
  exit 1
51
42
  end
52
43
 
53
- instance_eval(&block)
44
+ instance_eval &block
54
45
  end
55
46
 
56
47
  private
@@ -64,36 +55,43 @@ module ArgsParser
64
55
  end
65
56
 
66
57
  def filter(name=nil, &block)
67
- if block_given?
68
- @filter.add name, block
69
- end
58
+ @filter.add name, block if block_given?
70
59
  end
71
60
 
72
61
  def on_filter_error(err=nil, name=nil, value=nil, &block)
73
62
  if block_given?
74
63
  @on_filter_error = block
75
- else
76
- @on_filter_error.call(err, name, value) if @on_filter_error
64
+ elsif @on_filter_error.kind_of? Proc
65
+ @on_filter_error.call(err, name, value)
77
66
  end
78
67
  end
79
68
 
80
69
  def validate(name, message, &block)
81
- if block_given?
82
- @validator.add name, message, block
83
- end
70
+ @validator.add name, message, block if block_given?
84
71
  end
85
72
 
86
73
  def on_validate_error(err=nil, name=nil, value=nil, &block)
87
74
  if block_given?
88
75
  @on_validate_error = block
76
+ elsif @on_validate_error.kind_of? Proc
77
+ @on_validate_error.call(err, name, value)
78
+ end
79
+ end
80
+
81
+ def on(name, &block)
82
+ name = name.to_sym
83
+ if block_given?
84
+ @ons.push :name => name, :callback => block
89
85
  else
90
- @on_validate_error.call(err, name, value) if @on_validate_error
86
+ @ons.each do |event|
87
+ event[:callback].call(self[name]) if event[:name] == name
88
+ end
91
89
  end
92
90
  end
93
91
 
94
92
  def default(key)
95
93
  d = params[key.to_sym][:default]
96
- (d and d.kind_of? Proc) ? d.call : d
94
+ d.kind_of?(Proc) ? d.call : d
97
95
  end
98
96
 
99
97
  public
@@ -102,7 +100,7 @@ module ArgsParser
102
100
  end
103
101
 
104
102
  def parse(argv)
105
- method("parse_style_#{@config[:style]}".to_sym).call(argv)
103
+ send "parse_style_#{@config[:style]}", argv
106
104
  params.each do |name, param|
107
105
  next if [nil, true].include? param[:value]
108
106
  begin
@@ -119,27 +117,25 @@ module ArgsParser
119
117
  on_validate_error ValidationError.new(msg), name, param[:value]
120
118
  end
121
119
  end
120
+ params.keys.each do |name|
121
+ on name if has_option? name or has_param? name
122
+ end
122
123
  end
123
124
 
124
125
  def [](key)
125
- params[key.to_sym][:value] or default(key)
126
+ params[key.to_sym][:value] || default(key)
126
127
  end
127
128
 
128
129
  def []=(key, value)
129
130
  params[key.to_sym][:value] = value
130
131
  end
131
132
 
132
- def has_option?(*opt)
133
- !(opt.flatten.map{|i|
134
- self[i] == true
135
- }.include? false)
133
+ def has_option?(*opts)
134
+ !opts.flatten.find{|i| self[i] != true}
136
135
  end
137
136
 
138
- def has_param?(*param_)
139
- !(param_.flatten.map{|i|
140
- v = self[i]
141
- (v != nil and v != true) ? true : false
142
- }.include? false)
137
+ def has_param?(*params)
138
+ !params.flatten.find{|i| self[i] == nil or self[i] == true }
143
139
  end
144
140
 
145
141
  def inspect
@@ -154,24 +150,20 @@ module ArgsParser
154
150
  params_ = Array.new
155
151
  params.each do |k,v|
156
152
  v[:name] = k
157
- params_ << v
153
+ params_.push v
158
154
  end
159
- params_ = params_.delete_if{|i|
160
- i[:index] < 0
161
- }.sort{|a,b|
162
- a[:index] <=> b[:index]
163
- }
155
+ params_ = params_.reject{|i| i[:index] < 0 }.sort{|a,b| a[:index] <=> b[:index] }
164
156
 
165
157
  len = params_.map{|i|
166
- line = " -#{i[:name]}"
167
- line += " (-#{i[:alias]})" if i[:alias]
168
- line.size
158
+ (i[:alias] ?
159
+ " -#{i[:name]} (-#{i[:alias]})" :
160
+ " -#{i[:name]}").size
169
161
  }.max
170
162
 
171
163
  "options:\n" + params_.map{|i|
172
- line = " -#{i[:name]}"
173
- line += " (-#{i[:alias]})" if i[:alias]
174
- line = line.ljust(len+2)
164
+ line = (i[:alias] ?
165
+ " -#{i[:name]} (-#{i[:alias]})" :
166
+ " -#{i[:name]}").ljust(len+2)
175
167
  line += i[:description].to_s
176
168
  line += " : default - #{default i[:name]}" if i[:default]
177
169
  line
@@ -11,7 +11,7 @@ module ArgsParser
11
11
  k = arg.scan(is_key)[0][0].strip.to_sym
12
12
  k = aliases[k] if aliases[k]
13
13
  else
14
- self.argv.push arg
14
+ @argv.push arg
15
15
  end
16
16
  else
17
17
  if arg =~ is_key
@@ -12,11 +12,11 @@ module ArgsParser
12
12
  is_param = /^-+([^-=\s]+)=(.+)$/
13
13
  argv.each_with_index do |arg, i|
14
14
  if arg =~ is_option
15
- k,v = [arg.scan(is_option)[0][0], true]
15
+ k,v = arg.scan(is_option)[0][0], true
16
16
  elsif arg =~ is_param
17
17
  k,v = arg.scan(is_param)[0]
18
18
  else
19
- self.argv.push arg
19
+ @argv.push arg
20
20
  end
21
21
  if k and v
22
22
  k = k.strip.to_sym
@@ -1,8 +1,10 @@
1
-
2
1
  module ArgsParser
3
2
  class Validator
4
- def validators
5
- @validators ||= []
3
+
4
+ attr_reader :validators
5
+
6
+ def initialize
7
+ @validators = []
6
8
  end
7
9
 
8
10
  def add(name, message, validator)
@@ -1,3 +1,3 @@
1
1
  module ArgsParser
2
- VERSION = '0.1.6'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,18 @@
1
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
2
+
3
+ require 'args_parser'
4
+
5
+ args = ArgsParser.parse ARGV, :style => :equal do
6
+ arg :help, 'show help', :alias => :h
7
+
8
+ on :help do |v|
9
+ STDERR.puts help
10
+ STDERR.puts "e.g. ruby equal_style.rb --user=shokai hello world --a=1234"
11
+ exit 1
12
+ end
13
+ end
14
+
15
+ puts "== args"
16
+ p args
17
+ puts "== args.argv"
18
+ p args.argv
@@ -20,45 +20,45 @@ class TestArgsParser < MiniTest::Test
20
20
  end
21
21
 
22
22
  def test_first
23
- assert @parser.first == 'test'
23
+ assert_equal @parser.first, 'test'
24
24
  end
25
25
 
26
26
  def test_argv
27
- assert @parser.argv == ['test', 'foo', 'bar']
27
+ assert_equal @parser.argv, ['test', 'foo', 'bar']
28
28
  end
29
29
 
30
30
  def test_arg
31
- assert @parser[:input] == 'http://shokai.org'
31
+ assert_equal @parser[:input], 'http://shokai.org'
32
32
  end
33
33
 
34
34
  def test_alias
35
- assert @parser[:output] == './out'
35
+ assert_equal @parser[:output], './out'
36
36
  end
37
37
 
38
38
  def test_string_access
39
- assert @parser['output'] == './out'
39
+ assert_equal @parser['output'], './out'
40
40
  end
41
41
 
42
42
  def test_cast_integer
43
- assert @parser[:depth] == 30
44
- assert @parser[:depth].class == Fixnum
43
+ assert_equal @parser[:depth], 30
44
+ assert_equal @parser[:depth].class, Fixnum
45
45
  end
46
46
 
47
47
  def test_cast_float
48
- assert @parser[:pi] == 3.14
49
- assert @parser[:pi].class == Float
48
+ assert_equal @parser[:pi], 3.14
49
+ assert_equal @parser[:pi].class, Float
50
50
  end
51
51
 
52
52
  def test_filter
53
- assert @parser[:name] == 'shokai'
53
+ assert_equal @parser[:name], 'shokai'
54
54
  end
55
55
 
56
56
  def test_missing_arg
57
- assert @parser[:b] != true
57
+ assert_equal @parser[:b], nil
58
58
  end
59
59
 
60
60
  def test_switch
61
- assert @parser[:help] == true
61
+ assert_equal @parser[:help], true
62
62
  end
63
63
 
64
64
  def test_has_param?
@@ -20,45 +20,45 @@ class TestArgsParserStyleEqual < MiniTest::Test
20
20
  end
21
21
 
22
22
  def test_first
23
- assert @parser.first == 'test'
23
+ assert_equal @parser.first, 'test'
24
24
  end
25
25
 
26
26
  def test_argv
27
- assert @parser.argv == ['test', 'foo', 'bar']
27
+ assert_equal @parser.argv, ['test', 'foo', 'bar']
28
28
  end
29
29
 
30
30
  def test_arg
31
- assert @parser[:input] == 'http://shokai.org'
31
+ assert_equal @parser[:input], 'http://shokai.org'
32
32
  end
33
33
 
34
34
  def test_alias
35
- assert @parser[:output] == './out'
35
+ assert_equal @parser[:output], './out'
36
36
  end
37
37
 
38
38
  def test_string_access
39
- assert @parser['output'] == './out'
39
+ assert_equal @parser['output'], './out'
40
40
  end
41
41
 
42
42
  def test_cast_integer
43
- assert @parser[:depth] == -30
44
- assert @parser[:depth].class == Fixnum
43
+ assert_equal @parser[:depth], -30
44
+ assert_equal @parser[:depth].class, Fixnum
45
45
  end
46
46
 
47
47
  def test_cast_float
48
- assert @parser[:pi] == -3.14
49
- assert @parser[:pi].class == Float
48
+ assert_equal @parser[:pi], -3.14
49
+ assert_equal @parser[:pi].class, Float
50
50
  end
51
51
 
52
52
  def test_filter
53
- assert @parser[:name] == 'shokai'
53
+ assert_equal @parser[:name], 'shokai'
54
54
  end
55
55
 
56
56
  def test_missing_arg
57
- assert @parser[:b] != true
57
+ assert_equal @parser[:b], nil
58
58
  end
59
59
 
60
60
  def test_switch
61
- assert @parser[:help] == true
61
+ assert_equal @parser[:help], true
62
62
  end
63
63
 
64
64
  def test_has_param?
@@ -11,11 +11,11 @@ class TestArgsParserDefault < MiniTest::Test
11
11
  end
12
12
 
13
13
  def test_default
14
- assert @parser[:name] == 'shokai'
14
+ assert_equal @parser[:name], 'shokai'
15
15
  end
16
16
 
17
17
  def test_overwrite
18
- assert @parser[:age] == 40
18
+ assert_equal @parser[:age], 40
19
19
  end
20
20
 
21
21
  def test_name
@@ -1,5 +1,4 @@
1
1
  require 'rubygems'
2
- require 'bundler/setup'
3
2
  require 'minitest/autorun'
4
3
 
5
4
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
@@ -0,0 +1,26 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestArgsParserOn < MiniTest::Test
4
+
5
+ def test_filter_error
6
+ argv = ['--h', '--say', 'hello']
7
+ help_called = false
8
+ say_called = false
9
+ args = ArgsParser.parse argv do
10
+ arg :help, 'show help', :alias => :h
11
+ arg :say, 'string'
12
+
13
+ on :say do |v|
14
+ say_called = v
15
+ end
16
+
17
+ on :help do
18
+ help_called = true
19
+ end
20
+ end
21
+
22
+ assert_equal say_called, "hello"
23
+ assert_equal help_called, true
24
+ end
25
+
26
+ end
@@ -12,7 +12,7 @@ class TestArgsParserValidator < MiniTest::Test
12
12
  validate :url, 'invalid URL' do |v|
13
13
  v =~ Regexp.new("^https?://.+$")
14
14
  end
15
-
15
+
16
16
  on_validate_error do |err, name, value|
17
17
  @@err = err
18
18
  @@name = name
@@ -22,8 +22,8 @@ class TestArgsParserValidator < MiniTest::Test
22
22
  end
23
23
 
24
24
  def test_validate_error
25
- assert @@name == :url
26
- assert @@value == 'hptt://shokai.org'
27
- assert @@err.class == ArgsParser::ValidationError
25
+ assert_equal @@name, :url
26
+ assert_equal @@value, 'hptt://shokai.org'
27
+ assert_equal @@err.class, ArgsParser::ValidationError
28
28
  end
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: args_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sho Hashimoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-08 00:00:00.000000000 Z
11
+ date: 2013-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -84,22 +84,25 @@ files:
84
84
  - lib/args_parser/config.rb
85
85
  - lib/args_parser/error.rb
86
86
  - lib/args_parser/filter.rb
87
+ - lib/args_parser/main.rb
87
88
  - lib/args_parser/parser.rb
88
89
  - lib/args_parser/styles/default.rb
89
90
  - lib/args_parser/styles/equal.rb
90
91
  - lib/args_parser/validator.rb
91
92
  - lib/args_parser/version.rb
92
93
  - samples/download_webpage.rb
93
- - samples/twitter_timeline.rb
94
+ - samples/equal_style.rb
94
95
  - test/test_args_parser.rb
95
96
  - test/test_args_parser_style_equal.rb
96
97
  - test/test_default.rb
97
98
  - test/test_filter.rb
98
99
  - test/test_helper.rb
100
+ - test/test_on.rb
99
101
  - test/test_validator.rb
100
102
  - test/test_version.rb
101
103
  homepage: http://shokai.github.com/args_parser
102
- licenses: []
104
+ licenses:
105
+ - MIT
103
106
  metadata: {}
104
107
  post_install_message:
105
108
  rdoc_options: []
@@ -117,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
120
  version: '0'
118
121
  requirements: []
119
122
  rubyforge_project:
120
- rubygems_version: 2.0.3
123
+ rubygems_version: 2.0.5
121
124
  signing_key:
122
125
  specification_version: 4
123
126
  summary: Parse/Filter/Validate ARGV from command line with DSL.
@@ -127,6 +130,6 @@ test_files:
127
130
  - test/test_default.rb
128
131
  - test/test_filter.rb
129
132
  - test/test_helper.rb
133
+ - test/test_on.rb
130
134
  - test/test_validator.rb
131
135
  - test/test_version.rb
132
- has_rdoc:
@@ -1,72 +0,0 @@
1
- #!/usr/bin/env ruby
2
- ## equal style sample app
3
- ## % gem install twitter --version=3.7.0
4
-
5
- $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
6
- require 'rubygems'
7
- require 'args_parser'
8
- gem 'twitter', '< 4.0.0', '>= 3.7.0'
9
- require 'twitter'
10
-
11
- args = ArgsParser.parse ARGV, :style => :equal do
12
- arg :user, 'user', :alias => :u
13
- arg :favorite, 'show favorites', :alias => :fav
14
- arg :retweet, 'show retweets', :alias => :rt
15
- arg :help, 'show help', :alias => :h
16
-
17
- filter :user do |v|
18
- v.to_s.strip
19
- end
20
-
21
- validate :user, "invalid twitter screen_name" do |v|
22
- v =~ /^[a-zA-Z0-9_]+$/
23
- end
24
- end
25
-
26
- if args.has_option? :help or !args.has_param? :user
27
- STDERR.puts "Twitter Timeline\n=="
28
- STDERR.puts args.help
29
- STDERR.puts "e.g. ruby #{$0} --user=ymrl"
30
- STDERR.puts "e.g. ruby #{$0} --user=ymrl --fav"
31
- STDERR.puts "e.g. ruby #{$0} --user=ymrl --fav --rt"
32
- exit 1
33
- end
34
-
35
- p args
36
-
37
- Twitter::configure do
38
- end
39
-
40
- data = []
41
- Twitter::user_timeline(args[:user]).each do |i|
42
- data.push(:id => i.id,
43
- :name => i.user.screen_name,
44
- :text => i.text,
45
- :date => i.created_at)
46
- end
47
-
48
- if args.has_option? :favorite
49
- Twitter::favorites(args[:user]).each do |i|
50
- data.push(:id => i.id,
51
- :name => i.user.screen_name,
52
- :text => i.text,
53
- :date => i.created_at)
54
- end
55
- end
56
-
57
- if args.has_option? :retweet
58
- Twitter::retweeted_by_user(args[:user]).each do |i|
59
- data.push(:id => i.id,
60
- :name => i.user.screen_name,
61
- :text => i.text,
62
- :date => i.created_at)
63
- end
64
- end
65
-
66
- data.uniq{|i|
67
- i[:id]
68
- }.sort{|a,b|
69
- a[:date] <=> b[:date]
70
- }.each{|i|
71
- puts "@#{i[:name]}\t#{i[:text]}"
72
- }