lib/smallcage/application.rb in smallcage-0.2.1 vs lib/smallcage/application.rb in smallcage-0.2.2
- old
+ new
@@ -29,141 +29,239 @@
def self.execute
STDOUT.sync = true
self.new.execute
end
- def execute
+ def execute(argv = ARGV)
+ options = parse_options(argv)
+ SmallCage::Runner.run(options)
+ end
+
+ def parse_options(argv)
+ @argv = argv
@options = {}
+
@parser = create_main_parser
parse_main_options
+
@command_parsers = create_command_parsers
parse_command
parse_command_options
- SmallCage::Runner.run(@options)
+
+ @options
end
def create_main_parser
parser = OptionParser.new
parser.banner =<<BANNER
-Usage: #{File.basename($0)} <subcommand> [options]
+Usage: #{File.basename($0)} [options] <subcommand> [subcommand-options]
#{VERSION_NOTE}
Subcommands are:
update [path] Build smc contents.
clean [path] Remove files generated from *.smc source.
server [path] [port] Start HTTP server.
- auto [path] [port] Start auto update server.
+ auto [path] [port] [--bell] Start auto update server.
import [name|uri] Import project.
export [path] [outputpath] Export project.
uri [path] Print URIs.
manifest [path] Generate Manifest.html file.
-Options are:
BANNER
+
+ parser.separator "Options are:"
+ parser.on("-h", "--help", "Show this help message.") do
+ puts parser
+ exit(true)
+ end
+ parser.on("-v", "--version", "Show version info.") do
+ puts VERSION_NOTE
+ exit(true)
+ end
+
+ @options[:quiet] = false
+ parser.on("-q", "--quiet", "Do not print message.") do |boolean|
+ @options[:quiet] = boolean
+ end
+
return parser
end
private :create_main_parser
def parse_main_options
- @parser.separator ""
- @parser.on("-h", "--help", "Show this help message.") do
- puts @parser
- exit
- end
- @parser.on("-v", "--version", "Show version info.") do
- puts VERSION_NOTE
- exit
- end
- @parser.order!(ARGV)
+ @parser.order!(@argv)
+ rescue OptionParser::InvalidOption => e
+ $stderr.puts e.message
+ exit(false)
end
private :parse_main_options
def create_command_parsers
- parsers = Hash.new do |h,k|
- STDERR << "no such subcommand: #{k}\n"
- exit 1
- end
-
banners = {
- :update => "smc update [path]\n",
- :clean => "smc clean [path]\n",
- :server => "smc server [path] [port]\n",
- :auto => "smc auto [path] [port]\n",
- :import => "smc import [name|uri]",
- :export => "smc export [path] [outputpath]",
- :help => "smc help [command]\n",
- :uri => "smc uri [path]\n",
+ :update => <<EOT,
+smc update [path] [options]
+ path : target directory. (default:'.')
+EOT
+
+ :clean => <<EOT,
+smc clean [path] [options]
+ path : target directory (default:'.')
+EOT
+
+ :server => <<EOT,
+smc server [path] [port] [options]
+ path : target directory (default:'.')
+ port : HTTP server port number (default:8080)
+EOT
+
+ :auto => <<EOT,
+smc auto [path] [port] [options]
+ path : target directory (default:'.')
+ port : HTTP server port number (default:don't launch the server)
+EOT
+
+ :import => <<EOT,
+smc import [name|uri] [options]
+EOT
+
+ :export => <<EOT,
+smc export [path] [outputpath] [options]
+EOT
+
+ :help => <<EOT,
+smc help [command]
+EOT
+
+ :uri => <<EOT,
+smc uri [path] [options]
+EOT
+
+ :manifest => <<EOT,
+smc manifest [path] [options]
+EOT
+
}
-
- banners.each do |k,v|
- parsers[k] = OptionParser.new do |cp|
- cp.banner = "Usage: " + v
- end
+
+ parsers = {}
+ banners.each do |command, banner|
+ parsers[command] = create_default_command_parser(banner)
end
+ parsers[:auto].on("--bell", "Ring bell after publishing files.") do |boolean|
+ @options[:bell] = boolean
+ end
+
return parsers
end
private :create_command_parsers
+ def create_default_command_parser(banner)
+ parser = OptionParser.new
+ parser.banner = "Usage: " + banner
+ parser.separator "Options are:"
+
+ # these options can place both before and after the subcommand.
+ parser.on("-h", "--help", "Show this help message.") do
+ puts parser
+ exit(true)
+ end
+ parser.on("-v", "--version", "Show version info.") do
+ puts VERSION_NOTE
+ exit(true)
+ end
+ parser.on("-q", "--quiet", "Do not print message.") do |boolean|
+ @options[:quiet] = boolean
+ end
+
+ return parser
+ end
+ private :create_default_command_parser
+
def parse_command
+ @options[:command] = get_command
+
+ if @options[:command].nil?
+ puts @parser
+ exit(false)
+ end
+ parser = @command_parsers[@options[:command]]
+ if parser.nil?
+ $stderr.puts "no such subcommand: #{@options[:command]}"
+ exit(false)
+ end
+ parser.parse!(@argv)
+ rescue OptionParser::InvalidOption => e
+ $stderr.puts e.message
+ exit(false)
+ end
+ private :parse_command
+
+
+ def get_command
commands = Hash.new {|h,k| k}
commands.merge!({
:up => :update,
:sv => :server,
:au => :auto,
})
- unless ARGV.empty?
- @options[:command] = commands[ARGV.shift.to_sym]
- @command_parsers[@options[:command]].parse!(ARGV)
- end
-
- if @options[:command].nil?
- puts @parser
- exit
- end
+ command_name = @argv.shift.to_s.strip
+ return nil if command_name.empty?
+ return commands[command_name.to_sym]
end
- private :parse_command
+ private :get_command
def parse_command_options
if @options[:command] == :help
- subcmd = ARGV.shift
+ subcmd = @argv.shift
if subcmd.nil?
puts @parser
else
puts @command_parsers[subcmd.to_sym]
end
- exit
+ exit(true)
elsif @options[:command] == :update
- @options[:path] = ARGV.shift
+ @options[:path] = @argv.shift
@options[:path] ||= "."
elsif @options[:command] == :server
- @options[:path] = ARGV.shift
- @options[:port] = ARGV.shift
+ @options[:path] = @argv.shift
@options[:path] ||= "."
- @options[:port] ||= 80
+ @options[:port] = get_port_number(8080)
elsif @options[:command] == :auto
- @options[:path] = ARGV.shift
+ @options[:path] = @argv.shift
@options[:path] ||= "."
- @options[:port] = ARGV.shift
+ @options[:port] = get_port_number(nil)
+ @options[:bell] ||= false
elsif @options[:command] == :import
- @options[:from] = ARGV.shift
+ @options[:from] = @argv.shift
@options[:from] ||= "default"
- @options[:to] = ARGV.shift
+ @options[:to] = @argv.shift
@options[:to] ||= "."
elsif @options[:command] == :export
- @options[:path] = ARGV.shift
+ @options[:path] = @argv.shift
@options[:path] ||= "."
- @options[:out] = ARGV.shift
+ @options[:out] = @argv.shift
elsif @options[:command] == :uri
- @options[:path] = ARGV.shift
+ @options[:path] = @argv.shift
@options[:path] ||= "."
elsif @options[:command] == :manifest
- @options[:path] = ARGV.shift
+ @options[:path] = @argv.shift
@options[:path] ||= "."
elsif @options[:command] == :clean
- @options[:path] = ARGV.shift
+ @options[:path] = @argv.shift
@options[:path] ||= "."
end
end
private :parse_command_options
+
+ def get_port_number(default)
+ return default if @argv.empty?
+
+ port = @argv.shift
+ if port.to_i == 0
+ $stderr.puts "illegal port number: #{port}"
+ exit(false)
+ end
+ return port.to_i
+ end
+ private :get_port_number
end