From cfde90fef31631b866fd5813e882f09a8d251a3b Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Sun, 6 May 2018 12:04:33 +0900 Subject: [PATCH 1/7] ... --- spec/clim/dsl_spec/custom_help_spec.cr | 40 ++++++++++++++++++++++++++ src/clim/command.cr | 13 ++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 spec/clim/dsl_spec/custom_help_spec.cr diff --git a/spec/clim/dsl_spec/custom_help_spec.cr b/spec/clim/dsl_spec/custom_help_spec.cr new file mode 100644 index 00000000..a97a7abf --- /dev/null +++ b/spec/clim/dsl_spec/custom_help_spec.cr @@ -0,0 +1,40 @@ +require "../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + command description: Command Line Interface Tool. + command usage: main_command_of_clim_library [options] [arguments] + + options: + a + b + c + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + <<-CUSTOM_HELP + custom_help do |desc, usage, options_help| + <<-MY_HELP + command description: \#{desc} + command usage: \#{usage} + + options: + \#{options_help.join("\n")} + MY_HELP + end + CUSTOM_HELP, + ], + spec_desc: "option type spec,", + spec_cases: [ + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + ] +) +{% end %} diff --git a/src/clim/command.cr b/src/clim/command.cr index bf884a01..d1773c5e 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -61,6 +61,17 @@ class Clim def define_version(parser) end + 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) + end + end + + def custom_help : String + Help.new(self).display + end + macro main_command {% raise "Can not be declared 'main_command' as sub command." if @type.superclass.id.stringify == "Clim::Command" %} end @@ -113,7 +124,7 @@ class Clim end private def help - Help.new(self).display + custom_help end private def display_help? : Bool From 90b59a4861c15c935d16ecaca1a7de3e016637e4 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Sun, 6 May 2018 12:48:50 +0900 Subject: [PATCH 2/7] ... --- spec/clim/dsl_spec/custom_help_spec.cr | 10 ++++++---- src/clim/command.cr | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/spec/clim/dsl_spec/custom_help_spec.cr b/spec/clim/dsl_spec/custom_help_spec.cr index a97a7abf..29b325cc 100644 --- a/spec/clim/dsl_spec/custom_help_spec.cr +++ b/spec/clim/dsl_spec/custom_help_spec.cr @@ -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 %} @@ -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: [ diff --git a/src/clim/command.cr b/src/clim/command.cr index d1773c5e..d4b66a4f 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -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 @@ -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 From 1c742b18863609cca54ef11dcd8b87b76a314473 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Tue, 8 May 2018 00:56:10 +0900 Subject: [PATCH 3/7] ... --- src/clim/command.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/clim/command.cr b/src/clim/command.cr index d4b66a4f..cfa31b22 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -62,12 +62,12 @@ class Clim end macro custom_help(&block) - def custom_help : String + def custom_help_def : String Proc(String, String, String, String).new {{ block.id }} .call(desc, usage, self.parser.to_s) end end - def custom_help : String + def custom_help_def : String Help.new(self).display end @@ -123,7 +123,7 @@ class Clim end private def help - custom_help + custom_help_def end def sub_cmds_help_lines From 241df21e99cb5a62e601f87bdd5b214102a3ad5b Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Thu, 31 May 2018 08:29:26 +0900 Subject: [PATCH 4/7] ... --- spec/clim/dsl_spec/custom_help_spec.cr | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/clim/dsl_spec/custom_help_spec.cr b/spec/clim/dsl_spec/custom_help_spec.cr index 29b325cc..58f6109f 100644 --- a/spec/clim/dsl_spec/custom_help_spec.cr +++ b/spec/clim/dsl_spec/custom_help_spec.cr @@ -3,8 +3,8 @@ require "../../dsl_spec" {% begin %} {% main_help_message = <<-HELP_MESSAGE - command description: Command Line Interface Tool. - command usage: main_command_of_clim_library [options] [arguments] + command description: my desc message. + command usage: my usage message. options: --uint8=VALUE Option description. [type:UInt8] @@ -28,6 +28,8 @@ spec( MY_HELP end CUSTOM_HELP, + "desc \"my desc message.\"", + "usage \"my usage message.\"", "option \"--uint8=VALUE\", type: UInt8", "option \"--uint16=VALUE\", type: UInt16", ], From 355b980571b31ba7467defdd0c08884ef62ab83c Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Thu, 31 May 2018 08:38:54 +0900 Subject: [PATCH 5/7] readme --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 50c777da..a98a6536 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ _"clim" = "cli" + "slim"_ - [x] Required flag for option - [x] Nested sub commands - [x] `--help` option +- [x] Customizable help message - [x] `version` macro - [x] Command name alias @@ -431,6 +432,33 @@ class MyCli < Clim end ``` +#### custom_help + +You can customize the help message. In the `custom_help` block, you need to return a `String`. + +```crystal +class MyCli < Clim + main_command do + desc "my desc message." + usage "my usage message." + option "-n", type: String, desc: "name." + option "--my-age", type: Int32, desc: "age." + custom_help do |desc, usage, options_help| + <<-MY_HELP + command description: \#{desc} + command usage: \#{usage} + + options: + \#{options_help} + MY_HELP + end + run do |options, arguments| + puts options.help + end + end +end +``` + ### help string ```crystal From b92bcbce9a0dfe42ffa3fe8d3a74a2cf703278ae Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Thu, 31 May 2018 08:58:16 +0900 Subject: [PATCH 6/7] ... --- README.md | 17 ++++++++++++++--- src/clim/command.cr | 15 --------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a98a6536..dc371ffb 100644 --- a/README.md +++ b/README.md @@ -445,11 +445,11 @@ class MyCli < Clim option "--my-age", type: Int32, desc: "age." custom_help do |desc, usage, options_help| <<-MY_HELP - command description: \#{desc} - command usage: \#{usage} + command description: #{desc} + command usage: #{usage} options: - \#{options_help} + #{options_help} MY_HELP end run do |options, arguments| @@ -459,6 +459,17 @@ class MyCli < Clim end ``` +```console +$ crystal run src/custom_help_test.cr +command description: my desc message. +command usage: my usage message. + +options: + -n name. [type:String] + --my-age age. [type:Int32] + --help Show this help. +``` + ### help string ```crystal diff --git a/src/clim/command.cr b/src/clim/command.cr index cfa31b22..d441fb38 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -126,21 +126,6 @@ class Clim custom_help_def 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 From d4bd5cf525a379461e6d0073fdec76710e1e6b2b Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Thu, 31 May 2018 09:21:25 +0900 Subject: [PATCH 7/7] fix version --- README.md | 2 +- shard.yml | 2 +- src/clim/version.cr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dc371ffb..7b9dc0e4 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Add this to your application's `shard.yml`: dependencies: clim: github: at-grandpa/clim - version: 0.3.0 + version: 0.3.1 ``` ## Minimum sample diff --git a/shard.yml b/shard.yml index 16c68d61..897b266b 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: clim -version: 0.3.0 +version: 0.3.1 authors: - at-grandpa <@at_grandpa> diff --git a/src/clim/version.cr b/src/clim/version.cr index e3a0314e..c3d54872 100644 --- a/src/clim/version.cr +++ b/src/clim/version.cr @@ -1,3 +1,3 @@ class Clim - VERSION = "0.3.0" + VERSION = "0.3.1" end