Class: Wsv::CLI
- Inherits:
-
Object
- Object
- Wsv::CLI
- Defined in:
- lib/wsv/cli.rb
Constant Summary collapse
- DEFAULT_HOST =
"127.0.0.1"- DEFAULT_PORT =
8000
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
Instance Method Summary collapse
-
#initialize(argv, out: $stdout, err: $stderr) ⇒ CLI
constructor
A new instance of CLI.
- #parse_options(args) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(argv, out: $stdout, err: $stderr) ⇒ CLI
Returns a new instance of CLI.
13 14 15 16 17 |
# File 'lib/wsv/cli.rb', line 13 def initialize(argv, out: $stdout, err: $stderr) @argv = argv.dup @out = out @err = err end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
11 12 13 |
# File 'lib/wsv/cli.rb', line 11 def argv @argv end |
Instance Method Details
#parse_options(args) ⇒ Object
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 70 |
# File 'lib/wsv/cli.rb', line 36 def (args) = { host: DEFAULT_HOST, port: DEFAULT_PORT, directory: Dir.pwd } parser = OptionParser.new do |opts| opts. = "Usage: wsv [options] [directory]" opts.on("-h", "--host HOST", "Bind host (default: #{DEFAULT_HOST})") do |host| [:host] = host end opts.on("-p", "--port PORT", Integer, "Bind port (default: #{DEFAULT_PORT})") do |port| [:port] = validate_port(port) end opts.on("--help", "Show help") do @out.puts opts [:handled] = true end opts.on("--version", "Show version") do @out.puts Wsv::VERSION [:handled] = true end end parser.parse!(args) raise ArgumentError, "too many directories" if args.length > 1 [:directory] = args.first if args.first end |
#run ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wsv/cli.rb', line 19 def run = (argv) return 0 if [:handled] root = resolve_root([:directory]) server = Server.new(host: [:host], port: [:port], root: root, out: @out, err: @err) server.start 0 rescue OptionParser::ParseError, ArgumentError => e @err.puts "wsv: #{e.}" @err.puts "Try `wsv --help` for usage." 1 rescue SystemCallError => e @err.puts "wsv: #{e.}" 1 end |