| Module | Mongrel::Command::Base |
| In: |
lib/mongrel/command.rb
lib/mongrel/command.rb |
| done_validating | [R] | |
| done_validating | [R] | |
| original_args | [R] | |
| original_args | [R] | |
| valid | [R] | |
| valid | [R] |
Called by the subclass to setup the command and parse the argv arguments. The call is destructive on argv since it uses the OptionParser#parse! function.
# File lib/mongrel/command.rb, line 44
44: def initialize(options={})
45: argv = options[:argv] || []
46: @opt = OptionParser.new
47: @opt.banner = Mongrel::Command::BANNER
48: @valid = true
49: # this is retarded, but it has to be done this way because -h and -v exit
50: @done_validating = false
51: @original_args = argv.dup
52:
53: configure
54:
55: # I need to add my own -h definition to prevent the -h by default from exiting.
56: @opt.on_tail("-h", "--help", "Show this message") do
57: @done_validating = true
58: puts @opt
59: end
60:
61: # I need to add my own -v definition to prevent the -v from exiting by default as well.
62: @opt.on_tail("--version", "Show version") do
63: @done_validating = true
64: if VERSION
65: puts "Version #{Mongrel::Const::MONGREL_VERSION}"
66: end
67: end
68:
69: @opt.parse! argv
70: end
Called by the subclass to setup the command and parse the argv arguments. The call is destructive on argv since it uses the OptionParser#parse! function.
# File lib/mongrel/command.rb, line 44
44: def initialize(options={})
45: argv = options[:argv] || []
46: @opt = OptionParser.new
47: @opt.banner = Mongrel::Command::BANNER
48: @valid = true
49: # this is retarded, but it has to be done this way because -h and -v exit
50: @done_validating = false
51: @original_args = argv.dup
52:
53: configure
54:
55: # I need to add my own -h definition to prevent the -h by default from exiting.
56: @opt.on_tail("-h", "--help", "Show this message") do
57: @done_validating = true
58: puts @opt
59: end
60:
61: # I need to add my own -v definition to prevent the -v from exiting by default as well.
62: @opt.on_tail("--version", "Show version") do
63: @done_validating = true
64: if VERSION
65: puts "Version #{Mongrel::Const::MONGREL_VERSION}"
66: end
67: end
68:
69: @opt.parse! argv
70: end
Called by the implemented command to set the options for that command. Every option has a short and long version, a description, a variable to set, and a default value. No exceptions.
# File lib/mongrel/command.rb, line 32
32: def options(opts)
33: # process the given options array
34: opts.each do |short, long, help, variable, default|
35: self.instance_variable_set(variable, default)
36: @opt.on(short, long, help) do |arg|
37: self.instance_variable_set(variable, arg)
38: end
39: end
40: end
Called by the implemented command to set the options for that command. Every option has a short and long version, a description, a variable to set, and a default value. No exceptions.
# File lib/mongrel/command.rb, line 32
32: def options(opts)
33: # process the given options array
34: opts.each do |short, long, help, variable, default|
35: self.instance_variable_set(variable, default)
36: @opt.on(short, long, help) do |arg|
37: self.instance_variable_set(variable, arg)
38: end
39: end
40: end
Runs the command doing it‘s job. You should implement this otherwise it will throw a NotImplementedError as a reminder.
# File lib/mongrel/command.rb, line 88
88: def run
89: raise NotImplementedError
90: end
Runs the command doing it‘s job. You should implement this otherwise it will throw a NotImplementedError as a reminder.
# File lib/mongrel/command.rb, line 88
88: def run
89: raise NotImplementedError
90: end
Validates the given expression is true and prints the message if not, exiting.
# File lib/mongrel/command.rb, line 94
94: def valid?(exp, message)
95: if not @done_validating and (not exp)
96: failure message
97: @valid = false
98: @done_validating = true
99: end
100: end
Validates the given expression is true and prints the message if not, exiting.
# File lib/mongrel/command.rb, line 94
94: def valid?(exp, message)
95: if not @done_validating and (not exp)
96: failure message
97: @valid = false
98: @done_validating = true
99: end
100: end
Validates that the given directory exists
# File lib/mongrel/command.rb, line 114
114: def valid_dir?(file, message)
115: valid?(file != nil && File.directory?(file), message)
116: end
Validates that the given directory exists
# File lib/mongrel/command.rb, line 114
114: def valid_dir?(file, message)
115: valid?(file != nil && File.directory?(file), message)
116: end
Validates that a file exists and if not displays the message
# File lib/mongrel/command.rb, line 103
103: def valid_exists?(file, message)
104: valid?(file != nil && File.exist?(file), message)
105: end
Validates that a file exists and if not displays the message
# File lib/mongrel/command.rb, line 103
103: def valid_exists?(file, message)
104: valid?(file != nil && File.exist?(file), message)
105: end
Validates that the file is a file and not a directory or something else.
# File lib/mongrel/command.rb, line 109
109: def valid_file?(file, message)
110: valid?(file != nil && File.file?(file), message)
111: end
Validates that the file is a file and not a directory or something else.
# File lib/mongrel/command.rb, line 109
109: def valid_file?(file, message)
110: valid?(file != nil && File.file?(file), message)
111: end
# File lib/mongrel/command.rb, line 128
128: def valid_group?(group)
129: valid?(@user, "You must also specify a user.")
130: begin
131: Etc.getgrnam(group)
132: rescue
133: failure "Group does not exist: #{group}"
134: @valid = false
135: end
136: end
# File lib/mongrel/command.rb, line 128
128: def valid_group?(group)
129: valid?(@user, "You must also specify a user.")
130: begin
131: Etc.getgrnam(group)
132: rescue
133: failure "Group does not exist: #{group}"
134: @valid = false
135: end
136: end
# File lib/mongrel/command.rb, line 118
118: def valid_user?(user)
119: valid?(@group, "You must also specify a group.")
120: begin
121: Etc.getpwnam(user)
122: rescue
123: failure "User does not exist: #{user}"
124: @valid = false
125: end
126: end
# File lib/mongrel/command.rb, line 118
118: def valid_user?(user)
119: valid?(@group, "You must also specify a group.")
120: begin
121: Etc.getpwnam(user)
122: rescue
123: failure "User does not exist: #{user}"
124: @valid = false
125: end
126: end
Returns true/false depending on whether the command is configured properly.
# File lib/mongrel/command.rb, line 77
77: def validate
78: return @valid
79: end