mothership 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,11 @@ class Mothership
14
14
  end
15
15
 
16
16
  class << self
17
+ # all of the defined commands
18
+ def commands
19
+ @@commands
20
+ end
21
+
17
22
  # start defining a new command with the given description
18
23
  def desc(description)
19
24
  @command = Command.new(self, description)
@@ -90,15 +90,17 @@ class Mothership
90
90
  options[:interact] = interact if interact
91
91
  options[:description] = options.delete(:desc) if options.key?(:desc)
92
92
 
93
- @flags["--#{name.to_s.gsub("_", "-")}"] = name
93
+ unless options[:hidden]
94
+ @flags["--#{name.to_s.gsub("_", "-")}"] = name
94
95
 
95
- if options[:singular]
96
- @flags["--#{options[:singular]}"] = name
97
- end
96
+ if options[:singular]
97
+ @flags["--#{options[:singular]}"] = name
98
+ end
98
99
 
99
- if aliases = options[:aliases] || options[:alias]
100
- Array(aliases).each do |a|
101
- @flags[a] = name
100
+ if aliases = options[:aliases] || options[:alias]
101
+ Array(aliases).each do |a|
102
+ @flags[a] = name
103
+ end
102
104
  end
103
105
  end
104
106
 
@@ -1,4 +1,5 @@
1
1
  require "mothership/base"
2
+ require "mothership/help/commands"
2
3
 
3
4
  module Mothership::Help
4
5
  @@groups = []
@@ -9,69 +10,6 @@ module Mothership::Help
9
10
  !@@groups.empty?
10
11
  end
11
12
 
12
- def print_help_groups(global = nil, all = false)
13
- @@groups.each do |commands|
14
- print_help_group(commands, all)
15
- end
16
-
17
- command_options(global)
18
- end
19
-
20
- def unique_commands(commands)
21
- uniq_commands = []
22
- cmd_index = {}
23
-
24
- commands.each do |cmd|
25
- if idx = cmd_index[cmd.name]
26
- uniq_commands[idx] = cmd
27
- else
28
- cmd_index[cmd.name] = uniq_commands.size
29
- uniq_commands << cmd
30
- end
31
- end
32
-
33
- uniq_commands
34
- end
35
-
36
- def print_help_group(group, all = false, indent = 0)
37
- return if nothing_printable?(group, all)
38
-
39
- members = group[:members]
40
-
41
- unless all
42
- members = members.reject do |_, opts|
43
- opts[:hidden]
44
- end
45
- end
46
-
47
- commands = members.collect(&:first)
48
-
49
- i = " " * indent
50
-
51
- print i
52
- puts group[:description]
53
-
54
- commands = unique_commands(commands)
55
-
56
- width = 0
57
- commands.each do |cmd|
58
- len = cmd.usage.size
59
- if len > width
60
- width = len
61
- end
62
- end
63
-
64
- commands.each do |cmd|
65
- puts "#{i} #{cmd.usage.ljust(width)}\t#{cmd.description}"
66
- end
67
-
68
- puts "" unless commands.empty?
69
-
70
- group[:children].each do |group|
71
- print_help_group(group, all, indent + 1)
72
- end
73
- end
74
-
75
13
  # define help groups
76
14
  def groups(*tree)
77
15
  tree.each do |*args|
@@ -79,119 +17,33 @@ module Mothership::Help
79
17
  end
80
18
  end
81
19
 
82
- def add_to_group(command, names, options)
83
- where = @@tree
84
- top = true
85
- names.each do |n|
86
- where = where[:children] unless top
87
- break unless where
88
-
89
- where = where[n]
90
- break unless where
91
-
92
- top = false
93
- end
94
-
95
- unless where
96
- raise "unknown help group: #{names.join("/")}"
97
- end
98
-
99
- where[:members] << [command, options]
100
- end
101
-
102
- def basic_help(commands, global)
103
- puts "Commands:"
104
-
105
- width = 0
106
- commands.each do |_, c|
107
- len = c.usage.size
108
- width = len if len > width
109
- end
110
-
111
- commands.each do |_, c|
112
- puts " #{c.usage.ljust(width)}\t#{c.description}"
113
- end
114
-
115
- unless global.flags.empty?
116
- puts ""
117
- command_options(global)
20
+ def group(*names)
21
+ if where = find_group(names, @@tree)
22
+ where[:members].collect(&:first)
23
+ else
24
+ []
118
25
  end
119
26
  end
120
27
 
121
- def command_help(cmd)
122
- puts cmd.description
123
- puts ""
124
- command_usage(cmd)
125
- end
126
-
127
- def command_usage(cmd, io = $stdout)
128
- io.puts "Usage: #{cmd.usage}"
129
-
130
- unless cmd.flags.empty?
131
- io.puts ""
132
- command_options(cmd, io)
133
- end
28
+ def add_to_group(command, names, options)
29
+ where = find_group(names, @@tree)
30
+ raise "unknown help group: #{names.join("/")}" unless where
31
+
32
+ where[:members] << [command, options]
134
33
  end
135
34
 
136
- def command_options(cmd, io = $stdout)
137
- io.puts "Options:"
138
-
139
- rev_flags = Hash.new { |h, k| h[k] = [] }
140
-
141
- cmd.flags.each do |f, n|
142
- rev_flags[n] << f
143
- end
144
-
145
- usages = []
146
-
147
- max_width = 0
148
- rev_flags.collect do |name, fs|
149
- info = cmd.inputs[name]
150
-
151
- flag = name.to_s.gsub("_", "-")
152
-
153
- # move full form to the front
154
- fs.unshift fs.delete("--#{flag}")
155
-
156
- if short = fs.find { |x| x =~ /^-.$/ }
157
- fs.delete short
158
- end
159
-
160
- if info[:type] == :boolean && info[:default]
161
- fs[0] = "--[no-]#{flag}"
162
- end
163
-
164
- if info.key?(:default) && info.key?(:interact)
165
- fs.unshift "--ask-#{flag}"
166
- end
167
-
168
- usage = "#{short ? short + "," : " "} #{fs.join ", "}"
169
-
170
- unless info[:type] == :boolean
171
- usage << " #{(info[:value] || name).to_s.upcase}"
172
- end
173
-
174
- max_width = usage.size if usage.size > max_width
175
-
176
- usages << [usage, info[:description]]
177
- end
35
+ private
178
36
 
179
- usages.sort! { |a, b| a.first <=> b.first }
37
+ def find_group(names, where)
38
+ names.each_with_index do |n, index|
39
+ where = where[:children] unless index == 0
40
+ break unless where
180
41
 
181
- usages.each do |u, d|
182
- if d
183
- io.puts " #{u.ljust(max_width)} #{d}"
184
- else
185
- io.puts " #{u}"
186
- end
42
+ where = where[n]
43
+ break unless where
187
44
  end
188
- end
189
-
190
- private
191
45
 
192
- def nothing_printable?(group, all = false)
193
- group[:members].reject { |_, opts| !all && opts[:hidden] }.empty? &&
194
- group[:children].all? { |g| nothing_printable?(g) }
46
+ where
195
47
  end
196
48
 
197
49
  def add_group(groups, tree, name, desc, *subs)
@@ -210,51 +62,3 @@ module Mothership::Help
210
62
  end
211
63
  end
212
64
  end
213
-
214
- class Mothership
215
- class << self
216
- # add command to help group
217
- def group(*names)
218
- options =
219
- if names.last.is_a? Hash
220
- names.pop
221
- else
222
- {}
223
- end
224
-
225
- Mothership::Help.add_to_group(@command, names, options)
226
- end
227
- end
228
-
229
- def default_action
230
- invoke :help
231
- end
232
-
233
- def unknown_command(name)
234
- $stderr.print "Unknown command '#{name.to_s.gsub("_", "-")}'. "
235
- $stderr.puts "See 'help' for available commands."
236
- exit_status 1
237
- end
238
-
239
- desc "Help!"
240
- input :command, :argument => :optional
241
- input :all, :type => :boolean
242
- def help
243
- if name = input[:command]
244
- if cmd = @@commands[name.gsub("-", "_").to_sym]
245
- Mothership::Help.command_help(cmd)
246
- else
247
- unknown_command(name)
248
- end
249
- elsif Help.has_groups?
250
- unless input[:all]
251
- puts "Showing basic command set. Pass --all to list all commands."
252
- puts ""
253
- end
254
-
255
- Mothership::Help.print_help_groups(@@global, input[:all])
256
- else
257
- Mothership::Help.basic_help(@@commands, @@global)
258
- end
259
- end
260
- end
@@ -0,0 +1,49 @@
1
+ require "mothership/help/printer"
2
+
3
+ class Mothership
4
+ class << self
5
+ # add command to help group
6
+ def group(*names)
7
+ options =
8
+ if names.last.is_a? Hash
9
+ names.pop
10
+ else
11
+ {}
12
+ end
13
+
14
+ Mothership::Help.add_to_group(@command, names, options)
15
+ end
16
+ end
17
+
18
+ def default_action
19
+ invoke :help
20
+ end
21
+
22
+ def unknown_command(name)
23
+ $stderr.print "Unknown command '#{name.to_s.gsub("_", "-")}'. "
24
+ $stderr.puts "See 'help' for available commands."
25
+ exit_status 1
26
+ end
27
+
28
+ desc "Help!"
29
+ input :command, :argument => :optional
30
+ input :all, :type => :boolean
31
+ def help
32
+ if name = input[:command]
33
+ if cmd = @@commands[name.gsub("-", "_").to_sym]
34
+ Mothership::Help.command_help(cmd)
35
+ else
36
+ unknown_command(name)
37
+ end
38
+ elsif Help.has_groups?
39
+ unless input[:all]
40
+ puts "Showing basic command set. Pass --all to list all commands."
41
+ puts ""
42
+ end
43
+
44
+ Mothership::Help.print_help_groups(@@global, input[:all])
45
+ else
46
+ Mothership::Help.basic_help(@@commands, @@global)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,162 @@
1
+ module Mothership::Help
2
+ class << self
3
+ def print_help_groups(global = nil, all = false)
4
+ @@groups.each do |commands|
5
+ print_help_group(commands, all)
6
+ end
7
+
8
+ command_options(global)
9
+ end
10
+
11
+ def print_help_group(group, all = false, indent = 0)
12
+ return if nothing_printable?(group, all)
13
+
14
+ members = group[:members]
15
+
16
+ unless all
17
+ members = members.reject do |_, opts|
18
+ opts[:hidden]
19
+ end
20
+ end
21
+
22
+ commands = members.collect(&:first)
23
+
24
+ i = " " * indent
25
+
26
+ print i
27
+ puts group[:description]
28
+
29
+ commands = unique_commands(commands)
30
+
31
+ width = 0
32
+ commands.each do |cmd|
33
+ len = cmd.usage.size
34
+ if len > width
35
+ width = len
36
+ end
37
+ end
38
+
39
+ commands.each do |cmd|
40
+ puts "#{i} #{cmd.usage.ljust(width)}\t#{cmd.description}"
41
+ end
42
+
43
+ puts "" unless commands.empty?
44
+
45
+ group[:children].each do |group|
46
+ print_help_group(group, all, indent + 1)
47
+ end
48
+ end
49
+
50
+ def basic_help(commands, global)
51
+ puts "Commands:"
52
+
53
+ width = 0
54
+ commands.each do |_, c|
55
+ len = c.usage.size
56
+ width = len if len > width
57
+ end
58
+
59
+ commands.each do |_, c|
60
+ puts " #{c.usage.ljust(width)}\t#{c.description}"
61
+ end
62
+
63
+ unless global.flags.empty?
64
+ puts ""
65
+ command_options(global)
66
+ end
67
+ end
68
+
69
+ def command_help(cmd)
70
+ puts cmd.description
71
+ puts ""
72
+ command_usage(cmd)
73
+ end
74
+
75
+ def command_usage(cmd, io = $stdout)
76
+ io.puts "Usage: #{cmd.usage}"
77
+
78
+ unless cmd.flags.empty?
79
+ io.puts ""
80
+ command_options(cmd, io)
81
+ end
82
+ end
83
+
84
+ def command_options(cmd, io = $stdout)
85
+ io.puts "Options:"
86
+
87
+ rev_flags = Hash.new { |h, k| h[k] = [] }
88
+
89
+ cmd.flags.each do |f, n|
90
+ rev_flags[n] << f
91
+ end
92
+
93
+ usages = []
94
+
95
+ max_width = 0
96
+ rev_flags.collect do |name, fs|
97
+ info = cmd.inputs[name]
98
+ next if info[:hidden]
99
+
100
+ flag = name.to_s.gsub("_", "-")
101
+
102
+ # move full form to the front
103
+ fs.unshift fs.delete("--#{flag}")
104
+
105
+ if short = fs.find { |x| x =~ /^-.$/ }
106
+ fs.delete short
107
+ end
108
+
109
+ if info[:type] == :boolean && info[:default]
110
+ fs[0] = "--[no-]#{flag}"
111
+ end
112
+
113
+ if info.key?(:default) && info.key?(:interact)
114
+ fs.unshift "--ask-#{flag}"
115
+ end
116
+
117
+ usage = "#{short ? short + "," : " "} #{fs.join ", "}"
118
+
119
+ unless info[:type] == :boolean
120
+ usage << " #{(info[:value] || name).to_s.upcase}"
121
+ end
122
+
123
+ max_width = usage.size if usage.size > max_width
124
+
125
+ usages << [usage, info[:description]]
126
+ end
127
+
128
+ usages.sort! { |a, b| a.first <=> b.first }
129
+
130
+ usages.each do |u, d|
131
+ if d
132
+ io.puts " #{u.ljust(max_width)} #{d}"
133
+ else
134
+ io.puts " #{u}"
135
+ end
136
+ end
137
+ end
138
+
139
+ private
140
+
141
+ def nothing_printable?(group, all = false)
142
+ group[:members].reject { |_, opts| !all && opts[:hidden] }.empty? &&
143
+ group[:children].all? { |g| nothing_printable?(g) }
144
+ end
145
+
146
+ def unique_commands(commands)
147
+ uniq_commands = []
148
+ cmd_index = {}
149
+
150
+ commands.each do |cmd|
151
+ if idx = cmd_index[cmd.name]
152
+ uniq_commands[idx] = cmd
153
+ else
154
+ cmd_index[cmd.name] = uniq_commands.size
155
+ uniq_commands << cmd
156
+ end
157
+ end
158
+
159
+ uniq_commands
160
+ end
161
+ end
162
+ end
@@ -1,3 +1,3 @@
1
1
  class Mothership
2
- VERSION = "0.2.5"
2
+ VERSION = "0.3.0"
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: 29
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 5
10
- version: 0.2.5
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-11-19 00:00:00 -08:00
19
- default_executable:
18
+ date: 2012-11-28 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: rake
@@ -59,23 +58,22 @@ extra_rdoc_files: []
59
58
  files:
60
59
  - LICENSE
61
60
  - Rakefile
62
- - lib/mothership.rb
63
- - lib/mothership/version.rb
61
+ - lib/mothership/base.rb
64
62
  - lib/mothership/callbacks.rb
65
- - lib/mothership/inputs.rb
66
- - lib/mothership/pretty.rb
67
- - lib/mothership/errors.rb
68
63
  - lib/mothership/command.rb
69
- - lib/mothership/base.rb
64
+ - lib/mothership/errors.rb
65
+ - lib/mothership/help/commands.rb
66
+ - lib/mothership/help/printer.rb
70
67
  - lib/mothership/help.rb
68
+ - lib/mothership/inputs.rb
71
69
  - lib/mothership/parser.rb
72
- - lib/mothership/progress.rb
73
- - spec/helpers.rb
70
+ - lib/mothership/version.rb
71
+ - lib/mothership.rb
74
72
  - spec/arguments_spec.rb
75
73
  - spec/combination_spec.rb
76
- - spec/Rakefile
77
74
  - spec/flags_spec.rb
78
- has_rdoc: true
75
+ - spec/helpers.rb
76
+ - spec/Rakefile
79
77
  homepage: https://github.com/vito/mothership
80
78
  licenses: []
81
79
 
@@ -105,13 +103,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
103
  requirements: []
106
104
 
107
105
  rubyforge_project: mothership
108
- rubygems_version: 1.6.2
106
+ rubygems_version: 1.8.24
109
107
  signing_key:
110
108
  specification_version: 3
111
109
  summary: Command-line library for big honkin' CLI apps.
112
110
  test_files:
113
- - spec/helpers.rb
114
111
  - spec/arguments_spec.rb
115
112
  - spec/combination_spec.rb
116
- - spec/Rakefile
117
113
  - spec/flags_spec.rb
114
+ - spec/helpers.rb
115
+ - spec/Rakefile
@@ -1,91 +0,0 @@
1
- require "rbconfig"
2
-
3
- # Mix in to your Mothership class to enable user-toggleable colors.
4
- #
5
- # Redefine color_enabled? to control color enabling/disabling. Colors will be
6
- # auto-disabled if the platform is Windows or if $stdout is not a tty.
7
- #
8
- # Redefine user_colors to return a hash from tags to color, e.g. from a user's
9
- # color config file.
10
- module Mothership::Pretty
11
- WINDOWS = !!(RbConfig::CONFIG['host_os'] =~ /mingw|mswin32|cygwin/)
12
-
13
- COLOR_CODES = {
14
- :black => 0,
15
- :red => 1,
16
- :green => 2,
17
- :yellow => 3,
18
- :blue => 4,
19
- :magenta => 5,
20
- :cyan => 6,
21
- :white => 7
22
- }
23
-
24
- DEFAULT_COLORS = {
25
- :name => :blue,
26
- :neutral => :blue,
27
- :good => :green,
28
- :bad => :red,
29
- :error => :magenta,
30
- :unknown => :cyan,
31
- :warning => :yellow,
32
- :instance => :yellow,
33
- :number => :green,
34
- :prompt => :blue,
35
- :yes => :green,
36
- :no => :red
37
- }
38
-
39
- private
40
-
41
- # override with e.g. option(:color), or whatever toggle you use
42
- def color_enabled?
43
- true
44
- end
45
-
46
- # use colors?
47
- def color?
48
- color_enabled? && !WINDOWS && $stdout.tty?
49
- end
50
-
51
- # redefine to control the tag -> color settings
52
- def user_colors
53
- DEFAULT_COLORS
54
- end
55
-
56
- # colored text
57
- #
58
- # shouldn't use bright colors, as some color themes abuse
59
- # the bright palette (I'm looking at you, Solarized)
60
- def c(str, type)
61
- return str unless color?
62
-
63
- bright = false
64
- color = user_colors[type]
65
- if color.to_s =~ /bright-(.+)/
66
- bright = true
67
- color = $1.to_sym
68
- end
69
-
70
- return str unless color
71
-
72
- code = "\e[#{bright ? 9 : 3}#{COLOR_CODES[color]}m"
73
- "#{code}#{str.to_s.gsub("\e[0m", "\e[0m#{code}")}\e[0m"
74
- end
75
-
76
- # bold text
77
- def b(str)
78
- return str unless color?
79
-
80
- code = "\e[1m"
81
- "#{code}#{str.to_s.gsub("\e[0m", "\e[0m#{code}")}\e[0m"
82
- end
83
-
84
- # dim text
85
- def d(str)
86
- return str unless color?
87
-
88
- code = "\e[2m"
89
- "#{code}#{str.to_s.gsub("\e[0m", "\e[0m#{code}")}\e[0m"
90
- end
91
- end
@@ -1,112 +0,0 @@
1
- require "mothership/pretty"
2
-
3
- module Mothership::Progress
4
- include Mothership::Pretty
5
-
6
- module Dots
7
- class << self
8
- DOT_COUNT = 3
9
- DOT_TICK = 0.15
10
-
11
- def start!
12
- @dots ||=
13
- Thread.new do
14
- before_sync = $stdout.sync
15
-
16
- $stdout.sync = true
17
-
18
- printed = false
19
- i = 1
20
- until @stop_dots
21
- if printed
22
- print "\b" * DOT_COUNT
23
- end
24
-
25
- print ("." * i).ljust(DOT_COUNT)
26
- printed = true
27
-
28
- if i == DOT_COUNT
29
- i = 0
30
- else
31
- i += 1
32
- end
33
-
34
- sleep DOT_TICK
35
- end
36
-
37
- if printed
38
- print "\b" * DOT_COUNT
39
- print " " * DOT_COUNT
40
- print "\b" * DOT_COUNT
41
- end
42
-
43
- $stdout.sync = before_sync
44
- @stop_dots = nil
45
- end
46
- end
47
-
48
- def stop!
49
- return unless @dots
50
- return if @stop_dots
51
- @stop_dots = true
52
- @dots.join
53
- @dots = nil
54
- end
55
- end
56
- end
57
-
58
- class Skipper
59
- def initialize(&ret)
60
- @return = ret
61
- end
62
-
63
- def skip(&callback)
64
- @return.call("SKIPPED", :warning, callback)
65
- end
66
-
67
- def give_up(&callback)
68
- @return.call("GAVE UP", :bad, callback)
69
- end
70
-
71
- def fail(&callback)
72
- @return.call("FAILED", :error, callback)
73
- end
74
- end
75
-
76
- # override to determine whether to show progress
77
- def quiet?
78
- false
79
- end
80
-
81
- def with_progress(message)
82
- unless quiet?
83
- print message
84
- Dots.start!
85
- end
86
-
87
- skipper = Skipper.new do |status, color, callback|
88
- unless quiet?
89
- Dots.stop!
90
- puts "... #{c(status, color)}"
91
- end
92
-
93
- return callback && callback.call
94
- end
95
-
96
- begin
97
- res = yield skipper
98
- unless quiet?
99
- Dots.stop!
100
- puts "... #{c("OK", :good)}"
101
- end
102
- res
103
- rescue
104
- unless quiet?
105
- Dots.stop!
106
- puts "... #{c("FAILED", :error)}"
107
- end
108
-
109
- raise
110
- end
111
- end
112
- end