diff --git a/lib/prmd.rb b/lib/prmd.rb index 3719b03..f699701 100644 --- a/lib/prmd.rb +++ b/lib/prmd.rb @@ -3,6 +3,7 @@ require_relative 'prmd/commands' require_relative 'prmd/schema' require_relative 'prmd/link' +require_relative 'prmd/utils' require_relative 'prmd/template' require_relative 'prmd/url_generator' require_relative 'prmd/hash_helpers' diff --git a/lib/prmd/cli/base.rb b/lib/prmd/cli/base.rb index 3c1c864..045dab0 100644 --- a/lib/prmd/cli/base.rb +++ b/lib/prmd/cli/base.rb @@ -33,8 +33,8 @@ def make_parser(options = {}) # @param [Array] argv # @return [Array] remaining arguments # @private - def execute_parser(parser, argv) - parser.parse(argv) + def execute_parser(argv) + @parser.parse(argv) end # Set the given key and value in the given options Hash. @@ -66,10 +66,10 @@ def set_option(options, key, value) # @return [Hash] parsed options def parse_options(argv, options = {}) opts = {} - parser = make_parser(options) do |key, value| + @parser = make_parser(options) do |key, value| set_option(opts, key, value) end - argv = execute_parser(parser, argv) + argv = execute_parser(argv) opts[:argv] = argv opts end diff --git a/lib/prmd/cli/generate.rb b/lib/prmd/cli/generate.rb index 2944ed7..ba53e80 100644 --- a/lib/prmd/cli/generate.rb +++ b/lib/prmd/cli/generate.rb @@ -1,5 +1,6 @@ require_relative 'base' require_relative '../commands/init' +require_relative '../utils' module Prmd module CLI @@ -40,7 +41,11 @@ def self.make_parser(options = {}) # @return (see Prmd::CLI::Base#execute) def self.execute(options = {}) name = options.fetch(:argv).first - write_result Prmd.init(name, options), options + if Prmd::Utils.blank?(name) + abort @parser + else + write_result Prmd.init(name, options), options + end end end end diff --git a/lib/prmd/utils.rb b/lib/prmd/utils.rb new file mode 100644 index 0000000..bb5f68f --- /dev/null +++ b/lib/prmd/utils.rb @@ -0,0 +1,19 @@ +module Prmd + # For any tid bits, or core extension methods, without the "core" extension + module Utils + # For checking if the string contains only spaces + BLANK_REGEX = /\A\s+\z/ + + def self.blank?(obj) + if obj.nil? + true + elsif obj.is_a?(String) + obj.empty? || !!(obj =~ BLANK_REGEX) + elsif obj.respond_to?(:empty?) + obj.empty? + else + false + end + end + end +end diff --git a/test/utils_test.rb b/test/utils_test.rb new file mode 100644 index 0000000..46a9f1b --- /dev/null +++ b/test/utils_test.rb @@ -0,0 +1,16 @@ +require_relative 'helpers' + +class UtilsTest < Minitest::Test + def test_blank? + assert_equal true, Prmd::Utils.blank?(nil) + assert_equal true, Prmd::Utils.blank?([]) + assert_equal true, Prmd::Utils.blank?({}) + assert_equal true, Prmd::Utils.blank?("") + assert_equal true, Prmd::Utils.blank?(" ") + assert_equal true, Prmd::Utils.blank?(" ") + assert_equal false, Prmd::Utils.blank?([nil]) + assert_equal false, Prmd::Utils.blank?({ a: nil }) + assert_equal false, Prmd::Utils.blank?("A") + assert_equal false, Prmd::Utils.blank?(Object.new) + end +end