Skip to content

Commit

Permalink
pass
Browse files Browse the repository at this point in the history
  • Loading branch information
at-grandpa committed Dec 17, 2018
1 parent d98ca65 commit 9cfb138
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
19 changes: 13 additions & 6 deletions spec/clim/command/help_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ describe Clim::Command::Help do
help.sub_commands_name_and_alias_name(sub_command2).should eq ["abcdef", "ghijkl", "mnopqr"]
end
end
describe "#options_info" do
describe "#options" do
it "returns options info." do
help = Clim::Command::Help.new(SpecCommand.command)
help.options_info.should eq ({
help: [
help.options.should eq ({
help_lines: [
" -g WORDS, --greeting=WORDS Words of greetings. [type:String] [default:\"Hello\"]",
" -n NAME Target name. [type:Array(String)] [default:[\"Taro\"]] [required]",
],
Expand All @@ -167,11 +167,11 @@ describe Clim::Command::Help do
})
end
end
describe "#sub_commands_info" do
describe "#sub_commands" do
it "returns sub commands info." do
help = Clim::Command::Help.new(SpecCommand.command)
help.sub_commands_info.should eq ({
help: [
help.sub_commands.should eq ({
help_lines: [
" abc, def, ghi abc command.",
" abcdef, ghijkl, mnopqr abcdef command.",
],
Expand All @@ -187,5 +187,12 @@ describe Clim::Command::Help do
],
})
end
it "returns sub commands info without sub commands." do
help = Clim::Command::Help.new(SpecCommandNoSubCommands.command)
help.sub_commands.should eq ({
help_lines: [] of String,
info: [] of Array(NamedTuple(name: Array(String), desc: String)),
})
end
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
require "./../../../../src/clim"

class MyCli < Clim
help_template do |desc, usage, options_help, sub_commands_help|
help_template do |desc, usage, options, sub_commands|
<<-MY_HELP
command description: #{desc}
command usage: #{usage}
options:
#{options_help}
#{options[:help_lines].join("\n")}
sub_commands:
#{sub_commands_help}
#{sub_commands[:help_lines].join("\n")}
MY_HELP
Expand Down
22 changes: 14 additions & 8 deletions src/clim.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ require "./clim/*"
class Clim
include Types

alias HelpTemplateType = Proc(String, String, String, String, String)

DEAFULT_HELP_TEMPLATE = HelpTemplateType.new do |desc, usage, options_help, sub_commands_help|
{% begin %}
{% support_types = SUPPORT_TYPES.map { |k, _| k } + [Nil] %}
alias HelpOptionsInfoType = Array(NamedTuple(name: Array(String), type: {{ support_types.map(&.stringify.+(".class")).join(" | ").id }}, desc: String, default: {{ support_types.join(" | ").id }}, required: Bool))
{% end %}
alias HelpOptionsType = NamedTuple(help_lines: Array(String), info: HelpOptionsInfoType)
alias HelpSubCommandsType = NamedTuple(help_lines: Array(String), info: Array(NamedTuple(name: Array(String), desc: String)))
alias HelpTemplateType = Proc(String, String, HelpOptionsType, HelpSubCommandsType, String)

DEAFULT_HELP_TEMPLATE = HelpTemplateType.new do |desc, usage, options, sub_commands|
base_help_template = <<-HELP_MESSAGE
#{desc}
Expand All @@ -16,33 +22,33 @@ class Clim
Options:
#{options_help}
#{options[:help_lines].join("\n")}
HELP_MESSAGE

sub_commands_help_template = <<-HELP_MESSAGE
Sub Commands:
#{sub_commands_help}
#{sub_commands[:help_lines].join("\n")}
HELP_MESSAGE
sub_commands_help.empty? ? base_help_template : base_help_template + sub_commands_help_template
sub_commands[:help_lines].empty? ? base_help_template : base_help_template + sub_commands_help_template
end

class Clim::Command
def help_template_def
help = Help.new(self)
DEAFULT_HELP_TEMPLATE.call(help.desc, help.usage, help.parser.to_s, help.sub_cmds_help_display)
DEAFULT_HELP_TEMPLATE.call(help.desc, help.usage, help.options, help.sub_commands)
end
end

macro help_template(&block)
class Clim::Command
def help_template_def
help = Help.new(self)
Proc(String, String, String, String, String).new {{ block.stringify.id }} .call(help.desc, help.usage, help.parser.to_s, help.sub_cmds_help_display)
HelpTemplateType.new {{ block.stringify.id }} .call(help.desc, help.usage, help.options, help.sub_commands)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions src/clim/command/help.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ class Clim
([cmd.name] + cmd.alias_name)
end

def options_info
def options
{
help: @command.parser.to_s.split("\n"),
info: @command.options_info,
help_lines: @command.parser.to_s.split("\n"),
info: @command.options_info,
}
end

def sub_commands_info
def sub_commands
sub_commands_info = @command.sub_commands.map do |cmd|
{
name: sub_commands_name_and_alias_name(cmd),
desc: cmd.desc,
}
end
{
help: sub_cmds_help_lines,
info: sub_commands_info,
help_lines: sub_cmds_help_lines,
info: sub_commands_info,
}
end
end
Expand Down
10 changes: 5 additions & 5 deletions src/clim/command/options.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class Clim

def info
{% begin %}
{% support_types = SUPPORT_TYPES.map { |k, _| k } %}
array = [] of NamedTuple(name: Array(String), type: {{ support_types.map(&.stringify.+(".class")).join(" | ").id }}, desc: String, default: {{ support_types.join(" | ").id }}, required: Bool)
{% for iv in @type.instance_vars.reject { |iv| iv.stringify == "help" } %}
array << {{iv}}.to_named_tuple
{% end %}
{% support_types = SUPPORT_TYPES.map { |k, _| k } + [Nil] %}
array = [] of NamedTuple(name: Array(String), type: {{ support_types.map(&.stringify.+(".class")).join(" | ").id }}, desc: String, default: {{ support_types.join(" | ").id }}, required: Bool)
{% for iv in @type.instance_vars.reject { |iv| iv.stringify == "help" } %}
array << {{iv}}.to_named_tuple
{% end %}
{% end %}
end
end
Expand Down

0 comments on commit 9cfb138

Please sign in to comment.