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_json(options) ⇒ Object
-
#command_list(options) ⇒ Object
define sub commands.
- #command_sample(options) ⇒ Object
- #command_server(options) ⇒ Object
- #command_update(options) ⇒ 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
108 109 110 |
# File 'bin/vrowser', line 108 def command_daemon() execute_as_server(.merge({:daemonize => true})) end |
#command_json(options) ⇒ Object
96 97 98 99 100 101 102 |
# File 'bin/vrowser', line 96 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
72 73 74 75 76 |
# File 'bin/vrowser', line 72 def command_list() Vrowser.load_file([:config_file]) do |vrowser| puts vrowser.active_servers.map(&:name).join($/) end end |
#command_sample(options) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'bin/vrowser', line 78 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
104 105 106 |
# File 'bin/vrowser', line 104 def command_server() execute_as_server(.merge({:damonize => false})) end |
#command_update(options) ⇒ Object
89 90 91 92 93 94 |
# File 'bin/vrowser', line 89 def command_update() Vrowser.load_file([:config_file]) do |vrowser| vrowser.update vrowser.clear 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 67 68 69 |
# File 'bin/vrowser', line 25 def parse(argv) @argv = argv sub_commands = self.class.sub_commands global_opts = Trollop:: do "Usage: \#{File.basename($0)} [\#{sub_commands.join(',')}]\nhoge\n EOS\n version File.read(Pathname.new(__FILE__).dirname.realpath + \"../VERSION\")\n stop_on sub_commands\n end\n\n cmd = @argv.shift\n cmd_opts = Trollop::options do\n case cmd\n when \"sample\"\n opt :output_path, \"output path\", :short => \"-o\", :type => String, :default => \"./config.yml\"\n when \"list\"\n opt :config_file, \"config file path\", :short => \"-f\", :type => String, :required => true\n when \"fetch\"\n opt :config_file, \"config file path\", :short => \"-f\", :type => String, :required => true\n when \"update\"\n opt :config_file, \"config file path\", :short => \"-f\", :type => String, :required => true\n when \"json\"\n opt :config_file, \"config file path\", :short => \"-f\", :type => String, :required => true\n when \"server\", \"daemon\"\n opt :config_file, \"config file path\", :short => \"-f\", :type => String, :required => true\n opt :port, \"port number\", :short => \"-p\", :type => String, :default => '3000'\n opt :host, \"host or ip address\", :short => \"-h\", :type => String, :default => 'localhost'\n opt :log_path, \"log file path\", :short => \"-l\", :default => STDERR\n opt :document_root, \"document root path\", :short => \"-d\", :type => String,\n :default => (Pathname.new(__FILE__).dirname.realpath + '../public_html').to_s\n else\n Trollop::die \"unknown subcommand: \#{cmd.inspect}\"\n end\n end\n\n if sub_commands.include? cmd\n self.send(\"command_\" + cmd, cmd_opts)\n return 0\n else\n cmd_opts.help.display\n end\nend\n" |