Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
at-grandpa committed May 6, 2018
1 parent cfde90f commit 90b59a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
10 changes: 6 additions & 4 deletions spec/clim/dsl_spec/custom_help_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ require "../../dsl_spec"
command usage: main_command_of_clim_library [options] [arguments]
options:
a
b
c
--uint8=VALUE Option description. [type:UInt8]
--uint16=VALUE Option description. [type:UInt16]
--help Show this help.
HELP_MESSAGE
%}
Expand All @@ -24,10 +24,12 @@ spec(
command usage: \#{usage}
options:
\#{options_help.join("\n")}
\#{options_help}
MY_HELP
end
CUSTOM_HELP,
"option \"--uint8=VALUE\", type: UInt8",
"option \"--uint16=VALUE\", type: UInt16",
],
spec_desc: "option type spec,",
spec_cases: [
Expand Down
18 changes: 16 additions & 2 deletions src/clim/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class Clim

macro custom_help(&block)
def custom_help : String
options_help = ["a", "b", "c"]
Proc(String, String, Array(String), String).new {{ block.id }} .call(desc, usage, options_help)
Proc(String, String, String, String).new {{ block.id }} .call(desc, usage, self.parser.to_s)
end
end

Expand Down Expand Up @@ -127,6 +126,21 @@ class Clim
custom_help
end

def sub_cmds_help_lines
@sub_commands.map do |cmd|
name = name_and_alias_name(cmd) + "#{" " * (max_name_length - name_and_alias_name(cmd).size)}"
" #{name} #{cmd.desc}"
end
end

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

def name_and_alias_name(cmd)
([cmd.name] + cmd.alias_name).join(", ")
end

private def display_help? : Bool
@display_help_flag
end
Expand Down

0 comments on commit 90b59a4

Please sign in to comment.