mothership 0.0.2 → 0.0.3

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.
data/lib/mothership.rb CHANGED
@@ -27,7 +27,7 @@ class Mothership
27
27
  # arguments and flags can be in any order; all flags will be parsed out
28
28
  # first, and the bits left over will be treated as arguments
29
29
  def start(argv)
30
- @@inputs = Inputs.new(@@global, self, {})
30
+ @@inputs = Inputs.new(@@global)
31
31
 
32
32
  name, *argv =
33
33
  Parser.new(@@global).parse_flags(
@@ -56,7 +56,7 @@ class Mothership
56
56
 
57
57
  # get value of global option
58
58
  def option(name, *args)
59
- @@inputs[name, *args]
59
+ @@inputs.get(name, self, *args)
60
60
  end
61
61
 
62
62
  # test if an option was explicitly provided
@@ -37,7 +37,7 @@ class Mothership
37
37
  str = @name.to_s.gsub("_", "-")
38
38
 
39
39
  @arguments.each do |a|
40
- name = a[:name].to_s.upcase
40
+ name = (a[:value] || a[:name]).to_s.upcase
41
41
 
42
42
  case a[:type]
43
43
  when :splat
@@ -111,7 +111,11 @@ class Mothership
111
111
 
112
112
  options[:argument] = type
113
113
 
114
- @arguments << { :name => name, :type => type }
114
+ @arguments <<
115
+ { :name => name,
116
+ :type => type,
117
+ :value => options[:value]
118
+ }
115
119
  end
116
120
 
117
121
  @inputs[name] = options
@@ -126,36 +126,30 @@ module Mothership::Help
126
126
 
127
127
  usages = []
128
128
 
129
- max_bool = 0
129
+ max_width = 0
130
130
  rev_flags.collect do |name, fs|
131
131
  info = cmd.inputs[name]
132
132
 
133
- usage =
134
- case info[:type]
135
- when :boolean
136
- fs.sort.join(", ")
137
- else
138
- fs.sort.collect { |f| "#{f} #{name.to_s.upcase}" }.join(", ")
139
- end
133
+ flag = name.to_s.gsub("_", "-")
140
134
 
141
- say_no =
142
- if info[:type] == :boolean
143
- max_bool = usage.size if usage.size > max_bool
144
- "--no-#{name.to_s.gsub("_", "-")}"
145
- end
135
+ full = fs.delete "--#{flag}"
146
136
 
147
- usages << [usage, info[:description], say_no]
148
- end
137
+ short = fs.find { |x| x =~ /^-.$/ }
138
+ fs.delete short if short
149
139
 
150
- max_width = 0
151
- usages.collect! do |usage, desc, bool_no|
152
- if bool_no
153
- usage = usage.ljust(max_bool) + " #{bool_no}"
140
+ if info[:type] == :boolean && info[:default]
141
+ full = "--[no-]#{flag}"
142
+ end
143
+
144
+ usage = "#{short ? short + "," : " "} #{([full] + fs).join ", "}"
145
+
146
+ unless info[:type] == :boolean
147
+ usage << " #{(info[:value] || name).to_s.upcase}"
154
148
  end
155
149
 
156
150
  max_width = usage.size if usage.size > max_width
157
151
 
158
- [usage, desc]
152
+ usages << [usage, info[:description]]
159
153
  end
160
154
 
161
155
  usages.sort! { |a, b| a.first <=> b.first }
@@ -2,7 +2,7 @@ class Mothership
2
2
  class Inputs
3
3
  attr_reader :inputs
4
4
 
5
- def initialize(command, context, inputs = {})
5
+ def initialize(command, context = nil, inputs = {})
6
6
  @command = command
7
7
  @context = context
8
8
  @inputs = inputs
@@ -31,7 +31,12 @@ class Mothership
31
31
  end
32
32
 
33
33
  def [](name, *args)
34
+ get(name, @context, *args)
35
+ end
36
+
37
+ def get(name, context, *args)
34
38
  return @inputs[name] if @inputs.key?(name) && @inputs[name] != []
39
+
35
40
  return @cache[name] if @cache.key? name
36
41
 
37
42
  meta = @command.inputs[name]
@@ -40,7 +45,11 @@ class Mothership
40
45
 
41
46
  val =
42
47
  if meta[:default].respond_to? :to_proc
43
- @context.instance_exec(*args, &meta[:default])
48
+ unless context
49
+ raise "no context for input request"
50
+ end
51
+
52
+ context.instance_exec(*args, &meta[:default])
44
53
  elsif meta[:default]
45
54
  meta[:default]
46
55
  elsif meta[:type] == :boolean
@@ -1,3 +1,3 @@
1
1
  class Mothership
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mothership
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-02 00:00:00 Z
18
+ date: 2012-07-03 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake