argv 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b116f21bd831b039d26501cb7d02b0e0829fd40
4
- data.tar.gz: 9aba04ed33004f747b5d7659cd5e001dcc04edd6
3
+ metadata.gz: e2e1efaef4c65483411bb74d6463747ea4840cad
4
+ data.tar.gz: 0598d8b804c0c981439a8eb3c387adf7986e2fde
5
5
  SHA512:
6
- metadata.gz: 101c45711c3388f4de7b207a7c36bc2f0c10fee3bff3316f3b710a5cb156b9876c4289ca43c41ca8549f7f9fa3318fc54646b831defc97a2b19c3ce5186569c6
7
- data.tar.gz: d1437ce61e3560a01d564cba0ea7ef9f4722aea4420ce631de6b1a2cadd65b321b3966e34ece6730857310549706b7b5bbd6155202261ad4e05b7f94d20fd2de
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
- puts ARGV.to_hash
16
- # {"--test"=>"test"}
15
+ ARGV.to_hash
16
+ #> {"test"=>"test","zsa"=>"yo"}
17
17
 
18
- puts ARGV.to_hash( multi_value: true )
19
- # {"--test"=>["test", "this", "ok"]}
18
+ ARGV.to_hash(multi_value: true)
19
+ ARGV.to_hash(true)
20
+ #> {"test"=>["test", "this", "ok"],"zsa"=>["yo"]}
20
21
 
21
- puts ARGV.to_hash( sym_key: true )
22
- # {:test=>"test"}
22
+ ARGV.values
23
+ #> ["test", "this", "ok", "yo"]
23
24
 
24
- puts ARGV.to_hash( s: true, m: true )
25
- # {:test=>["test", "this", "ok"]}
25
+ ARGV.long_options
26
+ #> ["test"]
26
27
 
27
- puts ARGV.values.inspect
28
- # ["test", "this", "ok"]
28
+ ARGV.short_options
29
+ #> ["x","z","s","a"]
29
30
 
30
- puts ARGV.keys.inspect
31
- # ["--test"]
31
+ ARGV.options
32
+ #> ["x","z","s","a","test"]
32
33
 
33
- puts ARGV.flag_syms.inspect
34
- # [:test, :test, :this, :ok]
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
- 2.2.1
1
+ 3.0.0
@@ -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 parsing flags, inputs, so you can have easy hash objects that contain tag value, and so on :) }
11
- spec.summary = %q{ argv object extension for parsing flags, inputs }
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($/)
@@ -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 "heeeelp meeeeee~~~",:no
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
- # {"--test"=>"test"}
12
+ # {"test"=>"test"}
14
13
 
15
14
  puts "multi valued hash:",ARGV.to_hash( multi_value: true ),""
16
- # {"--test"=>["test", "this", "ok"]}
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, \"keys\":",ARGV.keys.inspect,""
28
- # ["--test"]
29
-
30
- puts "symbolized flags:",ARGV.sym_flags.inspect,""
20
+ puts "argv tags, \"options\":",ARGV.options.inspect,""
21
+ # ["test"]
@@ -1,56 +1,55 @@
1
1
  module ARGVEXT
2
2
 
3
3
  module MemoryCore
4
- class << self
5
4
 
6
- @@help_msg_obj ||= []
7
- def add_help_msg msg,*keys
8
- raise unless msg.class <= String
9
- @@help_msg_obj.push [msg,keys]
10
- return nil
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
- def help_msg
14
-
15
- helper_sym= [:helper,:help,:h]
16
- self.add_help_msg "This will show you the help msg (this)",*helper_sym
17
- unless ::ARGV.sym_flags.select{ |e| helper_sym.include?(e) }.empty?
18
-
19
- tmp_ary= []
20
- @@help_msg_obj.each{ |ary|
21
- tmp_ary.push(ary[0])
22
- ary[1].each{|e| tmp_ary.push( ( e.to_s.length == 1 ? "-#{e}" : "--#{e}" ) )}
23
- tmp_ary.push("")
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
- tmp_ary.each{ |element| element.include?('--') ? element.gsub!('--',"\t--") : element.gsub!('-',"\t -")}
27
- puts "",tmp_ary.join("\n"),""
28
- Process.exit!
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
- end
34
- end
35
-
36
- module MemoryEXT
41
+ protected
37
42
 
38
- def add_help_msg msg,*keys
39
- ::ARGVEXT::MemoryCore.add_help_msg msg,*keys
43
+ def __helper_msg_obj__
44
+ @help_msg_obj ||= []
40
45
  end
41
46
 
42
- alias :add_help :add_help_msg
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::MemoryEXT
55
+ ARGV.__send__ :extend, ARGVEXT::MemoryCore
@@ -2,79 +2,82 @@ module ARGVEXT
2
2
 
3
3
  module EXTENSION
4
4
 
5
- def to_hash opts= {}
6
- raise(ArgumentError) unless opts.class <= Hash
5
+ def to_hash(opts={})
7
6
 
8
- opts[:sym_key] ||= opts[:sk] || opts[:sym] || opts[:s]
9
- opts[:multi_value] ||= opts[:mv] || opts[:m]
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
- return_obj= {}
12
- self.count.times do |index|
13
- unless opts[:multi_value]
14
+ return (0..self.size).reduce({}) do |return_obj,index|
14
15
 
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
16
+ if !self[index+1].nil? && self[index][0] =~ /^--?/ && !(self[index+1][0] =~ /^--?/)
21
17
 
22
18
  begin
23
19
 
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
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
- def flags
54
- self.select { |e| e[0].include?('-') }
55
- end
52
+ #> for persist in memory the temp memory object
53
+ (return_obj)
56
54
 
57
- alias :flag :flags
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
- alias :sym_tags :sym_flags
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
- alias :flag_syms :sym_flags
68
- alias :flag_sym :sym_flags
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
- alias :tag_syms :sym_flags
71
- alias :key_syms :sym_flags
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].include?('-') }
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: 2.2.1
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-05-29 00:00:00.000000000 Z
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 parsing flags, inputs, so you can have easy
42
- hash objects that contain tag value, and so on :) "
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 flags, inputs
85
+ summary: argv object extension for parsing terminal inputs as arguments/options
85
86
  test_files: []
87
+ has_rdoc: