dracula 0.1.1 → 0.2.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: bf8af3fa8ce0d0773e44cf93b3fe96c801ab73c9
4
- data.tar.gz: 748c0857159a8f4f554c47c659a4ce39f481c5ac
3
+ metadata.gz: 9ef4fdc18122746e01e3149d3fdbc21d262649f0
4
+ data.tar.gz: 0a2953b9331e1488dbb11955f1fc9a9ace65a767
5
5
  SHA512:
6
- metadata.gz: b419f1370ad664f0d74954ec949817a1523928f640db92917a74d196ec4eb7b47f4ccf25aded8f760623cff0442c4e6bc67cd45167572661eec7081b38087f0e
7
- data.tar.gz: ee539053a23e58bf2de318da6418202e59135cb084c042a81711b623523813ba5b4f22337ed974e3bbcbef041aa98ae5f2a5640edee6b4213b1536f604cf5b0a
6
+ metadata.gz: 560efe1eb189a1b04726345375797e277893f401a40dff95983ab6f29f31da8fb4710746018a42c3e4d10fd3006ee7751e5473bd4d2b2f1099d8b7d63bf7acb8
7
+ data.tar.gz: 84316707c0cee5f937493abc316a67f052b51140cb46430eb6631fed9a1394b3da24a3f3ccc07b799f0cb1731eaddf9b1053dcc948a24ed03101cee0e4cb102c
data/lib/dracula.rb CHANGED
@@ -3,6 +3,7 @@ require "optparse"
3
3
 
4
4
  class Dracula
5
5
  require "dracula/command"
6
+ require "dracula/flag"
6
7
  require "dracula/namespace"
7
8
  require "dracula/ui"
8
9
 
@@ -44,7 +45,7 @@ class Dracula
44
45
 
45
46
  def option(name, params = {})
46
47
  @options ||= []
47
- @options << Command::Option.new(name, params)
48
+ @options << Dracula::Flag.new(name, params)
48
49
  end
49
50
 
50
51
  def long_desc(description)
@@ -3,46 +3,6 @@ class Dracula
3
3
 
4
4
  Desc = Struct.new(:name, :description)
5
5
 
6
- class Option < Struct.new(:name, :params)
7
-
8
- def short_name
9
- params[:aliases]
10
- end
11
-
12
- def type
13
- params[:type] || :string
14
- end
15
-
16
- def boolean?
17
- type == :boolean
18
- end
19
-
20
- def has_default_value?
21
- params.has_key?(:default) || boolean?
22
- end
23
-
24
- def default_value
25
- if boolean?
26
- params.key?(:default) ? params[:default] : false
27
- else
28
- params[:default]
29
- end
30
- end
31
-
32
- def alias_name
33
- params[:alias]
34
- end
35
-
36
- def banner
37
- if alias_name.empty?
38
- "--#{name}"
39
- else
40
- "-#{alias_name}, --#{name}"
41
- end
42
- end
43
-
44
- end
45
-
46
6
  attr_reader :method_name
47
7
  attr_reader :desc
48
8
  attr_reader :options
@@ -89,11 +49,26 @@ class Dracula
89
49
  args = params.take_while { |p| p[0] != "-" }
90
50
  flags = parse_flags(params.drop_while { |p| p[0] != "-" })
91
51
 
92
- @klass.new(flags).public_send(method_name, *args)
52
+ missing_flags = missing_required_flags(flags)
53
+
54
+ if missing_flags.empty?
55
+ @klass.new(flags).public_send(method_name, *args)
56
+ else
57
+ puts "Required Parameter: --#{missing_flags.first.name}"
58
+ puts ""
59
+ help
60
+ exit(1)
61
+ end
93
62
  end
94
63
 
95
64
  private
96
65
 
66
+ def missing_required_flags(parsed_flags)
67
+ flags.select(&:required?).reject do |flag|
68
+ parsed_flags.keys.include?(flag.name)
69
+ end
70
+ end
71
+
97
72
  def parse_flags(args)
98
73
  parsed_flags = {}
99
74
 
data/lib/dracula/flag.rb CHANGED
@@ -1,15 +1,12 @@
1
- module Dracula
2
- class Flag
3
- attr_reader :name
4
- attr_reader :short_name
5
- attr_reader :type
6
-
7
- def initialize(name, params)
8
- @name = name
9
- @params = params
10
-
11
- @short_name = params.fetch(:short)
12
- @type = params.fetch(:type, :string)
1
+ class Dracula
2
+ class Flag < Struct.new(:name, :params)
3
+
4
+ def type
5
+ params[:type] || :string
6
+ end
7
+
8
+ def required?
9
+ params[:required] == true
13
10
  end
14
11
 
15
12
  def boolean?
@@ -17,15 +14,30 @@ module Dracula
17
14
  end
18
15
 
19
16
  def has_default_value?
20
- @params.key?(:default_value) || boolean?
17
+ params.has_key?(:default) || boolean?
21
18
  end
22
19
 
23
20
  def default_value
24
- if type == :boolean
25
- @params.key?(:default_value) || false
21
+ if boolean?
22
+ params.key?(:default) ? params[:default] : false
26
23
  else
27
- @default_value
24
+ params[:default]
28
25
  end
29
26
  end
27
+
28
+ def alias_name
29
+ params[:alias]
30
+ end
31
+
32
+ alias_method :short_name, :alias_name
33
+
34
+ def banner
35
+ if alias_name.nil?
36
+ "--#{name}"
37
+ else
38
+ "-#{alias_name}, --#{name}"
39
+ end
40
+ end
41
+
30
42
  end
31
43
  end
@@ -1,3 +1,3 @@
1
1
  class Dracula
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dracula
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Šarčević
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-18 00:00:00.000000000 Z
11
+ date: 2017-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler