Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle prmd init with no arguments #292

Merged
merged 1 commit into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/prmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
8 changes: 4 additions & 4 deletions lib/prmd/cli/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def make_parser(options = {})
# @param [Array<String>] argv
# @return [Array<String>] 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.
Expand Down Expand Up @@ -66,10 +66,10 @@ def set_option(options, key, value)
# @return [Hash<Symbol, Object>] 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
Expand Down
7 changes: 6 additions & 1 deletion lib/prmd/cli/generate.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative 'base'
require_relative '../commands/init'
require_relative '../utils'

module Prmd
module CLI
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/prmd/utils.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions test/utils_test.rb
Original file line number Diff line number Diff line change
@@ -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