Class: VrowserCLI
- Inherits:
-
Object
- Object
- VrowserCLI
- Defined in:
- bin/vrowser
Class Method Summary collapse
- .run(argv) ⇒ Object
-
.sub_commands ⇒ Object
get subcommand names.
Instance Method Summary collapse
- #command_daemon(options) ⇒ Object
- #command_fetch(options) ⇒ Object
- #command_httpd_daemon(options) ⇒ Object
- #command_json(options) ⇒ Object
-
#command_list(options) ⇒ Object
define sub commands.
- #command_sample(options) ⇒ Object
- #command_server(options) ⇒ Object
- #command_update(options) ⇒ Object
- #execute_sub_commands(cmd, cmd_opts) ⇒ Object
-
#initialize ⇒ VrowserCLI
constructor
A new instance of VrowserCLI.
- #parse(argv) ⇒ Object
Constructor Details
#initialize ⇒ VrowserCLI
Returns a new instance of VrowserCLI.
22 23 |
# File 'bin/vrowser', line 22 def initialize end |
Class Method Details
.run(argv) ⇒ Object
11 12 13 |
# File 'bin/vrowser', line 11 def self.run(argv) self.new.parse(argv) end |
.sub_commands ⇒ Object
get subcommand names
16 17 18 19 20 |
# File 'bin/vrowser', line 16 def self.sub_commands self.instance_methods.map(&:to_s).grep(/command_/).map{ |command_symbol| command_symbol.to_s.gsub(/^command_/, "") } end |
Instance Method Details
#command_daemon(options) ⇒ Object
122 123 124 |
# File 'bin/vrowser', line 122 def command_daemon() execute_as_server(.merge({:daemonize => true})) end |
#command_fetch(options) ⇒ Object
96 97 98 99 100 101 |
# File 'bin/vrowser', line 96 def command_fetch() Vrowser.load_file([:config_file]) do |vrowser| vrowser.fetch vrowser.clear end end |
#command_httpd_daemon(options) ⇒ Object
126 127 128 |
# File 'bin/vrowser', line 126 def command_httpd_daemon() execute_as_httpd_server(.merge({:daemonize => true})) end |
#command_json(options) ⇒ Object
110 111 112 113 114 115 116 |
# File 'bin/vrowser', line 110 def command_json() Vrowser.load_file([:config_file]) do |vrowser| greped = vrowser.active_servers.select(:name, :host, :ping, :num_players, :type, :map, :players) ordered = greped.order(:host) ordered.map(&:values).to_json.display end end |
#command_list(options) ⇒ Object
define sub commands
79 80 81 82 83 |
# File 'bin/vrowser', line 79 def command_list() Vrowser.load_file([:config_file]) do |vrowser| puts vrowser.active_servers.map(&:name).join($/) end end |
#command_sample(options) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'bin/vrowser', line 85 def command_sample() sample_config_path = (Pathname.new(__FILE__).dirname + "../examples/config.yml").realpath output_path = [:output_path] if File.exist? output_path STDERR.puts "Already file exists!: #{output_path}" else FileUtils.cp(sample_config_path, [:output_path]) STDOUT.puts "Generated sample config file: #{output_path}" end end |
#command_server(options) ⇒ Object
118 119 120 |
# File 'bin/vrowser', line 118 def command_server() execute_as_server(.merge({:damonize => false})) end |
#command_update(options) ⇒ Object
103 104 105 106 107 108 |
# File 'bin/vrowser', line 103 def command_update() Vrowser.load_file([:config_file]) do |vrowser| vrowser.update vrowser.clear end end |
#execute_sub_commands(cmd, cmd_opts) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'bin/vrowser', line 68 def execute_sub_commands(cmd, cmd_opts) if self.class.sub_commands.include? cmd self.send("command_" + cmd, cmd_opts) return 0 else cmd_opts.help.display return 1 end end |
#parse(argv) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'bin/vrowser', line 25 def parse(argv) @argv = argv sub_commands = self.class.sub_commands global_opts = Trollop:: do <<-EOS Usage: #{File.basename($0)} [#{sub_commands.join(',')}] hoge EOS version File.read(Pathname.new(__FILE__).dirname.realpath + "../VERSION") stop_on sub_commands end cmd = @argv.shift cmd_opts = Trollop:: do case cmd when "sample" opt :output_path, "output path", :short => "-o", :type => String, :default => "./config.yml" when "list" opt :config_file, "config file path", :short => "-f", :type => String, :required => true when "fetch" opt :config_file, "config file path", :short => "-f", :type => String, :required => true when "update" opt :config_file, "config file path", :short => "-f", :type => String, :required => true when "json" opt :config_file, "config file path", :short => "-f", :type => String, :required => true when "server", "daemon" opt :config_file, "config file path", :short => "-f", :type => String, :required => true opt :log_path, "log file path", :short => "-l", :type => String when "httpd_daemon" opt :config_file, "config file path", :short => "-f", :type => String, :required => true opt :port, "port number", :short => "-p", :type => String, :default => '3000' opt :host, "host or ip address", :short => "-h", :type => String, :default => 'localhost' opt :log_path, "log file path", :short => "-l", :default => STDERR opt :document_root, "document root path", :short => "-d", :type => String, :default => (Pathname.new(__FILE__).dirname.realpath + '../public_html').to_s else Trollop::die "unknown subcommand: #{cmd.inspect}" end end return execute_sub_commands(cmd, cmd_opts) end |