diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-10-01 17:21:45 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-10-01 17:21:45 +0100 |
commit | 5e700000d575aa69b08050aa39f19873fdf0a08f (patch) | |
tree | 7a56be21e79c8b612c55a144751d7eaaf6b5f462 | |
parent | ff6b74052dac041403241e011a6ff432f123d2ab (diff) | |
download | morph-5e700000d575aa69b08050aa39f19873fdf0a08f.tar.gz |
import: Put Ruby option parsing into base class
-rw-r--r-- | import/importer_base.rb | 13 | ||||
-rwxr-xr-x | import/rubygems.to_chunk | 28 |
2 files changed, 29 insertions, 12 deletions
diff --git a/import/importer_base.rb b/import/importer_base.rb index fcf602cd..820d53c1 100644 --- a/import/importer_base.rb +++ b/import/importer_base.rb @@ -19,12 +19,25 @@ require 'json' require 'logger' +require 'optparse' require 'yaml' module Importer class Base private + def create_option_parser(banner, description) + opts = OptionParser.new + + opts.banner = banner + + opts.on('-?', '--help', 'print this help') do + puts opts + print "\n", description + exit 1 + end + end + def log @logger ||= create_logger end diff --git a/import/rubygems.to_chunk b/import/rubygems.to_chunk index b8561bcf..cc21fb83 100755 --- a/import/rubygems.to_chunk +++ b/import/rubygems.to_chunk @@ -23,6 +23,15 @@ require 'optparse' require_relative 'importer_base' +DESCRIPTION = <<-END +This tool reads the Gemfile and optionally the Gemfile.lock from a Ruby project +source tree in SOURCE_DIR. It outputs a chunk morphology for GEM_NAME on +stdout. If VERSION is supplied, it is used to check that the build instructions +will produce the expected version of the Gem. + +It is intended for use with the `baserock-import` tool. +END + class << Bundler def default_gemfile # This is a hack to make things not crash when there's no Gemfile @@ -42,25 +51,20 @@ class RubyGemChunkMorphologyGenerator < Importer::Base end def parse_options(arguments) - # No options so far .. opts = OptionParser.new opts.banner = "Usage: rubygems.to_chunk SOURCE_DIR GEM_NAME [VERSION]" - opts.separator "" - opts.separator "This tool reads the Gemfile and optionally the " + - "Gemfile.lock from a Ruby project " - opts.separator "source tree in SOURCE_DIR. It outputs a chunk " + - "morphology for GEM_NAME on stdout." - opts.separator "If VERSION is supplied, it is used to check that " + - "the build instructions will" - opts.separator "produce the expected version of the Gem." - opts.separator "" - opts.separator "It is intended for use with the `baserock-import` tool." + + opts.on('-?', '--help', 'print this help') do + puts opts + print "\n", DESCRIPTION + exit 1 + end parsed_arguments = opts.parse!(arguments) if parsed_arguments.length != 2 && parsed_arguments.length != 3 - STDERR.puts opts.help + opts.parse(['-?']) exit 1 end |