args_parser 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +5 -0
- data/README.rdoc +11 -0
- data/lib/args_parser/config.rb +9 -0
- data/lib/args_parser/parser.rb +15 -30
- data/lib/args_parser/styles/default.rb +32 -0
- data/lib/args_parser/styles/equal.rb +23 -0
- data/lib/args_parser.rb +4 -1
- data/samples/download_webpage.rb +2 -0
- data/samples/twitter_timeline.rb +72 -0
- data/test/test_args_parser_style_equal.rb +84 -0
- metadata +107 -88
data/Manifest.txt
CHANGED
@@ -5,12 +5,17 @@ Manifest.txt
|
|
5
5
|
README.rdoc
|
6
6
|
Rakefile
|
7
7
|
lib/args_parser.rb
|
8
|
+
lib/args_parser/config.rb
|
8
9
|
lib/args_parser/parser.rb
|
9
10
|
lib/args_parser/filter.rb
|
11
|
+
lib/args_parser/styles/default.rb
|
12
|
+
lib/args_parser/styles/equal.rb
|
10
13
|
lib/args_parser/validator.rb
|
11
14
|
samples/download_webpage.rb
|
15
|
+
samples/twitter_timeline.rb
|
12
16
|
script/console
|
13
17
|
script/destroy
|
14
18
|
script/generate
|
15
19
|
test/test_args_parser.rb
|
20
|
+
test/test_args_parser_style_equal.rb
|
16
21
|
test/test_helper.rb
|
data/README.rdoc
CHANGED
@@ -43,6 +43,17 @@ parse ARGV
|
|
43
43
|
puts "saved! => #{parser[:output]}"
|
44
44
|
|
45
45
|
|
46
|
+
equal style
|
47
|
+
|
48
|
+
% ruby samples/twitter_timeline.rb --user=shokai --fav --rt
|
49
|
+
|
50
|
+
parse equal style ARGV
|
51
|
+
|
52
|
+
parser = ArgsParser.parse ARGV, :style => :equal do
|
53
|
+
|
54
|
+
see more [samples](https://github.com/shokai/args_parser/tree/master/samples)
|
55
|
+
|
56
|
+
|
46
57
|
== REQUIREMENTS:
|
47
58
|
|
48
59
|
* Ruby 1.8.7+
|
data/lib/args_parser/parser.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
|
2
2
|
module ArgsParser
|
3
|
-
def self.parse(argv=[], &block)
|
4
|
-
|
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)
|
5
8
|
parser.parse argv
|
6
9
|
parser
|
7
10
|
end
|
@@ -28,7 +31,8 @@ module ArgsParser
|
|
28
31
|
end
|
29
32
|
|
30
33
|
public
|
31
|
-
def initialize(&block)
|
34
|
+
def initialize(config, &block)
|
35
|
+
@config = config
|
32
36
|
unless block_given?
|
33
37
|
raise ArgumentError, 'initialize block was not given'
|
34
38
|
end
|
@@ -68,35 +72,16 @@ module ArgsParser
|
|
68
72
|
end
|
69
73
|
|
70
74
|
def parse(argv)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
80
|
-
else
|
81
|
-
if arg =~ /^-+[^-\s]+$/
|
82
|
-
params[k][:value] = true
|
83
|
-
k = arg.scan(/^-+([^-\s]+)$/)[0][0].strip.to_sym
|
84
|
-
k = aliases[k] if aliases[k]
|
85
|
-
else
|
86
|
-
arg = @filter.filter k, arg
|
87
|
-
msg = @validator.validate k, arg
|
88
|
-
if msg
|
89
|
-
STDERR.puts "Error: #{msg} (--#{k} #{arg})"
|
90
|
-
exit 1
|
91
|
-
end
|
92
|
-
params[k][:value] = arg
|
93
|
-
k = nil
|
94
|
-
end
|
75
|
+
method("parse_style_#{@config[:style]}".to_sym).call(argv)
|
76
|
+
params.each do |name, param|
|
77
|
+
next if [nil, true].include? param[:value]
|
78
|
+
param[:value] = @filter.filter name, param[:value]
|
79
|
+
msg = @validator.validate name, param[:value]
|
80
|
+
if msg
|
81
|
+
STDERR.puts "Error: #{msg} (--#{name} #{param[:value]})"
|
82
|
+
exit 1
|
95
83
|
end
|
96
84
|
end
|
97
|
-
if k
|
98
|
-
params[k][:value] = true
|
99
|
-
end
|
100
85
|
end
|
101
86
|
|
102
87
|
def [](key)
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
module ArgsParser
|
3
|
+
class Parser
|
4
|
+
|
5
|
+
def parse_style_default(argv)
|
6
|
+
k = nil
|
7
|
+
argv.each_with_index do |arg, index|
|
8
|
+
unless k
|
9
|
+
if arg =~ /^-+[^-\s]+$/
|
10
|
+
k = arg.scan(/^-+([^-\s]+)$/)[0][0].strip.to_sym
|
11
|
+
k = aliases[k] if aliases[k]
|
12
|
+
elsif index == 0
|
13
|
+
@first = arg
|
14
|
+
end
|
15
|
+
else
|
16
|
+
if arg =~ /^-+[^-\s]+$/
|
17
|
+
params[k][:value] = true
|
18
|
+
k = arg.scan(/^-+([^-\s]+)$/)[0][0].strip.to_sym
|
19
|
+
k = aliases[k] if aliases[k]
|
20
|
+
else
|
21
|
+
params[k][:value] = arg
|
22
|
+
k = nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
if k
|
27
|
+
params[k][:value] = true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
module ArgsParser
|
3
|
+
class Parser
|
4
|
+
|
5
|
+
def parse_style_equal(argv)
|
6
|
+
argv.each_with_index do |arg, i|
|
7
|
+
if arg =~ /^-+[^-=\s]+$/
|
8
|
+
k,v = [arg.scan(/^-+([^-=\s]+)$/)[0][0], true]
|
9
|
+
elsif arg =~ /^-+[^-=\s]+=[^=\s]+$/
|
10
|
+
k,v = arg.scan(/^-+([^-=\s]+)=([^=\s]+)$/)[0]
|
11
|
+
elsif i == 0
|
12
|
+
@first = arg
|
13
|
+
end
|
14
|
+
if k and v
|
15
|
+
k = k.strip.to_sym
|
16
|
+
k = aliases[k] if aliases[k]
|
17
|
+
params[k][:value] = v
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/lib/args_parser.rb
CHANGED
@@ -2,9 +2,12 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
4
|
require 'args_parser/parser'
|
5
|
+
require 'args_parser/styles/default'
|
6
|
+
require 'args_parser/styles/equal'
|
5
7
|
require 'args_parser/filter'
|
6
8
|
require 'args_parser/validator'
|
9
|
+
require 'args_parser/config'
|
7
10
|
|
8
11
|
module ArgsParser
|
9
|
-
VERSION = '0.0.
|
12
|
+
VERSION = '0.0.6'
|
10
13
|
end
|
data/samples/download_webpage.rb
CHANGED
@@ -0,0 +1,72 @@
|
|
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
|
+
parser = 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.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 parser.has_option? :help or !parser.has_param? :user
|
27
|
+
STDERR.puts "Twitter Timeline\n=="
|
28
|
+
STDERR.puts parser.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 parser
|
36
|
+
|
37
|
+
Twitter::configure do
|
38
|
+
end
|
39
|
+
|
40
|
+
data = []
|
41
|
+
Twitter::user_timeline(parser[: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 parser.has_option? :favorite
|
49
|
+
Twitter::favorites(parser[: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 parser.has_option? :retweet
|
58
|
+
Twitter::retweeted_by_user(parser[: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
|
+
}
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestArgsParserStyleEqual < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@argv = 'test --input=http://shokai.org --a --o=./out -h --depth=030 --pi=3.14 --n=ShoKaI'.split(/\s+/)
|
6
|
+
@parser = ArgsParser.parse @argv, :style => :equal do
|
7
|
+
arg :input, 'input', :alias => :i
|
8
|
+
arg :output, 'output dir', :alias => :o
|
9
|
+
arg :name, 'user name', :alias => :n
|
10
|
+
arg :help, 'show help', :alias => :h
|
11
|
+
|
12
|
+
filter :name do |v|
|
13
|
+
v.downcase
|
14
|
+
end
|
15
|
+
|
16
|
+
validate :input, "input must be valid URL" do |v|
|
17
|
+
v =~ /^https?:\/\/.+/
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_first
|
23
|
+
assert @parser.first == 'test'
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_arg
|
27
|
+
assert @parser[:input] == 'http://shokai.org'
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_alias
|
31
|
+
assert @parser[:output] == './out'
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_cast_integer
|
35
|
+
assert @parser[:depth] == 30
|
36
|
+
assert @parser[:depth].class == Fixnum
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_cast_float
|
40
|
+
assert @parser[:pi] == 3.14
|
41
|
+
assert @parser[:pi].class == Float
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_filter
|
45
|
+
assert @parser[:name] == 'shokai'
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_missing_arg
|
49
|
+
assert @parser[:b] != true
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_switch
|
53
|
+
assert @parser[:help] == true
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_has_param?
|
57
|
+
assert @parser.has_param? :input
|
58
|
+
assert @parser.has_param? :output
|
59
|
+
assert @parser.has_param? :depth
|
60
|
+
assert @parser.has_param? :pi
|
61
|
+
assert @parser.has_param? :name
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_has_params?
|
65
|
+
assert @parser.has_param? :input, :output, :depth, :pi, :name
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_has_not_param?
|
69
|
+
assert !@parser.has_param?(:a)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_has_option?
|
73
|
+
assert @parser.has_option? :help
|
74
|
+
assert @parser.has_option? :a
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_has_options?
|
78
|
+
assert @parser.has_option? :help, :a
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_has_not_option?
|
82
|
+
assert !@parser.has_option?(:b)
|
83
|
+
end
|
84
|
+
end
|
metadata
CHANGED
@@ -1,107 +1,126 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: args_parser
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.5
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
|
7
|
+
authors:
|
8
|
+
- Sho Hashimoto
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
12
|
+
date: 2012-09-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rdoc
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.10'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.10'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: newgem
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.5.3
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.5.3
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: hoe
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.1'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
48
62
|
description: ARGV parser - Parse args from command line.
|
49
|
-
email:
|
50
|
-
|
63
|
+
email:
|
64
|
+
- hashimoto@shokai.org
|
51
65
|
executables: []
|
52
|
-
|
53
66
|
extensions: []
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
67
|
+
extra_rdoc_files:
|
68
|
+
- History.txt
|
69
|
+
- Manifest.txt
|
70
|
+
- README.rdoc
|
71
|
+
files:
|
72
|
+
- Gemfile
|
73
|
+
- Gemfile.lock
|
74
|
+
- History.txt
|
75
|
+
- Manifest.txt
|
76
|
+
- README.rdoc
|
77
|
+
- Rakefile
|
78
|
+
- lib/args_parser.rb
|
79
|
+
- lib/args_parser/config.rb
|
80
|
+
- lib/args_parser/parser.rb
|
81
|
+
- lib/args_parser/filter.rb
|
82
|
+
- lib/args_parser/styles/default.rb
|
83
|
+
- lib/args_parser/styles/equal.rb
|
84
|
+
- lib/args_parser/validator.rb
|
85
|
+
- samples/download_webpage.rb
|
86
|
+
- samples/twitter_timeline.rb
|
87
|
+
- script/console
|
88
|
+
- script/destroy
|
89
|
+
- script/generate
|
90
|
+
- test/test_args_parser.rb
|
91
|
+
- test/test_args_parser_style_equal.rb
|
92
|
+
- test/test_helper.rb
|
93
|
+
- .gemtest
|
77
94
|
homepage: http://github.com/shokai/args_parser
|
78
95
|
licenses: []
|
79
|
-
|
80
96
|
post_install_message:
|
81
|
-
rdoc_options:
|
82
|
-
|
83
|
-
|
84
|
-
require_paths:
|
85
|
-
|
86
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
rdoc_options:
|
98
|
+
- --main
|
99
|
+
- README.rdoc
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
103
|
none: false
|
88
|
-
requirements:
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: 3014351784407155723
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
112
|
none: false
|
94
|
-
requirements:
|
95
|
-
|
96
|
-
|
97
|
-
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
98
117
|
requirements: []
|
99
|
-
|
100
118
|
rubyforge_project: args_parser
|
101
|
-
rubygems_version: 1.8.
|
119
|
+
rubygems_version: 1.8.24
|
102
120
|
signing_key:
|
103
121
|
specification_version: 3
|
104
122
|
summary: ARGV parser - Parse args from command line.
|
105
|
-
test_files:
|
106
|
-
|
107
|
-
|
123
|
+
test_files:
|
124
|
+
- test/test_args_parser.rb
|
125
|
+
- test/test_args_parser_style_equal.rb
|
126
|
+
- test/test_helper.rb
|