Implement basic CLI usage help

This commit is contained in:
Thibault Jouan
2015-04-07 17:34:16 +00:00
parent f38a5761be
commit 422a940eae
6 changed files with 184 additions and 10 deletions

View File

@@ -1,13 +1,56 @@
module Uh
module WM
class CLI
ArgumentError = Class.new(ArgumentError)
USAGE = "Usage: #{File.basename $0} [options]".freeze
EX_USAGE = 64
class << self
def run! arguments
$stdout.sync = true
@display = Display.new
@display.open
puts "Connected to X server on `#{display}'"
sleep 8
def run arguments, **options
cli = new arguments, **options
cli.parse_arguments!
cli.run
rescue ArgumentError => e
$stderr.puts e
exit EX_USAGE
end
end
def initialize args, stdin: $stdin, stdout: $stdout, stderr: $stderr
@arguments = args
@stdin = stdin
@stdout = stdout
@stderr = stderr
end
def parse_arguments!
option_parser.parse! @arguments
rescue OptionParser::InvalidOption => e
fail ArgumentError, option_parser
end
def run
@stdout.sync = true
@display = Display.new
@display.open
@stdout.puts "Connected to X server on `#{@display}'"
end
private
def option_parser
OptionParser.new do |opts|
opts.banner = USAGE
opts.separator ''
opts.separator 'options:'
opts.on '-h', '--help', 'print this message' do
@stdout.print opts
exit
end
end
end
end