Skip to content
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
30 changes: 19 additions & 11 deletions spec/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ class WelcomeCommand < Cling::Command
end
end

command = GreetCommand.new
command.add_command WelcomeCommand.new
command_with_subcommand = GreetCommand.new
command_with_subcommand.add_command WelcomeCommand.new

command_without_subcommand = GreetCommand.new
formatter = Cling::Formatter.new

describe Cling::Formatter do
it "generates a help template" do
formatter.generate(command).chomp.should eq <<-HELP
formatter.generate(command_with_subcommand).chomp.should eq <<-HELP
Greets a person

Usage:
\tgreet <arguments> [options]
\tgreet <command> <arguments> [options]

Commands:
\twelcome sends a friendly welcome message
Expand All @@ -53,11 +55,11 @@ describe Cling::Formatter do
it "generates with a custom delimiter" do
formatter.options.option_delim = '+'

formatter.generate(command).chomp.should eq <<-HELP
formatter.generate(command_with_subcommand).chomp.should eq <<-HELP
Greets a person

Usage:
\tgreet <arguments> [options]
\tgreet <command> <arguments> [options]

Commands:
\twelcome sends a friendly welcome message
Expand All @@ -74,27 +76,33 @@ describe Cling::Formatter do

it "generates a description section" do
String.build do |io|
formatter.format_description(command, io)
formatter.format_description(command_with_subcommand, io)
end.should eq "Greets a person\n\n"
end

it "generates a usage section" do
it "generates a usage section with subcommand" do
String.build do |io|
formatter.format_usage(command_with_subcommand, io)
end.should eq "Usage:\n\tgreet <command> <arguments> [options]\n\n"
end

it "generates a usage section without subcommand" do
String.build do |io|
formatter.format_usage(command, io)
formatter.format_usage(command_without_subcommand, io)
end.should eq "Usage:\n\tgreet <arguments> [options]\n\n"
end

it "generates an arguments section" do
String.build do |io|
formatter.format_arguments(command, io)
formatter.format_arguments(command_with_subcommand, io)
end.should eq "Arguments:\n\tname the name of the person (required)\n\n"
end

it "generates an options section" do
formatter.options.option_delim = '-'

String.build do |io|
formatter.format_options(command, io)
formatter.format_options(command_with_subcommand, io)
end.chomp.should eq <<-HELP
Options:
\t-h, --help sends help information
Expand Down
2 changes: 1 addition & 1 deletion spec/helper_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe Cling::MainCommand do
Runs some Crystal commands

Usage:
\tmain [options]
\tmain <command> [options]

Commands:
\tcontext
Expand Down
4 changes: 4 additions & 0 deletions src/cling/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ module Cling

if command.usage.empty?
io << "\n\t" << command.name
unless command.children.empty?
io << " <command>"
end

unless command.arguments.empty?
if command.arguments.values.any? &.required?
io << " <arguments>"
Expand Down