argv 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +41 -0
- data/VERSION +1 -1
- data/lib/argv.rb +46 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9bc8f2bf35c658b1039a558b3ade585f7d9f7efc
|
4
|
+
data.tar.gz: a7a769f338a6106b75aa9169906fccab98efebed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bab7e3eb9a01d5f2ecab5cceb9d519942dd2cc55308a3a3c09f006c29e8c4c6296f49350aa6167b87738ead1747c131c9cbc62e9bae9571dc03d60853dddf32
|
7
|
+
data.tar.gz: e73767cda42b3bb00f3cc0bb00191ee18a6416a31d3c946388cca3f979bb6f97d08b8c321b29fa1f6c84f02b6e96d2b2594fe3b6d8c491bcff24e028bdb8c5c5
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -8,6 +8,8 @@ With this extension module, you can parse in an easy way the script input tags
|
|
8
8
|
|
9
9
|
```ruby
|
10
10
|
|
11
|
+
require 'argv'
|
12
|
+
|
11
13
|
#> $ script.rb --test this --hello world --sup
|
12
14
|
|
13
15
|
puts ARGV.flag_tags
|
@@ -16,4 +18,43 @@ With this extension module, you can parse in an easy way the script input tags
|
|
16
18
|
puts ARGV.flags.inspect
|
17
19
|
# ["--test", "--hello", "--sup"]
|
18
20
|
|
21
|
+
```
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
|
25
|
+
|
26
|
+
#> $ script.rb --test this --hello world --su-p asd sdsd -asd -dsa
|
27
|
+
|
28
|
+
puts ARGV.values.inspect
|
29
|
+
#> ["this", "world", "asd", "sdsd"]
|
30
|
+
|
31
|
+
puts ARGV.flagtags sym_key: true
|
32
|
+
#> {:test=>"this", :hello=>"world", :"su-p"=>"asd"}
|
33
|
+
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
|
39
|
+
|
40
|
+
#> $ script.rb --test this this multi value --hello world asdf
|
41
|
+
|
42
|
+
puts ARGV.values.inspect
|
43
|
+
#> ["this", "world", "asd", "sdsd"]
|
44
|
+
|
45
|
+
puts ARGV.flagtags sym_key: true
|
46
|
+
#> {:test=>"this", :hello=>"world", :"su-p"=>"asd"}
|
47
|
+
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
### multi value use case
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
|
55
|
+
#> $ script.rb --test this --hello world --su-p asd sdsd -asd -dsa
|
56
|
+
|
57
|
+
puts ARGV.flag_multi_value.inspect
|
58
|
+
#> {"--test"=>["this"], "--hello"=>["world"], "--su-p"=>["asd", "sdsd"]}
|
59
|
+
|
19
60
|
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.1.0
|
data/lib/argv.rb
CHANGED
@@ -1,6 +1,32 @@
|
|
1
1
|
module ARGVEXT
|
2
2
|
|
3
|
-
def flag_tags
|
3
|
+
def flag_tags opts= {}
|
4
|
+
raise(ArgumentError) unless opts.class <= Hash
|
5
|
+
|
6
|
+
opts[:sym_key] ||= opts[:sym] || opts[:sk] || opts[:s]
|
7
|
+
|
8
|
+
return_obj= {}
|
9
|
+
self.count.times do |index|
|
10
|
+
|
11
|
+
next if self[index+1].nil?
|
12
|
+
if self[index][0].include?('-') && !self[index+1][0].include?('-')
|
13
|
+
return_obj[( opts[:sym_key] ? self[index].dup.to_s.gsub!(/^-*/,'').to_sym : self[index] )]= self[index+1]
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
return return_obj
|
18
|
+
|
19
|
+
end
|
20
|
+
alias :flag_value :flag_tags
|
21
|
+
alias :flag_tag :flag_tags
|
22
|
+
alias :flagtag :flag_tags
|
23
|
+
alias :flagtags :flag_tags
|
24
|
+
|
25
|
+
def flag_multi_value opts= {}
|
26
|
+
raise(ArgumentError) unless opts.class <= Hash
|
27
|
+
|
28
|
+
opts[:sym_key] ||= opts[:sym] || opts[:sk] || opts[:s]
|
29
|
+
|
4
30
|
|
5
31
|
return_obj= {}
|
6
32
|
self.count.times do |index|
|
@@ -8,7 +34,19 @@ module ARGVEXT
|
|
8
34
|
begin
|
9
35
|
|
10
36
|
if self[index][0].include?('-') && !self[index+1][0].include?('-')
|
11
|
-
|
37
|
+
|
38
|
+
new_element= []
|
39
|
+
index_at= index+1
|
40
|
+
loop do
|
41
|
+
if self[index_at].nil? || self[index_at].to_s[0].include?('-')
|
42
|
+
break
|
43
|
+
else
|
44
|
+
new_element.push(self[index_at])
|
45
|
+
end
|
46
|
+
index_at += 1
|
47
|
+
end
|
48
|
+
return_obj[( opts[:sym_key] ? self[index].dup.to_s.gsub!(/^-*/,'').to_sym : self[index] )]= new_element
|
49
|
+
|
12
50
|
end
|
13
51
|
|
14
52
|
rescue
|
@@ -18,15 +56,15 @@ module ARGVEXT
|
|
18
56
|
return return_obj
|
19
57
|
|
20
58
|
end
|
21
|
-
alias :flag_tag :flag_tags
|
22
|
-
alias :flagtag :flag_tags
|
23
|
-
alias :flagtags :flag_tags
|
24
59
|
|
25
60
|
def flags
|
26
|
-
self.select{ |e| e[0].include?('-') }
|
61
|
+
self.select { |e| e[0].include?('-') }
|
27
62
|
end
|
28
63
|
|
29
|
-
|
64
|
+
def values
|
65
|
+
self.select { |e| !e[0].include?('-') }
|
66
|
+
end
|
30
67
|
|
31
|
-
|
68
|
+
end
|
32
69
|
|
70
|
+
ARGV.__send__ :extend,ARGVEXT
|