diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2014-02-17 02:31:08 -0700 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2014-02-17 02:31:08 -0700 |
| commit | d7131fd84aa599625050fad7e2179279256569f8 (patch) | |
| tree | c7f963f204067a6b6b6604935614c596a8cfc4c6 | |
| parent | 455652ece0effa9d0670a8bc250795de6c46db69 (diff) | |
| parent | a8efe0393888c01fb71c53c702a446df6c212d39 (diff) | |
Merge pull request #902 from mojavelinux/issue-574
resolves #574 add -r and -I flags to asciidoctor command
| -rw-r--r-- | lib/asciidoctor/cli/options.rb | 18 | ||||
| -rw-r--r-- | test/options_test.rb | 25 | ||||
| -rw-r--r-- | test/test_helper.rb | 3 |
3 files changed, 40 insertions, 6 deletions
diff --git a/lib/asciidoctor/cli/options.rb b/lib/asciidoctor/cli/options.rb index ce5d857a..080a0cfa 100644 --- a/lib/asciidoctor/cli/options.rb +++ b/lib/asciidoctor/cli/options.rb @@ -23,6 +23,7 @@ module Asciidoctor self[:eruby] = options[:eruby] || nil self[:compact] = options[:compact] || false self[:verbose] = options[:verbose] || 1 + self[:load_paths] = options[:load_paths] || nil self[:requires] = options[:requires] || nil self[:base_dir] = options[:base_dir] self[:destination_dir] = options[:destination_dir] || nil @@ -109,10 +110,13 @@ 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('-r', '--require LIBRARY', ::Array, - 'require the specified library before executing the processor (calls Kernel.require)', - 'may be specified more than once') do |paths| - self[:requires] = paths + opts.on('-IDIRECTORY', '--load-path LIBRARY', 'add a directory to the $LOAD_PATH', + 'may be specified more than once') do |path| + (self[:load_paths] ||= []) << path + end + opts.on('-rLIBRARY', '--require LIBRARY', 'require the specified library before executing the processor (using require)', + 'may be specified more than once') do |path| + (self[:requires] ||= []) << path end opts.on('-q', '--quiet', 'suppress warnings (default: false)') do |verbose| self[:verbose] = 0 @@ -191,6 +195,12 @@ Example: asciidoctor -b html5 source.asciidoc end end + if (load_paths = self[:load_paths]) + load_paths.reverse_each do |path| + $:.unshift File.expand_path(path) + end + end + if (requires = self[:requires]) requires.each do |path| begin diff --git a/test/options_test.rb b/test/options_test.rb index 099016df..2cba24f4 100644 --- a/test/options_test.rb +++ b/test/options_test.rb @@ -112,6 +112,31 @@ context 'Options' do assert_equal ['custom-backend', 'custom-backend-hacks'], options[:template_dirs] end + test '-r option requires specified libraries' do + options = Asciidoctor::Cli::Options.new + redirect_streams do |stdout, stderr| + exitval = options.parse! %w(-r foobar -r foobaz test/fixtures/sample.asciidoc) + assert_match(%(asciidoctor: FAILED: 'foobar' could not be loaded), stderr.string) + assert_equal 1, exitval + assert_equal ['foobar', 'foobaz'], options[:requires] + end + end + + test '-I option appends directories to $LOAD_PATH' do + options = Asciidoctor::Cli::Options.new + old_load_path = $LOAD_PATH.dup + begin + exitval = options.parse! %w(-I foobar -I foobaz test/fixtures/sample.asciidoc) + assert_not_equal 1, exitval + assert_equal old_load_path.size + 2, $LOAD_PATH.size + assert_equal File.expand_path('foobar'), $LOAD_PATH[0] + assert_equal File.expand_path('foobaz'), $LOAD_PATH[1] + assert_equal ['foobar', 'foobaz'], options[:load_paths] + ensure + ($LOAD_PATH.size - old_load_path.size).times { $LOAD_PATH.shift } + end + 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] diff --git a/test/test_helper.rb b/test/test_helper.rb index 408724d9..0064b2fd 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,12 +1,11 @@ ASCIIDOCTOR_PROJECT_DIR = File.dirname File.dirname(__FILE__) Dir.chdir ASCIIDOCTOR_PROJECT_DIR -$:.unshift File.join(ASCIIDOCTOR_PROJECT_DIR, 'lib') if RUBY_VERSION < '1.9' require 'rubygems' end -require 'asciidoctor' +require File.join(ASCIIDOCTOR_PROJECT_DIR, 'lib', 'asciidoctor') require 'test/unit' require 'nokogiri' |
