From ce335a1cf4f3485508cc1c146c8afbb090533cd2 Mon Sep 17 00:00:00 2001 From: Nobuhiro IMAI Date: Thu, 3 May 2012 20:47:27 +0900 Subject: [PATCH 1/2] use Slop "no-" prefix feature * "-n" is marked as deprecated, but works preferentially --- bin/earthquake | 15 ++++++++++----- lib/earthquake/core.rb | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/bin/earthquake b/bin/earthquake index 9868b58..2faadc6 100755 --- a/bin/earthquake +++ b/bin/earthquake @@ -5,9 +5,10 @@ argv = ARGV.dup slop = Slop.new(:strict => true, :help => true) slop.banner "Usage: earthquake [options] [directory]" slop.on :d, :debug, 'Enable debug mode' -slop.on :n, :'no-logo', 'No Logo' -slop.on :c, :command, "Invoke a command and exit", true -slop.on :'--no-stream', "No stream mode" +slop.on :l, :logo, 'Show Logo', default: true +slop.on :n, 'No Logo (deprecated)' +slop.on :c, :command=, "Invoke a command and exit" +slop.on :stream, "Stream mode", default: true begin slop.parse!(argv) rescue => e @@ -15,6 +16,10 @@ rescue => e exit! end options = slop.to_hash +if options.delete(:n) + warn "-n is deprecated. use --no-logo instead." + options.delete(:logo) +end options.delete(:help) options[:dir] = argv.shift unless argv.empty? @@ -23,9 +28,9 @@ $:.unshift Pathname.new(__FILE__).realpath.join('../../lib') if $0 == __FILE__ require "earthquake/version" command = options.delete(:command) -no_logo = options.delete(:'no-logo') +logo = options.delete(:logo) -if !no_logo && !command +if logo && !command print "\e[31m" puts %q{ _ _ _ diff --git a/lib/earthquake/core.rb b/lib/earthquake/core.rb index 2b483eb..d981998 100644 --- a/lib/earthquake/core.rb +++ b/lib/earthquake/core.rb @@ -148,7 +148,7 @@ def start(options = {}) end end - reconnect unless options[:'no-stream'] == true + reconnect if options[:stream] trap('INT') { stop } end From 93f21d0560cf2aab2fd31831647913786637fb9b Mon Sep 17 00:00:00 2001 From: Nobuhiro IMAI Date: Fri, 4 May 2012 00:17:45 +0900 Subject: [PATCH 2/2] add "[no-]" to flag-like options on help --- bin/earthquake | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bin/earthquake b/bin/earthquake index 2faadc6..47060a1 100755 --- a/bin/earthquake +++ b/bin/earthquake @@ -1,8 +1,10 @@ #!/usr/bin/env ruby require 'slop' +require 'stringio' argv = ARGV.dup -slop = Slop.new(:strict => true, :help => true) +io = StringIO.new +slop = Slop.new(:strict => true, :help => true, :exit_on_help => false, :io => io) slop.banner "Usage: earthquake [options] [directory]" slop.on :d, :debug, 'Enable debug mode' slop.on :l, :logo, 'Show Logo', default: true @@ -11,6 +13,17 @@ slop.on :c, :command=, "Invoke a command and exit" slop.on :stream, "Stream mode", default: true begin slop.parse!(argv) + (help = io.string).each_line do |line| + if m = /\A.{8}--(\S+ {5})/.match(line) + name = m[1].rstrip + if name != "help" and !slop.options[name.to_sym].expects_argument? + puts line.sub(m[1], "[no-]" + name) + next + end + end + puts line + end + exit unless help.empty? rescue => e puts e exit!