Class: DaemonKit::Arguments
- Inherits:
-
Object
- Object
- DaemonKit::Arguments
- Defined in:
- lib/daemon_kit/arguments.rb
Overview
A wrapper around OptParse for setting up arguments to the daemon process.
TODO: Set rules for basic options that go for all daemons TODO: Load options from config/arguments.rb
Class Attribute Summary collapse
-
.commands ⇒ Object
readonly
Returns the value of attribute commands.
-
.default_command ⇒ Object
readonly
Returns the value of attribute default_command.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.arguments(argv) ⇒ Object
Return the arguments remaining after running through #configuration.
-
.command(argv) ⇒ Object
Parse the provided argument array for a given command, or return the default command and the remaining arguments.
-
.configuration(argv) ⇒ Object
Extracts any values for arguments matching ‘–config’ as well as some implication arguments like ‘-e’.
-
.parse(argv) ⇒ Object
Parse the argument values and return an array with the command name, config values and argument values.
Instance Method Summary collapse
-
#initialize ⇒ Arguments
constructor
A new instance of Arguments.
- #parse(argv) ⇒ Object
Constructor Details
#initialize ⇒ Arguments
Returns a new instance of Arguments.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/daemon_kit/arguments.rb', line 112 def initialize @options = {} @parser = OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} [command] [options]" opts.separator "" opts.separator "Command is one of the following:" opts.separator " run - Run the daemon without forking (default)" opts.separator " start - Run the daemon" opts.separator " stop - Stop the running daemon" opts.separator "" opts.separator "Options can be:" arg_file = File.join( DaemonKit.root, 'config', 'arguments.rb' ) eval(IO.read(arg_file), binding, arg_file) if File.exists?( arg_file ) opts.on("-e", "--env", "--environment ENV", "Environment for the process", " Default: development") do # Nothing, just here for show end opts.on("-i", "--instance N", "Process instance number", " Default: 1") do # Nothing, just here for show end opts.on("-p", "--pid", "--pidfile PATH", "Path to the pidfile", " Default: log/#{DaemonKit.configuration.daemon_name}.N.pid") do # Nothing, just here for show end opts.on("-l", "--log PATH", "Path to the log file", " Default: log/[environment].log") do # Nothing, just here for show end opts.separator "" opts.separator "Advanced configurations:" opts.on("-c", "--config ATTRIBUTE=VALUE", "Change values of the daemon-kit Configuration instance", " Example: log_dir=/path/to/log-directory") do # Nothing, just here for show end opts.separator "" opts.separator "Common options:" opts.on("-v", "--version", "Show version information and exit") do puts "daemon-kit #{DaemonKit.version} (http://github.com/kennethkalmer/daemon-kit)" exit end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end end |
Class Attribute Details
.commands ⇒ Object (readonly)
Returns the value of attribute commands.
24 25 26 |
# File 'lib/daemon_kit/arguments.rb', line 24 def commands @commands end |
.default_command ⇒ Object (readonly)
Returns the value of attribute default_command.
24 25 26 |
# File 'lib/daemon_kit/arguments.rb', line 24 def default_command @default_command end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
110 111 112 |
# File 'lib/daemon_kit/arguments.rb', line 110 def @options end |
Class Method Details
.arguments(argv) ⇒ Object
Return the arguments remaining after running through #configuration
105 106 107 |
# File 'lib/daemon_kit/arguments.rb', line 105 def arguments( argv ) self.configuration( argv ).last end |
.command(argv) ⇒ Object
Parse the provided argument array for a given command, or return the default command and the remaining arguments
36 37 38 39 40 41 |
# File 'lib/daemon_kit/arguments.rb', line 36 def command( argv ) # extract command or set default cmd = self.commands.include?( argv[0] ) ? argv.shift : self.default_command return cmd.to_sym, argv end |
.configuration(argv) ⇒ Object
Extracts any values for arguments matching ‘–config’ as well as some implication arguments like ‘-e’. Returns an array with the configs as the first value and the remaing args as the last value.
To set a value on the default #Configuration instance, use the following notation:
--config attribute=value
The above notation can be used several times to set different values.
Special, or ‘normal’ arguments that are mapped to the default #Configuration instance are listed below:
-e value or --env value => environment
--pidfile pidfile => pid_file
-l path or --log path => /path/to/log/file
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/daemon_kit/arguments.rb', line 63 def configuration( argv ) configs = [] i = 0 while i < argv.size if argv[i] == "-c" || argv[i] == "--config" argv.delete_at( i ) configs << argv.delete_at(i) next end if argv[i] == "-e" || argv[i] == "--env" || argv[i] == "--environment" argv.delete_at( i ) configs << "environment=#{argv.delete_at(i)}" next end if argv[i] == "-l" || argv[i] == "--log" argv.delete_at( i ) configs << "log_path=#{argv.delete_at(i)}" next end if argv[i] == "-i" || argv[i] == "--instance" argv.delete_at( i ) configs << "instance=#{argv.delete_at(i)}" next end if argv[i] == "-p" || argv[i] == "--pidfile" || argv[i] == "--pid" argv.delete_at( i ) configs << "pid_file=#{argv.delete_at(i)}" next end i += 1 end return configs, argv end |
.parse(argv) ⇒ Object
Parse the argument values and return an array with the command name, config values and argument values
28 29 30 31 32 |
# File 'lib/daemon_kit/arguments.rb', line 28 def parse( argv ) cmd, argv = self.command( argv ) return cmd, *self.configuration( argv ) end |
Instance Method Details
#parse(argv) ⇒ Object
171 172 173 |
# File 'lib/daemon_kit/arguments.rb', line 171 def parse( argv ) @parser.parse!( argv ) end |