Skip to content
Browse files

Gracefully exit if an invalid option is given.

  • Loading branch information...
1 parent 1ee16b7 commit da30b33ddaa24acd27f039da4966a37750e5d052 @oggy committed Aug 11, 2012
Showing with 34 additions and 4 deletions.
  1. +4 −0 CHANGELOG
  2. +11 −3 lib/frag/app.rb
  3. +19 −1 test/unit/test_app.rb
View
4 CHANGELOG
@@ -1,3 +1,7 @@
+== LATEST
+
+ * Gracefully exit if an invalid option is given.
+
== 0.2.1 2012-08-11
* Gracefully exit if file is not writable.
View
14 lib/frag/app.rb
@@ -12,7 +12,11 @@ def initialize(args, input=STDIN, output=STDOUT, error=STDERR)
@state = State.new('frag:', 'frag end', '#', '', nil, nil)
- parser.parse!(args)
+ begin
+ parser.parse!(args)
+ rescue OptionParser::InvalidOption => e
+ return error e.message
+ end
args.size > 0 || @version_printed or
return error "no files given"
@@ -100,7 +104,7 @@ class << parser
def parse_subconfig!(args)
self.parsing_subconfig = true
# OptionParser will error on an argument like like "-->".
- if args.last =~ /\A--?(?:\W|\z)/
+ if args.last =~ /\A--?(?:[^0-9a-zA-Z]|\z)/
last_arg = args.pop
parse!(args)
args << last_arg
@@ -164,7 +168,11 @@ def process(input, output)
output.puts line
when /\A\s*(?:(\S+)\s*)?\$frag-config:\s*(.*)$/
args = Shellwords.shellsplit($2)
- parser.parse_subconfig!(args)
+ begin
+ parser.parse_subconfig!(args)
+ rescue OptionParser::InvalidOption => e
+ return error "#{input.lineno}: #{e.message}"
+ end
args.size <= 1 or
return error "#{input.lineno}: unexpected argument(s): #{args[0..-2].join(' ')}"
@state.leader = $1 || ''
View
20 test/unit/test_app.rb
@@ -371,6 +371,7 @@ def frag(*args)
|# $frag-config: --leader %
EOS
frag('input').must_equal 0
+ output.string.must_equal ''
error.string.must_equal "warning: -l / --leader is unnecessary in $frag-config line\n"
end
@@ -379,6 +380,7 @@ def frag(*args)
|# $frag-config: --trailer %
EOS
frag('input').must_equal 0
+ output.string.must_equal ''
error.string.must_equal "warning: -t / --trailer is unnecessary in $frag-config line\n"
end
@@ -502,12 +504,22 @@ def frag(*args)
EOS
end
+ it "errors if there is an invalid option" do
+ write_file 'input', <<-EOS.demargin
+ |# $frag-config: --invalid x
+ EOS
+ frag('input').must_equal 1
+ output.string.must_equal ''
+ error.string.must_match /invalid option: --invalid/
+ end
+
it "errors if there are stray arguments" do
write_file 'input', <<-EOS.demargin
|# $frag-config: arg trailer
EOS
frag('input').must_equal 1
- (output.string + error.string).must_match /unexpected argument/
+ output.string.must_equal ''
+ error.string.must_match /unexpected argument/
end
end
@@ -584,6 +596,12 @@ def frag(*args)
EOS
end
+ it "prints an error if an invalid option is given" do
+ frag('--invalid').must_equal 1
+ output.string.must_equal ''
+ error.string.must_match /invalid option: --invalid/
+ end
+
it "prints an error if an input file does not exist" do
frag('input').must_equal 1
output.string.must_equal ''

0 comments on commit da30b33

Please sign in to comment.
Something went wrong with that request. Please try again.