minimist 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd39c1402887988b0bcb8d279593aafba55747bd
4
- data.tar.gz: 1a2330a09de8e4ee7697c8f20542b57cf9cf1973
3
+ metadata.gz: cf3aae651ba5bbb43adc67d69864814b4c67beaf
4
+ data.tar.gz: 83f42f940f7494e05fc56e5aa6c28c5cc4371246
5
5
  SHA512:
6
- metadata.gz: f556665d8a97febe892b789edde1c61587d6a709a4fd8b88265d4629a6c95abcab72b90b883a1d22841393d8696b0a32f4b9e4aeaefca1568fa5278e634e2b31
7
- data.tar.gz: d79b42e39397534087b996095bc9e40840175c2e5700c2b81ca49fb6ab3613f77fa99ec912e0f68f4dad0e92640f56d448b39014cb9d33259ff6b49b28b3cf38
6
+ metadata.gz: b83242e0bb2609de9afc5e886f25eb2c4dd65f7afd79bd82eb69dee3806666ffdb518c860b5c1e8cb0bd732f538acc8869fe0d3cd2173f5a03b5de57c3bf78ed
7
+ data.tar.gz: ba7455ad470b90d6c3a5fa3a9cd99cad73e67b15e6a5f5a10a5c1d3981b47ca1c8e7a0d3182546046f99448e54db774061cd599396c645cdb7b06acab6af2a4b
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in minimist.gemspec
4
4
  gemspec
5
+
6
+ gem 'pry'
@@ -7,7 +7,7 @@ require "minimist"
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
10
+ require "pry"
11
11
  # Pry.start
12
12
 
13
13
  require "irb"
@@ -1,5 +1,120 @@
1
+ require "pry"
2
+
1
3
  require "minimist/version"
4
+ require "minimist/errors"
2
5
 
3
6
  module Minimist
4
- # Your code goes here...
7
+ class << self
8
+ REGEX = {
9
+ command: /^[A-Za-z]/,
10
+ double_dash: /^--.+/,
11
+ double_dash_negation: /^--no-.+/,
12
+ single_dash: /^-[^-]+/
13
+ }
14
+
15
+ #
16
+ # Accepts an array and returns a hash
17
+ # with actions and options passed
18
+ #
19
+ def parse(argv)
20
+ @argv = argv
21
+
22
+ out = {
23
+ commands: [],
24
+ options: {}
25
+ }
26
+ skip = false
27
+ type = nil
28
+
29
+ argv.each_with_index do |arg, i|
30
+ # reset
31
+ type = nil
32
+
33
+ if skip
34
+ skip = false
35
+ next
36
+ end
37
+
38
+ REGEX.each do |name, regex|
39
+ match = arg =~ regex
40
+
41
+ if match
42
+ type = name
43
+ break
44
+ end
45
+ end
46
+
47
+ #
48
+ # arg type method should indicate whether it wants
49
+ # to skip the next value or not
50
+ #
51
+ out, skip = send(type, arg, i, out)
52
+
53
+ puts "#{i} #{arg} #{type} - should skip #{skip}"
54
+ end
55
+
56
+ out
57
+ end
58
+
59
+ private
60
+
61
+ def command(arg, _, argv_object)
62
+ argv_object[:commands] << arg
63
+ [argv_object, false]
64
+ end
65
+
66
+ #
67
+ # If the next command is
68
+ #
69
+ def single_dash(arg, index, argv_object)
70
+ #
71
+ # match -abc, -n5 arg types
72
+ #
73
+ if match_data = arg.match(/^-([A-Za-z])([0-9])$/)
74
+ argv_object[:options][match_data[1].to_sym] = match_data[2]
75
+ elsif @argv[index + 1] =~ /^(\d|[A-Za-z])/
76
+ argv_object[:options][arg.slice(1..-1).to_sym] = try_convert(@argv[index + 1])
77
+ should_skip = true
78
+ else
79
+ #
80
+ # loop through and apply each letter to the
81
+ # options object
82
+ #
83
+ arg.slice(1..-1).split('').each do |letter|
84
+ argv_object[:options][letter.to_sym] = true
85
+ end
86
+ end
87
+
88
+ [argv_object, should_skip]
89
+ end
90
+
91
+ def double_dash(arg, index, argv_object)
92
+ #
93
+ # match --skip=true arg types
94
+ #
95
+ match_data = arg.match(/^--([A-Za-z]*-?[A-Za-z]?*)=?([A-Za-z0-9]*)/)
96
+
97
+ if !match_data[2].empty?
98
+ argv_object[:options][match_data[1].to_sym] = try_convert(match_data[2])
99
+ else
100
+ argv_object[:options][match_data[1].to_sym] = true
101
+ end
102
+
103
+ [argv_object, false]
104
+ end
105
+
106
+ def double_dash_negation(arg, index, argv_object)
107
+ #
108
+ # regex matvch --no-val into val = true
109
+ #
110
+ match_data = arg.match(/^--no-([A-Za-z]*-?[A-Za-z]?*)/)
111
+ argv_object[:options][match_data[1].to_sym] = false
112
+
113
+ [argv_object, false]
114
+ end
115
+
116
+ def try_convert(val)
117
+ Number(val) rescue val
118
+ end
119
+ end
5
120
  end
File without changes
@@ -1,3 +1,3 @@
1
1
  module Minimist
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Connor Atherton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-16 00:00:00.000000000 Z
11
+ date: 2015-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,6 +54,7 @@ files:
54
54
  - Rakefile
55
55
  - bin/console
56
56
  - lib/minimist.rb
57
+ - lib/minimist/errors.rb
57
58
  - lib/minimist/version.rb
58
59
  - minimist.gemspec
59
60
  homepage: https://github.com/ConnorAtherton/minimist