kommand 0.0.1 → 0.0.2

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: e728a399b1bbe6ae3294871c2f3186f6e866c675
4
- data.tar.gz: 692bbc295236b95010345f8638aae5ed036506f4
3
+ metadata.gz: 9d5b05ccf5a4e09c7056dec6cd72d62e5134c30d
4
+ data.tar.gz: 654cec67411b5d2c44b01d3937beb5491c0ee331
5
5
  SHA512:
6
- metadata.gz: 2fb412fdc3e080cbc595c7c455fcba65fb68cef6de77c5c7f58b7f45228552cdfeabf2f95a7591b082f8eef4fd94136278ed9eb69edb2ce1655f18396bf84e4f
7
- data.tar.gz: 841eb474194955c825122df2a6d97871563f456403c9e0bd60e3f1bf90b6e4cfca50411f378b6f8133b8db8067f6ccaaf82ab37c834d98097ce69fd604cc003d
6
+ metadata.gz: 83ac028e2305ebe6c59545c53a7393eb6b80baf43e11c83e475bf995f3bc5c4eed3174880db926160000898a15b67595aaf5b439b8b9d617f7d22afef8667728
7
+ data.tar.gz: 6dd15c9de3386c659d85f4dc750df61f393e50918f02b7f9f9fe00d3704be5b1adc033215dff8678cd27cffaca139e03565fa773e76f4eed452b8da254fa48a1
@@ -9,17 +9,40 @@ module Kommand
9
9
  end
10
10
 
11
11
  def key
12
- @keys.is_a?(Array) ? @keys[0] : @keys
12
+ if unnamed?
13
+ value
14
+ else
15
+ @keys.is_a?(Array) ? @keys[0] : @keys
16
+ end
13
17
  end
14
18
 
15
19
  def keys
16
- @keys.is_a?(Array) ? @keys : [@keys]
20
+ if unnamed?
21
+ [value]
22
+ else
23
+ @keys.is_a?(Array) ? @keys : [@keys]
24
+ end
17
25
  end
18
26
 
19
27
  def name
20
28
  keys.sort { |a,b| a.length <=> b.length }.last.gsub(/^--/, '')
21
29
  end
22
30
 
31
+ # is this an unnamed argument?
32
+ def unnamed?
33
+ @keys == nil
34
+ end
35
+
36
+ # is this argument valid?
37
+ def valid?
38
+ !((validate? && !@valid.include?(val)) && (!val.nil? && !val.empty?))
39
+ end
40
+
41
+ # should this argument validate?
42
+ def validate?
43
+ !@valid.nil?
44
+ end
45
+
23
46
  protected
24
47
 
25
48
  def initialize(keys, *args)
@@ -1,7 +1,29 @@
1
1
  module Kommand
2
2
  module Scripts
3
3
  class Arguments < Array
4
-
4
+
5
+ def named
6
+ args = self.class.new
7
+ args.concat(select { |arg| !arg.unnamed? })
8
+ end
9
+
10
+ def unnamed
11
+ args = self.class.new
12
+ args.concat(select { |arg| arg.unnamed? })
13
+ end
14
+
15
+ def arg(key)
16
+ named.select { |arg| arg.name.to_s == key.to_s }.first
17
+ end
18
+
19
+ def arg?(key)
20
+ !arg(key).nil?
21
+ end
22
+
23
+ def get(key)
24
+ arg?(key) ? arg(key).value : nil
25
+ end
26
+
5
27
  def to_s
6
28
  map do |arg|
7
29
  #if arg.valid.nil? || arg.valid.empty?
@@ -55,21 +55,32 @@ module Kommand
55
55
  arguments
56
56
  end
57
57
 
58
+ # reset arguments to the provided args
58
59
  def set_arguments(*args)
59
60
  @arguments = Arguments.new
60
61
  add_arguments(*args)
61
62
  end
62
63
 
64
+ # add arguments to existing args
63
65
  def add_arguments(*args)
64
66
  @arguments ||= Arguments.new
65
67
  args.each_with_index do |arg,a|
66
68
  argk, argv = *parse_arg(arg)
67
69
 
70
+ # if the argument is not a known flag, is not a valid argument and the last argument was
71
+ # a known flag, this must be a value for the last argument
68
72
  if !argument_flag?(arg) && !valid_argument?(argk) && (a > 0 && argument_flag?(args[a-1]))
69
73
  @arguments.last.value = arg
70
74
  next
71
75
  end
72
76
 
77
+ # swap key/val to create an unnamed arg - if next argument is a valid flag we must be
78
+ # a standalone argument
79
+ if a < args.length && (argv.nil? || argv.empty?) && !Commands::exists?(arg) && valid_argument?(args[a+1])
80
+ argv = argk
81
+ argk = nil
82
+ end
83
+
73
84
  raise InvalidArgument, "Invalid Argument: #{arg}" if validate_arguments? && !valid_argument?(argk)
74
85
  @arguments << argument_object(argk)
75
86
  @arguments.last.value = argv unless argv.empty?
@@ -79,7 +90,7 @@ module Kommand
79
90
 
80
91
  def parse_arg(arg)
81
92
  arga = arg.split("=")
82
- [arga.slice!(0), arga.join("=")]
93
+ [arga.slice!(0), arga.join("=")] # rejoin with '=' in case there was an '=' in the passed value
83
94
  end
84
95
 
85
96
  def valid_argument?(key)
@@ -1,3 +1,3 @@
1
1
  module Kommand
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kommand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rebec
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-13 00:00:00.000000000 Z
11
+ date: 2015-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport