Permalink
Browse files

Print help if CLI is called without arguments

  • Loading branch information...
1 parent 834f003 commit bd28476b6bedc3c8fc1ca09f4153819e48efaf05 @rrrene committed Feb 4, 2014
Showing with 46 additions and 1 deletion.
  1. +36 −1 lib/sparkr/cli.rb
  2. +10 −0 test/sparkr/cli_test.rb
View
@@ -2,8 +2,43 @@
module Sparkr
class CLI
+ # Helper method to run an instance with the given +args+
+ #
+ # @see #run
+ # @return [CLI] the instance that ran
def self.run(*args)
- puts Sparkline.new(args.map(&:to_f)).to_s
+ instance = new
+ instance.run(*args)
+ instance
+ end
+
+ # Runs sparkr with the given +args+
+ # @return [void]
+ def run(*args)
+ if args.empty?
+ puts help
+ else
+ sparkline = Sparkline.new(args.map(&:to_f))
+ puts sparkline.to_s
+ end
+ end
+
+ private
+
+ # Returns usage information
+ def help
+ """
+USAGE:
+ sparkr [-h|--help] VALUE,...
+
+EXAMPLES:
+ sparkr 1 5 22 13 53
+ ▁▁▃▂█
+ sparkr 0,30,55,80,33,150
+ ▁▂▃▄▂█
+ echo 9 13 5 17 1 | sparkr
+ ▄▆▂█▁
+ """
end
end
end
View
@@ -12,4 +12,14 @@
assert err.empty?, "there should be no errors"
assert_equal "▁▁▁▁▃▁▁▁▂█\n", out
end
+
+ it ".run should print usage information if args are missing" do
+ string_numbers = %w()
+ out, err = capture_io do
+ ::Sparkr::CLI.run(*string_numbers)
+ end
+ refute out.empty?, "there should be some output"
+ assert err.empty?, "there should be no errors"
+ assert_match /USAGE\:/, out
+ end
end

0 comments on commit bd28476

Please sign in to comment.