Class: RXCode::Command
- Inherits:
-
Object
show all
- Defined in:
- lib/rxcode/command.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
ARGUMENTS ==================================================================================================.
-
#err ⇒ Object
Returns the value of attribute err.
-
#input ⇒ Object
Returns the value of attribute input.
-
#options ⇒ Object
readonly
OPTIONS ====================================================================================================.
-
#output ⇒ Object
STREAMS ====================================================================================================.
Class Method Summary
collapse
Instance Method Summary
collapse
-
#env ⇒ Object
XCODE ENVIRONMENT ==========================================================================================.
-
#initialize(*args) {|_self| ... } ⇒ Command
constructor
Initializes the command with a set of options
and/or arguments
.
-
#run! ⇒ Object
COMMAND RUNNING ============================================================================================.
Constructor Details
#initialize(*args) {|_self| ... } ⇒ Command
Initializes the command with a set of options
and/or arguments
. The formal way to initialize a command looks like this, with options followed by arguments:
Command.new({ :option => 'value' }, [ 'first arg', 'second arg' ])
This is the way it’s done using parsed command-line arguments. However, this isn’t as intuitive when creating commands programmatically, so the more rubyish constructor signature is also supported:
Command.new('first arg', 'second arg', :option => 'value')
Command.new('first arg', 'second arg')
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/rxcode/command.rb', line 17
def initialize(*args)
if args.first.is_a?(Hash)
@options = args.shift
@arguments = args.shift
else
if args.last.is_a?(Hash)
@options = args.pop
else
@options = {}
end
@arguments = args
end
yield self if block_given?
end
|
Instance Attribute Details
#arguments ⇒ Object
ARGUMENTS ==================================================================================================
57
58
59
|
# File 'lib/rxcode/command.rb', line 57
def arguments
@arguments
end
|
#err ⇒ Object
Returns the value of attribute err.
41
42
43
|
# File 'lib/rxcode/command.rb', line 41
def err
@err
end
|
Returns the value of attribute input.
46
47
48
|
# File 'lib/rxcode/command.rb', line 46
def input
@input
end
|
#options ⇒ Object
OPTIONS ====================================================================================================
53
54
55
|
# File 'lib/rxcode/command.rb', line 53
def options
@options
end
|
#output ⇒ Object
STREAMS ====================================================================================================
36
37
38
|
# File 'lib/rxcode/command.rb', line 36
def output
@output
end
|
Class Method Details
.command_class_for_name(command_name) ⇒ Object
80
81
82
|
# File 'lib/rxcode/command.rb', line 80
def self.command_class_for_name(command_name)
self.commands[command_name]
end
|
.command_names ⇒ Object
76
77
78
|
# File 'lib/rxcode/command.rb', line 76
def self.command_names
self.commands.keys
end
|
.commands ⇒ Object
72
73
74
|
# File 'lib/rxcode/command.rb', line 72
def self.commands
@commands ||= {}
end
|
.display_name ⇒ Object
84
85
86
87
88
89
|
# File 'lib/rxcode/command.rb', line 84
def self.display_name
self.name.split('::').last.
gsub(/([A-Z]+)/) { |uppercase| '_' + uppercase.downcase }.
gsub(/^_/, '').
gsub(/[^a-z]_/) { |str| str.gsub(/_$/, '') }
end
|
.inherited(subclass) ⇒ Object
COMMAND REGISTRATION =======================================================================================
Commands are automatically registered when they subclass Command.
68
69
70
|
# File 'lib/rxcode/command.rb', line 68
def self.inherited(subclass)
self.commands[subclass.display_name] = subclass
end
|
.new_command_option_parser ⇒ Object
173
174
175
|
# File 'lib/rxcode/command.rb', line 173
def self.new_command_option_parser
Trollop::Parser.new
end
|
.new_global_option_parser ⇒ Object
—– COMMAND LINE PARSER —————————————————————————————-
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/rxcode/command.rb', line 154
def self.new_global_option_parser
Trollop::Parser.new do
version "rxcode #{RXCode::VERSION}"
banner <<-END
A utility for manipulating XCode projects.
Usage:
#{$0} [global options] command [command options]
Available Commands: #{::RXCode::Command.command_names.sort.join(', ')}
Global Options:
END
stop_on_unknown
end
end
|
.run!(args = ARGV) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/rxcode/command.rb', line 101
def self.run!(args = ARGV)
require 'trollop'
global_options = {}
command_name = nil
command_arguments = []
parser = self.new_global_option_parser
Trollop::with_standard_exception_handling parser do
global_options = parser.parse(args)
command_arguments = parser.leftovers
command_name = command_arguments.shift
if command_name.nil?
if (global_options.keys - [:version, :help]).empty?
raise Trollop::HelpNeeded
else
parser.die("No command given", nil)
end
elsif !RXCode::Command.command_names.include?(command_name)
parser.die("Unknown command (#{command_name.inspect})", nil)
end
end
run_command(command_name, command_arguments, global_options)
end
|
.run_command(command_name, command_args = [], global_options = {}) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/rxcode/command.rb', line 133
def self.run_command(command_name, command_args = [], global_options = {})
if command_class = self.command_class_for_name(command_name)
parser = command_class.new_command_option_parser
Trollop::with_standard_exception_handling parser do
command_options = parser.parse(command_args)
command_arguments = parser.leftovers
command = command_class.new(command_options, command_arguments)
command.run!
end
else
raise "Invalid Command: #{command_name.inspect}"
end
end
|
Instance Method Details
#env ⇒ Object
XCODE ENVIRONMENT ==========================================================================================
61
62
63
|
# File 'lib/rxcode/command.rb', line 61
def env
@env ||= RXCode::Environment.new(Dir.pwd)
end
|
#run! ⇒ Object
COMMAND RUNNING ============================================================================================
93
94
95
96
97
98
99
|
# File 'lib/rxcode/command.rb', line 93
def run!
if self.class == Command
raise "#{self.class.name} is an abstract class."
else
raise "#{Command.name}#run! is abstract and should be overridden"
end
end
|