Class: CF::UAA::BaseCli
- Inherits:
-
Object
- Object
- CF::UAA::BaseCli
- Defined in:
- lib/cli/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.global_options ⇒ Object
Returns the value of attribute global_options.
-
.input ⇒ Object
readonly
Returns the value of attribute input.
-
.option_defs ⇒ Object
readonly
Returns the value of attribute option_defs.
-
.output ⇒ Object
readonly
Returns the value of attribute output.
-
.overview ⇒ Object
Returns the value of attribute overview.
-
.topics ⇒ Object
Returns the value of attribute topics.
Class Method Summary collapse
-
.handle_bad_command(args, msg) ⇒ Object
to be implemented in subclass.
-
.preprocess_options(args, opts) ⇒ Object
to be implemented in subclass.
- .run(args = ARGV) ⇒ Object
Class Attribute Details
.global_options ⇒ Object
Returns the value of attribute global_options.
233 234 235 |
# File 'lib/cli/base.rb', line 233 def @global_options end |
.input ⇒ Object (readonly)
Returns the value of attribute input.
232 233 234 |
# File 'lib/cli/base.rb', line 232 def input @input end |
.option_defs ⇒ Object (readonly)
Returns the value of attribute option_defs.
232 233 234 |
# File 'lib/cli/base.rb', line 232 def option_defs @option_defs end |
.output ⇒ Object (readonly)
Returns the value of attribute output.
232 233 234 |
# File 'lib/cli/base.rb', line 232 def output @output end |
.overview ⇒ Object
Returns the value of attribute overview.
233 234 235 |
# File 'lib/cli/base.rb', line 233 def overview @overview end |
.topics ⇒ Object
Returns the value of attribute topics.
233 234 235 |
# File 'lib/cli/base.rb', line 233 def topics @topics end |
Class Method Details
.handle_bad_command(args, msg) ⇒ Object
to be implemented in subclass
237 |
# File 'lib/cli/base.rb', line 237 def self.handle_bad_command(args, msg) end |
.preprocess_options(args, opts) ⇒ Object
to be implemented in subclass
236 |
# File 'lib/cli/base.rb', line 236 def self.(args, opts) end |
.run(args = ARGV) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/cli/base.rb', line 239 def self.run(args = ARGV) @input ||= $stdin @output ||= $stdout @output.string = "" if @output.respond_to?(:string) args = Shellwords.split(args) if args.respond_to?(:split) @option_defs, @parser, orig = {}, OptionParser.new, args opts = @topics.each_with_object({}) do |tpc, o| tpc.option_defs.each do |k, optdef| @parser.on(*optdef) { |v| o[k] = v } @option_defs[k] = optdef end end @parser.parse! args (args, opts) @topics.each do |tpc| tpc.commands.each do |k, v| next unless args[0..v[:parts].length - 1] == v[:parts] args = args[v[:parts].length..-1] if v[:argc] == -1 # variable args, leave args alone elsif args.length > v[:argc] return handle_bad_command(orig, "Too many command line parameters given.") elsif args.length < v[:argc] (v[:argc] - args.length).times { args << nil } end opts.keys.each do |o| next if v[:options].include?(o) || @global_options.include?(o) return handle_bad_command(orig, "Invalid option: #{o}") end return tpc.new(self, opts, @input, @output).send(k, *args) end end @output.puts "#{File.basename($0)}: subcommand not found" rescue Exception => e @output.puts "#{File.basename($0)} error", "#{e.class}: #{e.}", (e.backtrace if opts[:trace]) ensure puts @output.string if opts[:trace] && @print_on_trace && @output.respond_to?(:string) end |