diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2013-10-28 23:28:01 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2013-11-03 22:07:58 -0700 |
| commit | 7455ec6b0fc14c5db744e1955682425c8904bd4c (patch) | |
| tree | fcc97a8fb8a533be7eaafc74d31cb0c8f40142c0 | |
| parent | 25754e5f5b9b247b0168e962987011dbb1fabb52 (diff) | |
resolves #557 add flag to cli to suppress warnings
| -rw-r--r-- | lib/asciidoctor/cli/invoker.rb | 15 | ||||
| -rw-r--r-- | lib/asciidoctor/cli/options.rb | 11 | ||||
| -rw-r--r-- | man/asciidoctor.1 | 5 | ||||
| -rw-r--r-- | man/asciidoctor.adoc | 3 | ||||
| -rw-r--r-- | test/invoker_test.rb | 26 | ||||
| -rw-r--r-- | test/options_test.rb | 22 |
6 files changed, 76 insertions, 6 deletions
diff --git a/lib/asciidoctor/cli/invoker.rb b/lib/asciidoctor/cli/invoker.rb index b5359c96..aba667a3 100644 --- a/lib/asciidoctor/cli/invoker.rb +++ b/lib/asciidoctor/cli/invoker.rb @@ -29,6 +29,17 @@ module Asciidoctor def invoke! return if @options.nil? + old_verbose = $VERBOSE + + case @options[:verbose] + when 0 + $VERBOSE = nil + when 1 + $VERBOSE = false + when 2 + $VERBOSE = true + end + begin opts = {} profile = false @@ -47,7 +58,7 @@ module Asciidoctor when :attributes opts[:attributes] = v.dup when :verbose - profile = true if v + profile = true if v == 2 when :trace # currently, nothing else @@ -101,6 +112,8 @@ module Asciidoctor err.puts e.message err.puts ' Use --trace for backtrace' @code = 1 + ensure + $VERBOSE = old_verbose end end diff --git a/lib/asciidoctor/cli/options.rb b/lib/asciidoctor/cli/options.rb index 61b62864..3f1a0e6f 100644 --- a/lib/asciidoctor/cli/options.rb +++ b/lib/asciidoctor/cli/options.rb @@ -22,7 +22,7 @@ module Asciidoctor end self[:eruby] = options[:eruby] || nil self[:compact] = options[:compact] || false - self[:verbose] = options[:verbose] || false + self[:verbose] = options[:verbose] || 1 self[:base_dir] = options[:base_dir] self[:destination_dir] = options[:destination_dir] || nil self[:trace] = false @@ -42,9 +42,6 @@ Example: asciidoctor -b html5 source.asciidoc EOS - opts.on('-v', '--verbose', 'enable verbose mode (default: false)') do |verbose| - self[:verbose] = true - end opts.on('-b', '--backend BACKEND', 'set output format backend (default: html5)') do |backend| self[:attributes]['backend'] = backend end @@ -111,9 +108,15 @@ Example: asciidoctor -b html5 source.asciidoc opts.on('-D', '--destination-dir DIR', 'destination output directory (default: directory of source file)') do |dest_dir| self[:destination_dir] = dest_dir end + opts.on('-q', '--quiet', 'suppress warnings (default: false)') do |verbose| + self[:verbose] = 0 + end opts.on('--trace', 'include backtrace information on errors (default: false)') do |trace| self[:trace] = true end + opts.on('-v', '--verbose', 'enable verbose mode (default: false)') do |verbose| + self[:verbose] = 2 + end opts.on_tail('-h', '--help', 'show this message') do $stdout.puts opts diff --git a/man/asciidoctor.1 b/man/asciidoctor.1 index e5caebb5..931a0c9a 100644 --- a/man/asciidoctor.1 +++ b/man/asciidoctor.1 @@ -104,6 +104,11 @@ This option may be specified more than once\&. Matching templates found in subse .RE .SS "Processing Information" .PP +\fB\-q, \-\-quiet\fR +.RS 4 +Silence warnings\&. +.RE +.PP \fB\-\-trace\fR .RS 4 Include backtrace information on errors\&. Not enabled by default\&. diff --git a/man/asciidoctor.adoc b/man/asciidoctor.adoc index 3cfffe40..cc31fd46 100644 --- a/man/asciidoctor.adoc +++ b/man/asciidoctor.adoc @@ -122,6 +122,9 @@ subsequent directories override ones previously discovered. === Processing Information +*-q, --quiet*:: + Silence warnings. + *--trace*:: Include backtrace information on errors. Not enabled by default. diff --git a/test/invoker_test.rb b/test/invoker_test.rb index f2609cc9..0082bdbc 100644 --- a/test/invoker_test.rb +++ b/test/invoker_test.rb @@ -92,6 +92,32 @@ context 'Invoker' do end end + test 'should print warnings to stderr by default' do + input = <<-EOS +2. second +3. third + EOS + warnings = nil + redirect_streams do |stdout, stderr| + invoke_cli_to_buffer(%w(-o /dev/null), '-') { input } + warnings = stderr.string + end + assert_match(/WARNING/, warnings) + end + + test 'should silence warnings if -q flag is specified' do + input = <<-EOS +2. second +3. third + EOS + warnings = nil + redirect_streams do |stdout, stderr| + invoke_cli_to_buffer(%w(-q -o /dev/null), '-') { input } + warnings = stderr.string + end + assert_equal '', warnings + end + test 'should report usage if no input file given' do redirect_streams do |stdout, stderr| invoke_cli [], nil diff --git a/test/options_test.rb b/test/options_test.rb index 3648f102..76dd8cb5 100644 --- a/test/options_test.rb +++ b/test/options_test.rb @@ -45,7 +45,7 @@ context 'Options' do test 'basic argument assignment' do options = Asciidoctor::Cli::Options.parse!(%w(-v -s -d book test/fixtures/sample.asciidoc)) - assert_equal true, options[:verbose] + assert_equal 2, options[:verbose] assert_equal false, options[:header_footer] assert_equal 'book', options[:attributes]['doctype'] assert_equal 1, options[:input_files].size @@ -108,4 +108,24 @@ context 'Options' do assert_equal ['custom-backend', 'custom-backend-hacks'], options[:template_dirs] end + test 'should set verbose to 2 when -v flag is specified' do + options = Asciidoctor::Cli::Options.parse!(%w(-v test/fixtures/sample.asciidoc)) + assert_equal 2, options[:verbose] + end + + test 'should set verbose to 0 when -q flag is specified' do + options = Asciidoctor::Cli::Options.parse!(%w(-q test/fixtures/sample.asciidoc)) + assert_equal 0, options[:verbose] + end + + test 'should set verbose to 2 when -v flag is specified after -q flag' do + options = Asciidoctor::Cli::Options.parse!(%w(-q -v test/fixtures/sample.asciidoc)) + assert_equal 2, options[:verbose] + end + + test 'should set verbose to 0 when -q flag is specified after -v flag' do + options = Asciidoctor::Cli::Options.parse!(%w(-v -q test/fixtures/sample.asciidoc)) + assert_equal 0, options[:verbose] + end + end |
