Class: Perennial::Application
Defined Under Namespace
Classes: CommandEnv
Instance Attribute Summary collapse
-
#banner ⇒ Object
Returns the value of attribute banner.
-
#command_env ⇒ Object
Returns the value of attribute command_env.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #add(raw_command, description = nil, &blk) ⇒ Object
- #add_default_options! ⇒ Object
- #controller!(controller, description) ⇒ Object
- #execute(arguments) ⇒ Object
- #generator! ⇒ Object
- #help_for(command) ⇒ Object
-
#initialize(opts = {}) ⇒ Application
constructor
A new instance of Application.
- #option(name, description = nil) ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Application
Returns a new instance of Application.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/perennial/application.rb', line 22 def initialize(opts = {}) @options = opts @commands = {} @descriptions = {} @option_parsers = {} @option_parser = nil @command_options = {} @command_env = (opts[:command_env] || CommandEnv) @banners = {} end |
Instance Attribute Details
#banner ⇒ Object
Returns the value of attribute banner.
20 21 22 |
# File 'lib/perennial/application.rb', line 20 def @banner end |
#command_env ⇒ Object
Returns the value of attribute command_env.
20 21 22 |
# File 'lib/perennial/application.rb', line 20 def command_env @command_env end |
#options ⇒ Object
Returns the value of attribute options.
20 21 22 |
# File 'lib/perennial/application.rb', line 20 def @options end |
Class Method Details
.processing(args, &blk) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/perennial/application.rb', line 121 def self.processing(args, &blk) application = self.new if blk.arity == 1 blk.call(application) else application.instance_eval(&blk) end application.execute args end |
Instance Method Details
#add(raw_command, description = nil, &blk) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/perennial/application.rb', line 69 def add(raw_command, description = nil, &blk) raise ArgumentError, "You must provide a block with an #{self.class.name}#add" if blk.nil? raise ArgumentError, "Your block must accept atleast one argument (a hash of options)" if blk.arity == 0 command, _ = raw_command.split(" ", 2) @banners[command] = raw_command @commands[command] = blk @descriptions[command] = description if description.present? # Add the default help message for a command option_parser.add(:help, "Show this message") { help_for(command) } @option_parsers[command] = option_parser @option_parser = nil end |
#add_default_options! ⇒ Object
37 38 39 |
# File 'lib/perennial/application.rb', line 37 def option_parser.add_defaults! end |
#controller!(controller, description) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/perennial/application.rb', line 47 def controller!(controller, description) return unless defined?(Loader) option :kill, "Kill any runninng instances" controller_name = controller.to_s.underscore controller = controller.to_sym command_name = controller_name.gsub("_", "-") add("#{command_name} [PATH]", description) do |*args| = args. path = File.(args[0] || ".") Settings.root = path if .delete(:kill) puts "Attempting to kill processess..." Daemon.kill_all(controller) else Loader.run!(controller, ) end end end |
#execute(arguments) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/perennial/application.rb', line 82 def execute(arguments) return usage if arguments.empty? arguments = arguments.dup command = arguments.shift if @commands.has_key?(command) execute_command(command, arguments) else puts "Unknown command '#{command}', please try again." return usage end end |
#generator! ⇒ Object
41 42 43 44 45 |
# File 'lib/perennial/application.rb', line 41 def generator! if defined?(Generator) self.command_env = Generator::CommandEnv end end |
#help_for(command) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/perennial/application.rb', line 109 def help_for(command) if .present? puts puts "" end puts @descriptions[command] puts "Usage: #{$0} #{@banners[command]} [options]" puts "Options:" puts @option_parsers[command].summary exit end |
#option(name, description = nil) ⇒ Object
33 34 35 |
# File 'lib/perennial/application.rb', line 33 def option(name, description = nil) option_parser.add(name, description) { |v| @command_options[name] = v } end |
#usage ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/perennial/application.rb', line 94 def usage if .present? puts puts "" end puts "Usage:" max_width = @banners.values.map { |b| b.length }.max @commands.keys.sort.each do |command| next unless @descriptions.has_key?(command) formatted_command = "#{@banners[command]} [OPTIONS]".ljust(max_width + 10) command = "%s - %s" % [formatted_command, @descriptions[command]] puts command end end |