visionmedia-commander 1.2.2 → 2.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +85 -0
- data/Manifest +33 -0
- data/README.rdoc +178 -0
- data/Rakefile +8 -30
- data/Todo.rdoc +12 -0
- data/bin/commander +23 -26
- data/commander.gemspec +39 -0
- data/lib/commander/command.rb +227 -31
- data/lib/commander/core_ext/array.rb +24 -0
- data/lib/commander/core_ext/kernel.rb +12 -0
- data/lib/commander/core_ext/object.rb +13 -0
- data/lib/commander/core_ext/string.rb +33 -0
- data/lib/commander/core_ext.rb +5 -0
- data/lib/commander/fileutils.rb +30 -0
- data/lib/commander/help_formatters/base.rb +38 -0
- data/lib/commander/help_formatters/terminal/command_help.erb +35 -0
- data/lib/commander/help_formatters/terminal/help.erb +21 -0
- data/lib/commander/help_formatters/terminal.rb +27 -0
- data/lib/commander/help_formatters.rb +6 -0
- data/lib/commander/import.rb +28 -0
- data/lib/commander/runner.rb +219 -0
- data/lib/commander/user_interaction.rb +202 -0
- data/lib/commander/version.rb +4 -4
- data/lib/commander.rb +46 -6
- data/spec/commander_spec.rb +208 -27
- data/spec/help_formatter_spec.rb +31 -0
- data/spec/spec_helper.rb +25 -0
- data/tasks/docs.rake +13 -0
- data/tasks/gemspec.rake +3 -0
- data/tasks/spec.rake +25 -0
- metadata +65 -25
- data/History.txt +0 -48
- data/Manifest.txt +0 -17
- data/README.txt +0 -76
- data/lib/commander/commander.rb +0 -35
- data/lib/commander/help_generators/default.rb +0 -126
- data/lib/commander/help_generators.rb +0 -2
- data/lib/commander/manager.rb +0 -129
- data/spec/all_spec.rb +0 -6
- data/spec/manager_spec.rb +0 -19
@@ -1,126 +0,0 @@
|
|
1
|
-
|
2
|
-
module Commander
|
3
|
-
module HelpGenerators
|
4
|
-
|
5
|
-
# Default help generator.
|
6
|
-
#
|
7
|
-
# Example formatting:
|
8
|
-
#
|
9
|
-
# NAME:
|
10
|
-
#
|
11
|
-
# Commander-init
|
12
|
-
#
|
13
|
-
# DESCRIPTION:
|
14
|
-
#
|
15
|
-
# Initialize an empty file with a commander skeleton.
|
16
|
-
#
|
17
|
-
# EXAMPLES:
|
18
|
-
#
|
19
|
-
# # Apply commander to a blank file.
|
20
|
-
# commander init ./bin/my_executable
|
21
|
-
#
|
22
|
-
# OPTIONS:
|
23
|
-
#
|
24
|
-
# -r, --recursive, Do something recursively
|
25
|
-
# -v, --verbose, Do something verbosely
|
26
|
-
#
|
27
|
-
class Default
|
28
|
-
|
29
|
-
attr_reader :manager
|
30
|
-
|
31
|
-
def initialize(manager)
|
32
|
-
$terminal.page_at = 22
|
33
|
-
@manager = manager
|
34
|
-
@command = @manager.user_command
|
35
|
-
render
|
36
|
-
end
|
37
|
-
|
38
|
-
# -----------------------------------------------------------
|
39
|
-
|
40
|
-
protected
|
41
|
-
|
42
|
-
# -----------------------------------------------------------
|
43
|
-
|
44
|
-
def render
|
45
|
-
say(render_command(@command)) if @command
|
46
|
-
say(render_global) unless @command
|
47
|
-
end
|
48
|
-
|
49
|
-
def render_global
|
50
|
-
%w[ name description command_list copyright ].collect { |v| send("render_#{v}") }.join
|
51
|
-
end
|
52
|
-
|
53
|
-
def render_name
|
54
|
-
o = head 'name'
|
55
|
-
o += row 6, @manager.info[:name]
|
56
|
-
o += "\n"
|
57
|
-
o
|
58
|
-
end
|
59
|
-
|
60
|
-
def render_description
|
61
|
-
o = head 'description'
|
62
|
-
o += row 6, @manager.info[:description] unless @manager.info[:description].nil?
|
63
|
-
o += "\n"
|
64
|
-
o
|
65
|
-
end
|
66
|
-
|
67
|
-
def render_command_list
|
68
|
-
o = head 'sub-commands'
|
69
|
-
o += @manager.commands.collect { |c, command| row(6, c.to_s, command.description) }.join
|
70
|
-
o += row 6, "help", "Display this help information, or help for the trailing sub-command."
|
71
|
-
o += "\n"
|
72
|
-
o
|
73
|
-
end
|
74
|
-
|
75
|
-
def render_command(command)
|
76
|
-
o = head 'name'
|
77
|
-
o += row 6, "#{@manager.info[:name]}-#{command.command}"
|
78
|
-
o += "\n"
|
79
|
-
|
80
|
-
o += head 'description'
|
81
|
-
o += row 6, command.description
|
82
|
-
o += "\n"
|
83
|
-
|
84
|
-
o += head('examples') unless command.examples.empty?
|
85
|
-
o += render_command_examples command
|
86
|
-
o += "\n"
|
87
|
-
|
88
|
-
o += head('options') unless command.options.empty?
|
89
|
-
o += render_command_options command
|
90
|
-
o += "\n\n"
|
91
|
-
o
|
92
|
-
end
|
93
|
-
|
94
|
-
def render_command_options(command)
|
95
|
-
command.options.collect { |option| row(6, option[:args].join(', ')) }.join
|
96
|
-
end
|
97
|
-
|
98
|
-
def render_command_examples(command)
|
99
|
-
command.examples.collect do |example|
|
100
|
-
o = row 6, "# #{example[:description]}"
|
101
|
-
o += row 6, "#{example[:code]}"
|
102
|
-
end.join "\n"
|
103
|
-
end
|
104
|
-
|
105
|
-
def render_copyright
|
106
|
-
@manager.info[:copyright].nil? ? "\n" : head('copyright') + row(6, @manager.info[:copyright]) + "\n\n";
|
107
|
-
end
|
108
|
-
|
109
|
-
def head(text)
|
110
|
-
"\n <%= color('#{text.upcase}', BOLD) %>:\n"
|
111
|
-
end
|
112
|
-
|
113
|
-
def row(lpad, *args)
|
114
|
-
"\n" + (' ' * lpad) + args.collect { |a| a.to_s.ljust(15, ' ') }.join
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
class Hash
|
121
|
-
def collect(&block)
|
122
|
-
o = []
|
123
|
-
self.each_pair { |k, v| o << block.call(k, v)}
|
124
|
-
o
|
125
|
-
end
|
126
|
-
end
|
data/lib/commander/manager.rb
DELETED
@@ -1,129 +0,0 @@
|
|
1
|
-
|
2
|
-
module Commander
|
3
|
-
|
4
|
-
# = Manager
|
5
|
-
#
|
6
|
-
# Controls management and invocation of sub-commands.
|
7
|
-
class Manager
|
8
|
-
|
9
|
-
attr_reader :command_options, :commands, :info
|
10
|
-
attr_reader :user_command, :user_args
|
11
|
-
|
12
|
-
# Initialize commander singleton.
|
13
|
-
#
|
14
|
-
# == Keys:
|
15
|
-
#
|
16
|
-
# * name (required)
|
17
|
-
# * version (required)
|
18
|
-
# * description
|
19
|
-
# * copyright
|
20
|
-
#
|
21
|
-
# == Examples:
|
22
|
-
#
|
23
|
-
# init_commander(
|
24
|
-
# :name => 'Commander',
|
25
|
-
# :version => Commander::VERSION,
|
26
|
-
# :description => 'Commander utility program.'
|
27
|
-
# )
|
28
|
-
#
|
29
|
-
def self.instance(options = {})
|
30
|
-
@@instance ||= Commander::Manager.new options
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.kill_instance!
|
34
|
-
@@instance = nil
|
35
|
-
end
|
36
|
-
|
37
|
-
def initialize(options)
|
38
|
-
@info, @command_options = options, {}
|
39
|
-
@user_args = ARGV.dup
|
40
|
-
@info[:help_generator] ||= Commander::HelpGenerators::Default
|
41
|
-
init_version
|
42
|
-
at_exit { run }
|
43
|
-
end
|
44
|
-
|
45
|
-
def add_command(command)
|
46
|
-
@commands ||= {} and @commands[command.command] = command
|
47
|
-
end
|
48
|
-
|
49
|
-
def get_command(command)
|
50
|
-
@commands[command.to_sym]
|
51
|
-
end
|
52
|
-
|
53
|
-
def execute_command
|
54
|
-
abort "Invalid command." if not @user_command
|
55
|
-
unless @user_command.options.empty?
|
56
|
-
opts = OptionParser.new
|
57
|
-
@user_command.options.each { |option| opts.on(*option[:args], &option[:proc]) }
|
58
|
-
opts.parse! @user_args
|
59
|
-
end
|
60
|
-
rescue OptionParser::MissingArgument => e
|
61
|
-
debug_abort e
|
62
|
-
else
|
63
|
-
@user_command.invoke(:when_called_proc, @user_args)
|
64
|
-
end
|
65
|
-
|
66
|
-
def valid_command?(command)
|
67
|
-
!@commands[command.to_sym].nil? unless command.nil?
|
68
|
-
end
|
69
|
-
alias :include? :valid_command?
|
70
|
-
|
71
|
-
def empty?
|
72
|
-
@commands.empty?
|
73
|
-
end
|
74
|
-
|
75
|
-
def length
|
76
|
-
@commands.length
|
77
|
-
end
|
78
|
-
alias :size :length
|
79
|
-
|
80
|
-
def valid_version?(version)
|
81
|
-
version.split('.').length == 3 if version.is_a? String
|
82
|
-
end
|
83
|
-
|
84
|
-
def parse_version(version)
|
85
|
-
version.split('.').collect { |v| v.to_i } if version.is_a? String
|
86
|
-
end
|
87
|
-
|
88
|
-
def debug_abort(msg, exception = nil)
|
89
|
-
abort msg
|
90
|
-
end
|
91
|
-
|
92
|
-
def init_version
|
93
|
-
raise "Your program must have a version tuple ('x.x.x')." unless valid_version?(@info[:version])
|
94
|
-
@info[:major], @info[:minor], @info[:tiny] = parse_version(@info[:version])
|
95
|
-
end
|
96
|
-
|
97
|
-
def run
|
98
|
-
parse_options!
|
99
|
-
abort "Invalid arguments." if @user_args.empty?
|
100
|
-
@command_options[:help] = true and @user_args.shift if @user_args[0] == 'help'
|
101
|
-
set_user_command
|
102
|
-
case
|
103
|
-
when (@command_options[:help] and @user_args.empty?) then output_help
|
104
|
-
when @command_options[:help] then output_help
|
105
|
-
else execute_command
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
def parse_options!
|
110
|
-
opts = OptionParser.new
|
111
|
-
opts.on('--help') { output_help; exit }
|
112
|
-
opts.on('--version') { output_version; exit }
|
113
|
-
opts.parse! @user_args rescue nil
|
114
|
-
end
|
115
|
-
|
116
|
-
def set_user_command
|
117
|
-
@user_command = get_command(@user_args[0]) unless @user_args.empty?
|
118
|
-
@user_args.shift
|
119
|
-
end
|
120
|
-
|
121
|
-
def output_help
|
122
|
-
@info[:help_generator].new self
|
123
|
-
end
|
124
|
-
|
125
|
-
def output_version
|
126
|
-
puts "#{@info[:name]} #{@info[:version]}"
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
data/spec/all_spec.rb
DELETED
data/spec/manager_spec.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
|
2
|
-
describe Commander::Manager do
|
3
|
-
before :each do
|
4
|
-
@manager = Commander::Manager.instance
|
5
|
-
end
|
6
|
-
|
7
|
-
it "should parse version numbers" do
|
8
|
-
@manager.parse_version('1.2.3').should == [1, 2, 3]
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should validate version numbers" do
|
12
|
-
@manager.valid_version?('1.2.3').should == true
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should invalidate version numbers" do
|
16
|
-
@manager.valid_version?('1.2').should_not == true
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|