argv 1.1.1 → 2.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 +32 -28
- data/VERSION +1 -1
- data/lib/argv.rb +2 -70
- data/lib/argv/cms.rb +33 -0
- data/lib/argv/ext.rb +77 -0
- data/sample/test.rb +23 -6
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bda76d6b927be05fc63143b9bde823c8c397bd9
|
4
|
+
data.tar.gz: 2afca9cf6493a55f328532b75a59e31cb0cd643c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0becc8a32de3cfd6f5495f7c4e0fa17db6d6462f74f15a5001a140e6cf03d776a9625bd9d79e502a9493d54b18dd0e432ff39098f46809eb9c3a902c5283bb7
|
7
|
+
data.tar.gz: 6adf6f6ab00b2e021f9c4dedcab2bc41f5673445b02bb71124908da494772b3c7e6e9749d58c76a27be0697061d68af86fb05bd7014c070dc8cfb810a58996e3
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -10,51 +10,55 @@ With this extension module, you can parse in an easy way the script input tags
|
|
10
10
|
|
11
11
|
require 'argv'
|
12
12
|
|
13
|
-
|
13
|
+
#$ ruby sample/test.rb --test test this ok
|
14
14
|
|
15
|
-
puts ARGV.
|
16
|
-
# {"--test"=>"
|
15
|
+
puts ARGV.to_hash
|
16
|
+
# {"--test"=>"test"}
|
17
17
|
|
18
|
-
puts ARGV.
|
19
|
-
#
|
20
|
-
|
21
|
-
```
|
22
|
-
|
23
|
-
```ruby
|
18
|
+
puts ARGV.to_hash( multi_value: true )
|
19
|
+
# {"--test"=>["test", "this", "ok"]}
|
24
20
|
|
21
|
+
puts ARGV.to_hash( sym_key: true )
|
22
|
+
# {:test=>"test"}
|
25
23
|
|
26
|
-
|
24
|
+
puts ARGV.to_hash( s: true, m: true )
|
25
|
+
# {:test=>["test", "this", "ok"]}
|
27
26
|
|
28
27
|
puts ARGV.values.inspect
|
29
|
-
|
28
|
+
# ["test", "this", "ok"]
|
30
29
|
|
31
|
-
puts ARGV.
|
32
|
-
|
30
|
+
puts ARGV.keys.inspect
|
31
|
+
# ["--test"]
|
33
32
|
|
33
|
+
puts ARGV.flag_syms.inspect
|
34
|
+
# [:test, :test, :this, :ok]
|
34
35
|
|
35
36
|
```
|
36
37
|
|
37
|
-
|
38
|
-
|
38
|
+
#### For Modules
|
39
39
|
|
40
|
-
|
40
|
+
if you write module and you want to have some custom tags and help msg for that,
|
41
|
+
use the following example
|
41
42
|
|
42
|
-
|
43
|
-
#> ["this", "world", "asd", "sdsd"]
|
44
|
-
|
45
|
-
puts ARGV.flagtags sym_key: true
|
46
|
-
#> {:test=>"this", :hello=>"world", :"su-p"=>"asd"}
|
43
|
+
```ruby
|
47
44
|
|
45
|
+
require 'argv'
|
48
46
|
|
49
|
-
|
47
|
+
ARGVEXT.add_help_msg "This will show you the help msg (this)",:helper,:help,:h
|
50
48
|
|
51
|
-
|
49
|
+
#
|
50
|
+
# ARGVEXT.help_msg or show_help will read on your values and will produce the following with this example
|
51
|
+
#
|
52
|
+
# This will show you the help msg (this)
|
53
|
+
# --helper
|
54
|
+
# --help
|
55
|
+
# -h
|
52
56
|
|
53
|
-
|
57
|
+
# this will run on the terminal and break Process,
|
58
|
+
# if the user give one of the helper tags as argv
|
54
59
|
|
55
|
-
|
60
|
+
```
|
56
61
|
|
57
|
-
|
58
|
-
#> {"--test"=>["this"], "--hello"=>["world"], "--su-p"=>["asd", "sdsd"]}
|
62
|
+
### Test
|
59
63
|
|
60
|
-
|
64
|
+
* you can find a test file in the sample named as "test"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.1.0
|
data/lib/argv.rb
CHANGED
@@ -1,70 +1,2 @@
|
|
1
|
-
|
2
|
-
|
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
|
-
|
30
|
-
|
31
|
-
return_obj= {}
|
32
|
-
self.count.times do |index|
|
33
|
-
|
34
|
-
begin
|
35
|
-
|
36
|
-
if self[index][0].include?('-') && !self[index+1][0].include?('-')
|
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
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
rescue
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
return return_obj
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
def flags
|
61
|
-
self.select { |e| e[0].include?('-') }
|
62
|
-
end
|
63
|
-
|
64
|
-
def values
|
65
|
-
self.select { |e| !e[0].include?('-') }
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
ARGV.__send__ :extend,ARGVEXT
|
1
|
+
require 'argv/ext'
|
2
|
+
require 'argv/cms'
|
data/lib/argv/cms.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module ARGVEXT
|
2
|
+
class << self
|
3
|
+
|
4
|
+
@@help_msg_obj ||= []
|
5
|
+
def add_help_msg msg,*keys
|
6
|
+
raise unless msg.class <= String
|
7
|
+
@@help_msg_obj.push [msg,keys]
|
8
|
+
return nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def help_msg
|
12
|
+
|
13
|
+
helper_sym= [:helper,:help,:h]
|
14
|
+
self.add_help_msg "This will show you the help msg (this)",*helper_sym
|
15
|
+
unless ::ARGV.sym_flags.select{ |e| helper_sym.include?(e) }.empty?
|
16
|
+
|
17
|
+
tmp_ary= []
|
18
|
+
@@help_msg_obj.each{ |ary|
|
19
|
+
tmp_ary.push(ary[0])
|
20
|
+
ary[1].each{|e| tmp_ary.push( ( e.to_s.length == 1 ? "-#{e}" : "--#{e}" ) )}
|
21
|
+
tmp_ary.push("")
|
22
|
+
}
|
23
|
+
|
24
|
+
tmp_ary.each{ |element| element.include?('--') ? element.gsub!('--',"\t--") : element.gsub!('-',"\t -")}
|
25
|
+
puts "",tmp_ary.join("\n"),""
|
26
|
+
Process.exit!
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
alias :show_help :help_msg
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/argv/ext.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
module ARGVEXT
|
2
|
+
|
3
|
+
module EXTENSION
|
4
|
+
|
5
|
+
def to_hash opts= {}
|
6
|
+
raise(ArgumentError) unless opts.class <= Hash
|
7
|
+
|
8
|
+
opts[:sym_key] ||= opts[:sk] || opts[:sym] || opts[:s]
|
9
|
+
opts[:multi_value] ||= opts[:mv] || opts[:m]
|
10
|
+
|
11
|
+
return_obj= {}
|
12
|
+
self.count.times do |index|
|
13
|
+
unless opts[:multi_value]
|
14
|
+
|
15
|
+
next if self[index+1].nil?
|
16
|
+
if self[index][0].include?('-') && !self[index+1][0].include?('-')
|
17
|
+
return_obj[( opts[:sym_key] ? self[index].to_s.dup.gsub!(/^-*/,'').to_sym : self[index] )]= self[index+1]
|
18
|
+
end
|
19
|
+
|
20
|
+
else
|
21
|
+
|
22
|
+
begin
|
23
|
+
|
24
|
+
if self[index][0].include?('-') && !self[index+1][0].include?('-')
|
25
|
+
|
26
|
+
new_element= []
|
27
|
+
index_at= index+1
|
28
|
+
loop do
|
29
|
+
if self[index_at].nil? || self[index_at].to_s[0].include?('-')
|
30
|
+
break
|
31
|
+
else
|
32
|
+
new_element.push(self[index_at])
|
33
|
+
end
|
34
|
+
index_at += 1
|
35
|
+
end
|
36
|
+
return_obj[( opts[:sym_key] ? self[index].to_s.dup.gsub!(/^-*/,'').to_sym : self[index] )]= new_element
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
rescue
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
return return_obj
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
alias :flagtag :to_hash
|
51
|
+
alias :flagtags :to_hash
|
52
|
+
|
53
|
+
def flags
|
54
|
+
self.select { |e| e[0].include?('-') }
|
55
|
+
end
|
56
|
+
|
57
|
+
alias :flag :flags
|
58
|
+
alias :tags :flags
|
59
|
+
alias :keys :flags
|
60
|
+
|
61
|
+
def sym_flags
|
62
|
+
self.flags.map { |e| e.to_s.dup.gsub!(/^-*/,'').to_sym }
|
63
|
+
end
|
64
|
+
|
65
|
+
alias :flag_syms :sym_flags
|
66
|
+
alias :flag_sym :sym_flags
|
67
|
+
alias :tag_syms :sym_flags
|
68
|
+
alias :key_syms :sym_flags
|
69
|
+
|
70
|
+
def values
|
71
|
+
self.select { |e| !e[0].include?('-') }
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
ARGV.__send__ :extend,ARGVEXT::EXTENSION
|
data/sample/test.rb
CHANGED
@@ -1,9 +1,26 @@
|
|
1
|
-
|
1
|
+
require "argv"
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
#$ ruby sample/test.rb --test test this ok
|
4
|
+
ARGVEXT.help_msg
|
5
5
|
|
6
|
-
|
6
|
+
puts "","original ARGV:",ARGV.inspect,""
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
puts "hash from argv:",ARGV.to_hash,""
|
9
|
+
# {"--test"=>"test"}
|
10
|
+
|
11
|
+
puts "multi valued hash:",ARGV.to_hash( multi_value: true ),""
|
12
|
+
# {"--test"=>["test", "this", "ok"]}
|
13
|
+
|
14
|
+
puts "sym keyed hash:",ARGV.to_hash( sym_key: true ),""
|
15
|
+
# {:test=>"test"}
|
16
|
+
|
17
|
+
puts "sym keyed multi valued hash:",ARGV.to_hash( s: true, m: true ),""
|
18
|
+
# {:test=>["test", "this", "ok"]}
|
19
|
+
|
20
|
+
puts "argv values without the tags:",ARGV.values.inspect,""
|
21
|
+
# ["test", "this", "ok"]
|
22
|
+
|
23
|
+
puts "argv tags, \"keys\":",ARGV.keys.inspect,""
|
24
|
+
# ["--test"]
|
25
|
+
|
26
|
+
puts "symbolized flags:",ARGV.sym_flags.inspect,""
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: argv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
@@ -55,6 +55,8 @@ files:
|
|
55
55
|
- VERSION
|
56
56
|
- argv.gemspec
|
57
57
|
- lib/argv.rb
|
58
|
+
- lib/argv/cms.rb
|
59
|
+
- lib/argv/ext.rb
|
58
60
|
- sample/test.rb
|
59
61
|
homepage:
|
60
62
|
licenses: []
|