improved option handling
This commit is contained in:
parent
f40510b93e
commit
44eb9fbd70
|
@ -6,6 +6,7 @@ require 'htmlentities'
|
||||||
require 'tsort'
|
require 'tsort'
|
||||||
require 'set'
|
require 'set'
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
require 'etc'
|
||||||
|
|
||||||
class ImportScripts::Smf2 < ImportScripts::Base
|
class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
|
|
||||||
|
@ -13,8 +14,12 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
options = Options.new
|
options = Options.new
|
||||||
begin
|
begin
|
||||||
options.parse!
|
options.parse!
|
||||||
|
rescue Options::SettingsError => err
|
||||||
|
$stderr.puts "Cannot load SMF settings: #{err.message}"
|
||||||
|
exit 1
|
||||||
rescue Options::Error => err
|
rescue Options::Error => err
|
||||||
$stderr.puts err.to_s.capitalize
|
$stderr.puts err.to_s.capitalize
|
||||||
|
$stderr.puts options.usage
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
new(options).perform
|
new(options).perform
|
||||||
|
@ -24,10 +29,7 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
super()
|
super()
|
||||||
|
|
||||||
@options = options
|
@options = options
|
||||||
options.host ||= 'localhost'
|
|
||||||
options.prefix ||= 'smf_'
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
timezone = `php -i`.lines.each do |line|
|
timezone = `php -i`.lines.each do |line|
|
||||||
|
@ -401,12 +403,16 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
class Options
|
class Options
|
||||||
|
|
||||||
class Error < StandardError ; end
|
class Error < StandardError ; end
|
||||||
|
class SettingsError < Error ; end
|
||||||
|
|
||||||
def self.parse!(args = ARGV)
|
def initialize
|
||||||
new.tap {|options| options.parse!(args) }
|
self.host = 'localhost'
|
||||||
|
self.username = Etc.getlogin
|
||||||
|
self.prefix = 'smf_'
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse!(args = ARGV)
|
def parse!(args = ARGV)
|
||||||
|
raise Error, 'not enough arguments' if ARGV.empty?
|
||||||
begin
|
begin
|
||||||
parser.parse!(args)
|
parser.parse!(args)
|
||||||
rescue OptionParser::ParseError => err
|
rescue OptionParser::ParseError => err
|
||||||
|
@ -443,17 +449,18 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue => err
|
rescue => err
|
||||||
raise Error, "#{settings}: failed reading SMF settings, #{err.message}"
|
raise SettingsError, err.message unless self.database
|
||||||
end
|
end
|
||||||
|
|
||||||
def parser
|
def parser
|
||||||
@parser ||= OptionParser.new do |o|
|
@parser ||= OptionParser.new(nil, 12) do |o|
|
||||||
o.banner = "Usage:\t#{File.basename($0)} [options] [SMFROOT]"
|
o.banner = "Usage:\t#{File.basename($0)} <SMFROOT> [options]\n"
|
||||||
o.on('-h', '--host=HOST', 'MySQL server hostname') {|s| self.host = s }
|
o.banner << "\t#{File.basename($0)} -d <DATABASE> [options]"
|
||||||
o.on('-u', '--username=USER', 'MySQL username') {|s| self.username = s }
|
o.on('-h HOST', :REQUIRED, "MySQL server hostname [\"#{self.host}\"]") {|s| self.host = s }
|
||||||
o.on('-p', '--password[=PASS]', 'MySQL password') {|s| self.password = s || :ask }
|
o.on('-u USER', :REQUIRED, "MySQL username [\"#{self.username}\"]") {|s| self.username = s }
|
||||||
o.on('-d', '--database=DB', 'Name of SMF database') {|s| self.database = s }
|
o.on('-p [PASS]', :OPTIONAL, 'MySQL password. Without argument, reads password from STDIN.') {|s| self.password = s || :ask }
|
||||||
o.on('-f', '--prefix=PREFIX', 'Table names prefix') {|s| self.prefix = s }
|
o.on('-d DBNAME', :REQUIRED, 'Name of SMF database') {|s| self.database = s }
|
||||||
|
o.on('-f PREFIX', :REQUIRED, "Table names prefix [\"#{self.prefix}\"]") {|s| self.prefix = s }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue