Skip to content

Commit

Permalink
add -h/--help using optparse, closes #8
Browse files Browse the repository at this point in the history
also added --version
  • Loading branch information
cldwalker committed Apr 7, 2012
1 parent da3f005 commit 311ea36
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/m.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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:
#
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions 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.