Permalink
Browse files

add -h/--help using optparse, closes #8

also added --version
  • Loading branch information...
1 parent da3f005 commit 311ea36e202e4d12efc83829d132269cb3670280 @cldwalker cldwalker committed Apr 7, 2012
Showing with 45 additions and 3 deletions.
  1. +27 −3 lib/m.rb
  2. +18 −0 test/options_test.rb
View
30 lib/m.rb
@@ -38,12 +38,12 @@
#
# $ cat -n test/example_test.rb
# 1 require 'test/unit'
-# 2
+# 2
# 3 class ExampleTest < Test::Unit::TestCase
# 4 def test_apple
# 5 assert_equal 1, 1
# 6 end
-# 7
+# 7
# 8 def test_banana
# 9 assert_equal 1, 1
# 10 end
@@ -73,7 +73,7 @@
#Want to run the whole test? Just leave off the line number.
#
# $ m test/example_test.rb
-# Run options:
+# Run options:
#
# # Running tests:
#
@@ -128,6 +128,8 @@ def parse
# Just shell out to `rake test`.
exec "rake test"
else
+ parse_options! @argv
+
# Parse out ARGV, it should be coming in in a format like `test/test_file.rb:9`
@file, line = @argv.first.split(':')
@line = line.to_i
@@ -147,6 +149,28 @@ def parse
end
end
+ def parse_options!(argv)
+ require 'optparse'
+
+ OptionParser.new do |opts|
+ opts.banner = 'Options:'
+ opts.version = M::VERSION
+
+ opts.on '-h', '--help', 'Display this help.' do
+ puts "Usage: m [OPTIONS] [FILES]\n\n", opts
+ exit
+ end
+
+ opts.on '--version', 'Display version.' do
+ puts "m #{M::VERSION}"
+ exit
+ end
+
+ opts.parse! argv
+ end
+
+ end
+
def execute
# Locate tests to run that may be inside of this line. There could be more than one!
tests_to_run = tests.within(@line)
View
18 test/options_test.rb
@@ -0,0 +1,18 @@
+require 'test_helper'
+
+class OptionsTest < MTest
+ def test_short_help_option
+ output = m('-h')
+ assert_output /^Usage: m \[OPTIONS\] \[FILES\]/, output
+ end
+
+ def test_long_help_option
+ output = m('--help')
+ assert_output /^Usage: m \[OPTIONS\] \[FILES\]/, output
+ end
+
+ def test_verbose_option
+ output = m('--version')
+ assert_output /^m #{M::VERSION}/, output
+ end
+end

0 comments on commit 311ea36

Please sign in to comment.