cli_tool 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: 4420b2da040b6b7fb8f05664834fd7cc0d33cf14
4
- data.tar.gz: 3ade50f54e87003b8111a9812c6d31502c598103
3
+ metadata.gz: 17d8d9e6d914296940ccbd28dd3eabb488dc7b46
4
+ data.tar.gz: 026adfb4294b909858ab0ff98c9d3c89d4a1f53a
5
5
  SHA512:
6
- metadata.gz: 65f885c50ac8668a209e9ef99197b78de9afce9932f225503b3f82cb4a11e756fc284fb326c41c4e000379e3ece271cbe9f1367bbe5397c3613d37f01119e58e
7
- data.tar.gz: db5e0aae0a5c47c6966f241bb92b29751e5c8e240c9da7aee4d197d71a2b225a8d148e6015a562af4740a834c433279f88e93a5ea95b93bcbe464e6df08c725e
6
+ metadata.gz: 66bdb094264f87a4928d380b04e5244472dce78b85aef3c047be6400b88645e887612ba8d7abedc1958c8053a7ce246ffbe4724b47476d7234378d7f6f4e2532
7
+ data.tar.gz: 8250abdc7bbfb6d3d6bf5ee8fed267b0598d1e1dba65b205072695689a15f5e0c7897f813d07e9f53c8317cdefb73ded1db51ae36a66685d113429d94431e2ce
@@ -25,9 +25,73 @@ module CliTool
25
25
  return @@options.uniq unless opts
26
26
 
27
27
  _create_attrs_(opts)
28
+ default_options(opts)
28
29
  @@options = @@options.concat(_create_gola_(opts)).uniq
29
30
  end
30
31
 
32
+ def default_options(opts = {})
33
+ @@defaults ||= []
34
+
35
+ # If no options were passed then return
36
+ return @@defaults.uniq.inject({}) { |o,(k,v)| o[k] = v; o } unless opts
37
+
38
+ # Set the default options
39
+ opts.each do |opt, details|
40
+ next unless details.is_a?(Hash)
41
+ next unless details[:default]
42
+ opt = opt.first if opt.is_a?(Array)
43
+ @@defaults << [optionify(opt), details[:default]]
44
+ end
45
+
46
+ @@defaults = @@defaults.uniq
47
+ end
48
+
49
+ # Parse to correct option set
50
+ def optionify(option, setter = false)
51
+ option = "#{option}".gsub(/^[\-]+/, '').gsub(/(-| )/, '_')
52
+ (setter ? option + '=' : option).to_sym
53
+ end
54
+
55
+ # Handle running options
56
+ def run(entrypoint = false, *args)
57
+ if args.last.instance_of?(self)
58
+ instance = args.pop
59
+ else
60
+ instance = new
61
+ end
62
+
63
+ # Option Setter Proc
64
+ option_setter = Proc.new do |option, value|
65
+ value = case value
66
+ when ''
67
+ true
68
+ when 'true'
69
+ true
70
+ when 'false'
71
+ false
72
+ else
73
+ value
74
+ end
75
+
76
+ puts "Setting @#{optionify(option)} = #{value}"
77
+ instance.__send__(optionify(option, :set), value)
78
+ end
79
+
80
+ # Set options
81
+ puts "CliTool... Loading Options..."
82
+ default_options.each(&option_setter)
83
+ GetoptLong.new(*options).each(&option_setter)
84
+ puts ''
85
+
86
+ # Handle the entrypoint
87
+ if entrypoint
88
+ entrypoint = optionify(entrypoint)
89
+ instance.__send__(entrypoint, *args)
90
+ else
91
+ instance
92
+ end
93
+ end
94
+
31
95
  private
32
96
 
33
97
  def _create_attrs_(opts)
@@ -41,10 +105,10 @@ module CliTool
41
105
  key = [key] unless key.is_a?(Array)
42
106
  key.each_with_index do |k, i|
43
107
  if i < 1
44
- default = k.to_sym
45
- attr_accessor(k.to_sym)
108
+ default = optionify(k)
109
+ attr_accessor(default)
46
110
  else
47
- define_method("#{k}=", Proc.new { |x| send(default, x) })
111
+ define_method(optionify(k, :set), Proc.new { |x| send("#{default}=", x) })
48
112
  end
49
113
  end
50
114
  end
@@ -65,12 +129,12 @@ module CliTool
65
129
  short = details[:short]
66
130
  dependency = details[:dependency]
67
131
  else
68
- dependency ||= details
132
+ dependency = details
69
133
  end
70
134
 
71
135
  # Prepare the GetoptLong Array option
72
136
  golao = []
73
- golao << short if short
137
+ golao << "-#{short}" if short
74
138
  golao << GOL_MAP[dependency.to_sym]
75
139
 
76
140
  # If the option has aliases then create
@@ -78,11 +142,11 @@ module CliTool
78
142
  if opt.is_a?(Array)
79
143
  opt.each do |key|
80
144
  golaot = golao.dup
81
- golaot.unshift(key)
145
+ golaot.unshift("--#{key}")
82
146
  gola << golaot
83
147
  end
84
148
  else
85
- golao.unshift(opt)
149
+ golao.unshift("--#{opt}")
86
150
  gola << golao
87
151
  end
88
152
  end
@@ -1,3 +1,3 @@
1
1
  module CliTool
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: cli_tool
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
  - Kelly Becker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-03 00:00:00.000000000 Z
11
+ date: 2013-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,8 +94,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  version: '0'
95
95
  requirements: []
96
96
  rubyforge_project:
97
- rubygems_version: 2.0.2
97
+ rubygems_version: 2.0.3
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Tools to help with writing command line applications
101
101
  test_files: []
102
+ has_rdoc: