args_parser 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/lib/args_parser/parser.rb +9 -2
- data/lib/args_parser/version.rb +1 -1
- data/samples/download_webpage.rb +1 -0
- data/test/test_default.rb +26 -0
- metadata +4 -2
data/History.txt
CHANGED
data/lib/args_parser/parser.rb
CHANGED
@@ -53,6 +53,7 @@ module ArgsParser
|
|
53
53
|
instance_eval(&block)
|
54
54
|
end
|
55
55
|
|
56
|
+
private
|
56
57
|
def arg(name, description, opts={})
|
57
58
|
name = name.to_sym
|
58
59
|
params[name][:default] = opts[:default]
|
@@ -90,6 +91,12 @@ module ArgsParser
|
|
90
91
|
end
|
91
92
|
end
|
92
93
|
|
94
|
+
def default(key)
|
95
|
+
d = params[key.to_sym][:default]
|
96
|
+
(d and d.kind_of? Proc) ? d.call : d
|
97
|
+
end
|
98
|
+
|
99
|
+
public
|
93
100
|
def args
|
94
101
|
params.keys
|
95
102
|
end
|
@@ -115,7 +122,7 @@ module ArgsParser
|
|
115
122
|
end
|
116
123
|
|
117
124
|
def [](key)
|
118
|
-
params[key.to_sym][:value]
|
125
|
+
params[key.to_sym][:value] or default(key)
|
119
126
|
end
|
120
127
|
|
121
128
|
def []=(key, value)
|
@@ -166,7 +173,7 @@ module ArgsParser
|
|
166
173
|
line += " (-#{i[:alias]})" if i[:alias]
|
167
174
|
line = line.ljust(len+2)
|
168
175
|
line += i[:description].to_s
|
169
|
-
line += " : default - #{i[:
|
176
|
+
line += " : default - #{default i[:name]}" if i[:default]
|
170
177
|
line
|
171
178
|
}.join("\n")
|
172
179
|
end
|
data/lib/args_parser/version.rb
CHANGED
data/samples/download_webpage.rb
CHANGED
@@ -9,6 +9,7 @@ parser = ArgsParser.parse ARGV do
|
|
9
9
|
arg :url, 'URL', :alias => :u
|
10
10
|
arg :output, 'output file', :alias => :o, :default => 'out.html'
|
11
11
|
arg :verbose, 'verbose mode'
|
12
|
+
arg :time, 'time', :default => lambda{ Time.now }
|
12
13
|
arg :help, 'show help', :alias => :h
|
13
14
|
|
14
15
|
filter :url do |v|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class TestArgsParserDefault < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@argv = ['--age', '40']
|
6
|
+
@parser = ArgsParser.parse @argv do
|
7
|
+
arg :name, 'use name', :default => 'shokai'
|
8
|
+
arg :age, 'age', :default => 14
|
9
|
+
arg :time, 'time', :default => lambda{ Time.now }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_default
|
14
|
+
assert @parser[:name] == 'shokai'
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_overwrite
|
18
|
+
assert @parser[:age] == 40
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_name
|
22
|
+
old = @parser[:time]
|
23
|
+
sleep 1
|
24
|
+
assert old < @parser[:time]
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: args_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-26 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Parse ARGV from command line with DSL.
|
15
15
|
email:
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- samples/twitter_timeline.rb
|
39
39
|
- test/test_args_parser.rb
|
40
40
|
- test/test_args_parser_style_equal.rb
|
41
|
+
- test/test_default.rb
|
41
42
|
- test/test_filter.rb
|
42
43
|
- test/test_helper.rb
|
43
44
|
- test/test_validator.rb
|
@@ -69,6 +70,7 @@ summary: Parse ARGV from command line with DSL.
|
|
69
70
|
test_files:
|
70
71
|
- test/test_args_parser.rb
|
71
72
|
- test/test_args_parser_style_equal.rb
|
73
|
+
- test/test_default.rb
|
72
74
|
- test/test_filter.rb
|
73
75
|
- test/test_helper.rb
|
74
76
|
- test/test_validator.rb
|