argv 2.2.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -15
- data/VERSION +1 -1
- data/argv.gemspec +2 -2
- data/exsample/test.rb +6 -15
- data/lib/argv/cms.rb +36 -37
- data/lib/argv/ext.rb +56 -53
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2e1efaef4c65483411bb74d6463747ea4840cad
|
4
|
+
data.tar.gz: 0598d8b804c0c981439a8eb3c387adf7986e2fde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb887d2e48b8282e0bdafa981d0db6a4087e46ae3ba1d7bf14edfbb52cf7f97af10607b73bc50ca34d604a5fac4b6d8286ded57ec68fde0cb95e5631f662c871
|
7
|
+
data.tar.gz: 4811bf6184d9ead9eeeb969eca0e090b8cc2af907fb3d1273bdf66dfc25a5c9ea1e2502f306b341b4c2288f7c516adca64fa14c6344005c5f9ace3d3969f19b0
|
data/README.md
CHANGED
@@ -10,28 +10,34 @@ With this extension module, you can parse in an easy way the script input tags
|
|
10
10
|
|
11
11
|
require 'argv'
|
12
12
|
|
13
|
-
#$ ruby sample/test.rb --test test this ok
|
13
|
+
#$ ruby sample/test.rb -x -zsa yo --test test this ok
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
ARGV.to_hash
|
16
|
+
#> {"test"=>"test","zsa"=>"yo"}
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
ARGV.to_hash(multi_value: true)
|
19
|
+
ARGV.to_hash(true)
|
20
|
+
#> {"test"=>["test", "this", "ok"],"zsa"=>["yo"]}
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
ARGV.values
|
23
|
+
#> ["test", "this", "ok", "yo"]
|
23
24
|
|
24
|
-
|
25
|
-
|
25
|
+
ARGV.long_options
|
26
|
+
#> ["test"]
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
ARGV.short_options
|
29
|
+
#> ["x","z","s","a"]
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
ARGV.options
|
32
|
+
#> ["x","z","s","a","test"]
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
#$ ruby sample/test.rb -hy -sx as --hello-world test test2
|
35
|
+
|
36
|
+
ARGV.to_hash
|
37
|
+
#> {["s", "x"]=>"as", "hello-world"=>"test"}
|
38
|
+
|
39
|
+
ARGV.to_hash(true)
|
40
|
+
#> {["s", "x"]=>["as"], "hello-world"=>["test", "test2"]}
|
35
41
|
|
36
42
|
```
|
37
43
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.0.0
|
data/argv.gemspec
CHANGED
@@ -7,8 +7,8 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.authors = ["Adam Luzsi"]
|
8
8
|
spec.email = ["adamluzsi@gmail.com"]
|
9
9
|
|
10
|
-
spec.description = %q{ ARGV object extension for
|
11
|
-
spec.summary = %q{ argv object extension for parsing
|
10
|
+
spec.description = %q{ ARGV object extension for ease of use of options and else terminal inputs. It can parse ARGV input into Hash object so you can work with ease, check GIT! }
|
11
|
+
spec.summary = %q{ argv object extension for parsing terminal inputs as arguments/options }
|
12
12
|
|
13
13
|
spec.homepage = "https://github.com/adamluzsi/#{__FILE__.split(File::Separator).last.split('.').first}"
|
14
14
|
spec.files = `git ls-files`.split($/)
|
data/exsample/test.rb
CHANGED
@@ -3,28 +3,19 @@ require "argv"
|
|
3
3
|
#$ ruby sample/test.rb --test test this ok
|
4
4
|
|
5
5
|
# new help
|
6
|
-
ARGV.add_help "
|
7
|
-
|
8
|
-
ARGV.show_help
|
6
|
+
ARGV.add_help "nothing really but just a option",*%W[ o option options]
|
7
|
+
ARGV.show_help#(false)
|
9
8
|
|
10
9
|
puts "","original ARGV:",ARGV.inspect,""
|
11
10
|
|
12
11
|
puts "hash from argv:",ARGV.to_hash,""
|
13
|
-
# {"
|
12
|
+
# {"test"=>"test"}
|
14
13
|
|
15
14
|
puts "multi valued hash:",ARGV.to_hash( multi_value: true ),""
|
16
|
-
# {"
|
17
|
-
|
18
|
-
puts "sym keyed hash:",ARGV.to_hash( sym_key: true ),""
|
19
|
-
# {:test=>"test"}
|
20
|
-
|
21
|
-
puts "sym keyed multi valued hash:",ARGV.to_hash( s: true, m: true ),""
|
22
|
-
# {:test=>["test", "this", "ok"]}
|
15
|
+
# {"test"=>["test", "this", "ok"]}
|
23
16
|
|
24
17
|
puts "argv values without the tags:",ARGV.values.inspect,""
|
25
18
|
# ["test", "this", "ok"]
|
26
19
|
|
27
|
-
puts "argv tags, \"
|
28
|
-
# ["
|
29
|
-
|
30
|
-
puts "symbolized flags:",ARGV.sym_flags.inspect,""
|
20
|
+
puts "argv tags, \"options\":",ARGV.options.inspect,""
|
21
|
+
# ["test"]
|
data/lib/argv/cms.rb
CHANGED
@@ -1,56 +1,55 @@
|
|
1
1
|
module ARGVEXT
|
2
2
|
|
3
3
|
module MemoryCore
|
4
|
-
class << self
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
def add_help_msg(msg,*keys)
|
6
|
+
raise(ArgumentError,'invalid Message, must be String like object') unless msg.class <= String
|
7
|
+
raise(ArgumentError,'invalid options given for this message, must be String like') if keys.any?{|e| !(e.class <= String) }
|
8
|
+
__helper_msg_obj__.push [msg,keys.sort{|a,b| b.length <=> a.length }]
|
9
|
+
return nil
|
10
|
+
end;alias add_help add_help_msg
|
11
|
+
|
12
|
+
def init_basic_help_msg
|
13
|
+
unless @init_done
|
14
|
+
__helper_flags__.push *%W[ helper help h ]
|
15
|
+
self.add_help_msg 'This will show you the help msg (this)',*__helper_flags__
|
16
|
+
@init_done = true
|
11
17
|
end
|
18
|
+
end
|
12
19
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
#> Basic help message is a boolean input,
|
21
|
+
# if false, there will be no basic options made like '-h' '--help'
|
22
|
+
def help_msg(basic_help_msg=true)
|
23
|
+
init_basic_help_msg if !!basic_help_msg
|
24
|
+
unless ::ARGV.options.select{ |e| __helper_flags__.include?(e) }.empty?
|
25
|
+
|
26
|
+
tmp_ary= []
|
27
|
+
__helper_msg_obj__.each do |ary|
|
28
|
+
tmp_ary.push(ary[0])
|
29
|
+
ary[1].each{|e| tmp_ary.push( ( e.to_s.length == 1 ? "-#{e}" : "--#{e}" ) )}
|
30
|
+
tmp_ary.push('')
|
31
|
+
end
|
25
32
|
|
26
|
-
|
27
|
-
|
28
|
-
|
33
|
+
tmp_ary.each{ |element| element.include?('--') ? element.gsub!('--',"\t--") : element.gsub!('-',"\t -")}
|
34
|
+
puts '',tmp_ary.join("\n"),''
|
35
|
+
Process.exit!
|
29
36
|
|
30
|
-
end
|
31
37
|
end
|
38
|
+
return nil
|
39
|
+
end;alias show_help help_msg
|
32
40
|
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
module MemoryEXT
|
41
|
+
protected
|
37
42
|
|
38
|
-
def
|
39
|
-
|
43
|
+
def __helper_msg_obj__
|
44
|
+
@help_msg_obj ||= []
|
40
45
|
end
|
41
46
|
|
42
|
-
|
43
|
-
|
44
|
-
def help_msg
|
45
|
-
::ARGVEXT::MemoryCore.help_msg
|
47
|
+
def __helper_flags__
|
48
|
+
@helper_flags ||= []
|
46
49
|
end
|
47
50
|
|
48
|
-
alias :show_help :help_msg
|
49
|
-
|
50
51
|
end
|
51
52
|
|
52
|
-
self.extend MemoryEXT
|
53
|
-
|
54
53
|
end
|
55
54
|
|
56
|
-
ARGV.__send__ :extend, ARGVEXT::
|
55
|
+
ARGV.__send__ :extend, ARGVEXT::MemoryCore
|
data/lib/argv/ext.rb
CHANGED
@@ -2,79 +2,82 @@ module ARGVEXT
|
|
2
2
|
|
3
3
|
module EXTENSION
|
4
4
|
|
5
|
-
def to_hash
|
6
|
-
raise(ArgumentError) unless opts.class <= Hash
|
5
|
+
def to_hash(opts={})
|
7
6
|
|
8
|
-
|
9
|
-
|
7
|
+
multi_value = if opts.class <= Hash
|
8
|
+
opt_keys = %W[ multi_value mv m ]
|
9
|
+
!!(opts.find{ |k,v| opt_keys.include?(k.to_s) })
|
10
|
+
else
|
11
|
+
!!opts
|
12
|
+
end
|
10
13
|
|
11
|
-
|
12
|
-
self.count.times do |index|
|
13
|
-
unless opts[:multi_value]
|
14
|
+
return (0..self.size).reduce({}) do |return_obj,index|
|
14
15
|
|
15
|
-
|
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
|
16
|
+
if !self[index+1].nil? && self[index][0] =~ /^--?/ && !(self[index+1][0] =~ /^--?/)
|
21
17
|
|
22
18
|
begin
|
23
19
|
|
24
|
-
if self[index]
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
20
|
+
option_key = if self[index] =~ /^--\w+/
|
21
|
+
self[index].sub(/^--/,'')
|
22
|
+
else #if self[index] =~ /^-\w+/
|
23
|
+
if (var = self[index].sub(/^-/,'').split('')).size == 1
|
24
|
+
var[0]
|
25
|
+
else
|
26
|
+
var
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
return_obj[option_key] = if multi_value
|
31
|
+
new_element= []
|
32
|
+
index_at= index+1
|
33
|
+
loop do
|
34
|
+
if self[index_at].nil? || self[index_at].to_s[0].include?('-')
|
35
|
+
break
|
36
|
+
else
|
37
|
+
new_element.push(self[index_at])
|
38
|
+
end
|
39
|
+
index_at += 1
|
40
|
+
end
|
41
|
+
new_element
|
42
|
+
else
|
43
|
+
# return_obj[self[index].sub(/^--?/,'')]=
|
44
|
+
self[index+1]
|
45
|
+
end
|
39
46
|
|
40
47
|
rescue
|
41
48
|
end
|
42
49
|
|
43
|
-
|
44
50
|
end
|
45
|
-
end
|
46
|
-
return return_obj
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
alias :flagtag :to_hash
|
51
|
-
alias :flagtags :to_hash
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
end
|
52
|
+
#> for persist in memory the temp memory object
|
53
|
+
(return_obj)
|
56
54
|
|
57
|
-
|
58
|
-
alias :tags :flags
|
59
|
-
alias :keys :flags
|
55
|
+
end
|
60
56
|
|
61
|
-
def sym_flags
|
62
|
-
self.flags.map { |e| e.to_s.dup.gsub!(/^-*/,'').to_sym }
|
63
57
|
end
|
64
58
|
|
65
|
-
|
59
|
+
# Mandatory arguments to long options are mandatory for short options too.
|
60
|
+
def short_options
|
61
|
+
self.select{ |e| e =~ /^-\w+$/ }.reduce([]){|m,o| o.scan(/\w+/).each{|e| m += e.split('') } ;m}
|
62
|
+
end;alias short_option_flags short_options
|
66
63
|
|
67
|
-
|
68
|
-
|
64
|
+
def long_options
|
65
|
+
self.select{ |e| e =~ /^--.*$/ }.reduce([]){|m,o| o.scan(/^--(.*)$/).each{|e| m += e } ;m}
|
66
|
+
end;alias long_option_flags long_options
|
69
67
|
|
70
|
-
|
71
|
-
|
68
|
+
def options
|
69
|
+
short_options + long_options
|
70
|
+
end;alias option_flags options
|
72
71
|
|
73
72
|
def values
|
74
|
-
self.select { |e| !e[0]
|
75
|
-
end
|
73
|
+
self.select { |e| !(e[0] =~ /^-/) }
|
74
|
+
end;alias arguments values
|
76
75
|
|
77
76
|
end
|
78
|
-
|
79
77
|
end
|
80
|
-
ARGV.__send__ :extend,ARGVEXT::EXTENSION
|
78
|
+
ARGV.__send__ :extend,ARGVEXT::EXTENSION
|
79
|
+
|
80
|
+
# puts ARGV.respond_to?(:options)
|
81
|
+
# puts ARGV.options.inspect
|
82
|
+
# puts ARGV.values.inspect
|
83
|
+
# puts ARGV.to_hash(true)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: argv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,8 +38,9 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description: " ARGV object extension for
|
42
|
-
|
41
|
+
description: " ARGV object extension for ease of use of options and else terminal
|
42
|
+
inputs. It can parse ARGV input into Hash object so you can work with ease, check
|
43
|
+
GIT! "
|
43
44
|
email:
|
44
45
|
- adamluzsi@gmail.com
|
45
46
|
executables: []
|
@@ -81,5 +82,6 @@ rubyforge_project:
|
|
81
82
|
rubygems_version: 2.2.2
|
82
83
|
signing_key:
|
83
84
|
specification_version: 4
|
84
|
-
summary: argv object extension for parsing
|
85
|
+
summary: argv object extension for parsing terminal inputs as arguments/options
|
85
86
|
test_files: []
|
87
|
+
has_rdoc:
|