Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
at-grandpa committed Jul 1, 2018
1 parent 5dddfa0 commit c12e0a9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 33 deletions.
3 changes: 2 additions & 1 deletion spec/clim/dsl_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ macro it_blocks(class_name, spec_case)
{% end %}
end

macro spec(spec_class_name, spec_dsl_lines, spec_desc, spec_cases, spec_class_define_lines = [] of StringLiteral)
macro spec(spec_class_name, spec_desc, spec_cases, spec_dsl_lines = [] of StringLiteral, spec_class_define_lines = [] of StringLiteral, spec_sub_command_lines = [] of StringLiteral)
{% for spec_case, index in spec_cases %}
{% class_name = (spec_class_name.stringify + index.stringify).id %}

Expand All @@ -53,6 +53,7 @@ macro spec(spec_class_name, spec_dsl_lines, spec_desc, spec_cases, spec_class_de
run do |opts, args|
assert_opts_and_args({{spec_case}})
end
expand_lines({{spec_sub_command_lines}})
end
end

Expand Down
25 changes: 24 additions & 1 deletion spec/clim/dsl_spec/main_command_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,10 @@ spec(
--uint16=VALUE Option description. [type:UInt16]
--help Show this help.
sub_commands:
sub_command1 sub command 1.
sub_command2 sub command 2.
HELP_MESSAGE
%}

Expand All @@ -680,17 +684,36 @@ spec(
"option \"--uint8=VALUE\", type: UInt8",
"option \"--uint16=VALUE\", type: UInt16",
<<-CUSTOM_HELP
custom_help do |desc, usage, options_help|
custom_help do |desc, usage, options_help, sub_commands|
<<-MY_HELP
command description: \#{desc}
command usage: \#{usage}
options:
\#{options_help}
sub_commands:
\#{sub_commands}
MY_HELP
end
CUSTOM_HELP
],
spec_sub_command_lines: [
<<-SUB_COMMAND1,
sub_command "sub_command1" do
desc "sub command 1."
run do |options, arguments|
end
end
SUB_COMMAND1
<<-SUB_COMMAND2,
sub_command "sub_command2" do
desc "sub command 2."
run do |options, arguments|
end
end
SUB_COMMAND2
],
spec_desc: "option type spec,",
spec_cases: [
{
Expand Down
42 changes: 12 additions & 30 deletions spec/clim/dsl_spec/sub_command_spec.cr
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
require "../dsl_spec"

macro spec_for_sub_commands(spec_class_name, spec_cases)
{% for spec_case, index in spec_cases %}
{% class_name = (spec_class_name.stringify + index.stringify).id %}

# define dsl
class {{class_name}} < Clim
main_command do
run do |opts, args|
assert_opts_and_args({{spec_case}})
end
sub_command "sub_command" do
desc "Sub command with desc."
usage "sub_command with usage [options] [arguments]"
run do |opts, args|
assert_opts_and_args({{spec_case}})
end
end
end
end

# spec
describe "sub command with desc and usage," do
describe "if argv is " + {{spec_case["argv"].stringify}} + "," do
it_blocks({{class_name}}, {{spec_case}})
end
end
{% end %}
end

{% begin %}
{%
main_help_message = <<-HELP_MESSAGE
Expand Down Expand Up @@ -66,8 +37,19 @@ end
HELP_MESSAGE
%}

spec_for_sub_commands(
spec(
spec_class_name: SubCommandWithDescAndUsage,
spec_sub_command_lines: [
<<-SUB_COMMAND,
sub_command "sub_command" do
desc "Sub command with desc."
usage "sub_command with usage [options] [arguments]"
run do |options, arguments|
end
end
SUB_COMMAND
],
spec_desc: "option type spec,",
spec_cases: [
{
argv: [] of String,
Expand Down
3 changes: 2 additions & 1 deletion src/clim/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class Clim
macro custom_help(&block)
{% raise "Can not be declared 'custom_help' as sub command." unless @type.id.stringify.split("::").last == "Command_Main_command_of_clim_library" %}
def custom_help_def : String
Proc(String, String, String, String).new {{ block.id }} .call(desc, usage, self.parser.to_s)
help = Help.new(self)
Proc(String, String, String, String, String).new {{ block.id }} .call(help.desc, help.usage, help.parser.to_s, help.sub_cmds_help_display)
end
end

Expand Down
8 changes: 8 additions & 0 deletions src/clim/command/help.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Clim
HELP_MESSAGE
end

delegate desc, to: @command
delegate usage, to: @command
delegate parser, to: @command

def sub_cmds_help
<<-HELP_MESSAGE
Sub Commands:
Expand All @@ -42,6 +46,10 @@ class Clim
end
end

def sub_cmds_help_display
sub_cmds_help_lines.join("\n")
end

def max_name_length
@command.sub_commands.empty? ? 0 : @command.sub_commands.map { |cmd| name_and_alias_name(cmd).size }.max
end
Expand Down

0 comments on commit c12e0a9

Please sign in to comment.