From b3897f317134e1a9614c530a3950e3fa08ad8ca5 Mon Sep 17 00:00:00 2001 From: Alex Duzsardi Date: Tue, 23 Mar 2021 00:54:36 +0200 Subject: [PATCH 1/7] Skip options validation when asking for version --- src/clim/command/options.cr | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/clim/command/options.cr b/src/clim/command/options.cr index 04a403df..f243624b 100644 --- a/src/clim/command/options.cr +++ b/src/clim/command/options.cr @@ -57,11 +57,15 @@ class Clim def required_validate! opts = self.dup - if opts.responds_to?(:help) + case + when opts.responds_to?(:help) return if opts.help + when opts.responds_to?(:version) + return if opts.version + else + return if invalid_required_names.empty? + raise ClimInvalidOptionException.new "Required options. \"#{invalid_required_names.join("\", \"")}\"" end - return if invalid_required_names.empty? - raise ClimInvalidOptionException.new "Required options. \"#{invalid_required_names.join("\", \"")}\"" end private def invalid_required_names From 5f90dbb98d9189b045c21294eaeac066fd2c3358 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 9 Apr 2021 02:16:07 +0900 Subject: [PATCH 2/7] fix stdout_spec.cr --- spec/clim/stdout_spec/stdout_spec.cr | 118 ++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 3 deletions(-) diff --git a/spec/clim/stdout_spec/stdout_spec.cr b/spec/clim/stdout_spec/stdout_spec.cr index 76f4492a..4b9ee652 100644 --- a/spec/clim/stdout_spec/stdout_spec.cr +++ b/spec/clim/stdout_spec/stdout_spec.cr @@ -189,7 +189,7 @@ describe "STDOUT spec, " do DISPLAY end it "exception message. (Required options)" do - `crystal run spec/clim/stdout_spec/files/exception_message.cr --no-color -- `.should eq <<-DISPLAY + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- `.should eq <<-DISPLAY ERROR: Required options. "--prefix " Please see the `--help`. @@ -197,7 +197,7 @@ describe "STDOUT spec, " do DISPLAY end it "exception message. (Option that requires an argument)" do - `crystal run spec/clim/stdout_spec/files/exception_message.cr --no-color -- --prefix`.should eq <<-DISPLAY + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --prefix`.should eq <<-DISPLAY ERROR: Option that requires an argument. "--prefix" Please see the `--help`. @@ -205,11 +205,123 @@ describe "STDOUT spec, " do DISPLAY end it "exception message. (Option that requires an argument)" do - `crystal run spec/clim/stdout_spec/files/exception_message.cr --no-color -- --prefix=foo`.should eq <<-DISPLAY + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --prefix=foo`.should eq <<-DISPLAY ERROR: Required arguments. "arg1" Please see the `--help`. + DISPLAY + end + it "For --help, options and arguments are not required." do + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --help`.should eq <<-DISPLAY + + Command Line Interface Tool. + + Usage: + + hello + + Options: + + --prefix Prefix. [type:String] [required] + -h, --help Show this help. + -v, --version Show version. + + Arguments: + + 01. arg1 argument1 [type:String] [required] + + + DISPLAY + end + it "For -h, options and arguments are not required." do + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- -h`.should eq <<-DISPLAY + + Command Line Interface Tool. + + Usage: + + hello + + Options: + + --prefix Prefix. [type:String] [required] + -h, --help Show this help. + -v, --version Show version. + + Arguments: + + 01. arg1 argument1 [type:String] [required] + + + DISPLAY + end + it "If --help is specified along with options or arguments, a help message will be displayed." do + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --prefix=foo arg1 --help `.should eq <<-DISPLAY + + Command Line Interface Tool. + + Usage: + + hello + + Options: + + --prefix Prefix. [type:String] [required] + -h, --help Show this help. + -v, --version Show version. + + Arguments: + + 01. arg1 argument1 [type:String] [required] + + + DISPLAY + end + it "If -h is specified along with options or arguments, a help message will be displayed." do + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --prefix=foo arg1 -h `.should eq <<-DISPLAY + + Command Line Interface Tool. + + Usage: + + hello + + Options: + + --prefix Prefix. [type:String] [required] + -h, --help Show this help. + -v, --version Show version. + + Arguments: + + 01. arg1 argument1 [type:String] [required] + + + DISPLAY + end + it "For --version, options and arguments are not required." do + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --version`.should eq <<-DISPLAY + Version 0.1.0 + + DISPLAY + end + it "For -v, options and arguments are not required." do + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- -v`.should eq <<-DISPLAY + Version 0.1.0 + + DISPLAY + end + it "If --version is specified along with options or arguments, a version message will be displayed." do + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --prefix=foo arg1 --version `.should eq <<-DISPLAY + Version 0.1.0 + + DISPLAY + end + it "If -v is specified along with options or arguments, a version message will be displayed." do + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --prefix=foo arg1 -v `.should eq <<-DISPLAY + Version 0.1.0 + DISPLAY end end From 9eaa632b17055ef71ba449946ce39bdc1c7d0246 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 9 Apr 2021 02:16:30 +0900 Subject: [PATCH 3/7] fix required.cr --- .../stdout_spec/files/{exception_message.cr => required.cr} | 2 ++ 1 file changed, 2 insertions(+) rename spec/clim/stdout_spec/files/{exception_message.cr => required.cr} (84%) diff --git a/spec/clim/stdout_spec/files/exception_message.cr b/spec/clim/stdout_spec/files/required.cr similarity index 84% rename from spec/clim/stdout_spec/files/exception_message.cr rename to spec/clim/stdout_spec/files/required.cr index 99d8aca8..91de0d9e 100644 --- a/spec/clim/stdout_spec/files/exception_message.cr +++ b/spec/clim/stdout_spec/files/required.cr @@ -4,6 +4,8 @@ module Hello class Cli < Clim main do usage "hello " + help short: "-h" + version "Version 0.1.0", short: "-v" option "--prefix ", type: String, desc: "Prefix.", required: true argument "arg1", type: String, desc: "argument1", required: true run do |opts, args| From 5ac664d5042a597707caeb5ff9fcc4460ae8daea Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 9 Apr 2021 02:17:06 +0900 Subject: [PATCH 4/7] fix required_validate! in arguments --- src/clim/command/arguments.cr | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/clim/command/arguments.cr b/src/clim/command/arguments.cr index e25613ad..5db9f513 100644 --- a/src/clim/command/arguments.cr +++ b/src/clim/command/arguments.cr @@ -26,10 +26,9 @@ class Clim def set_argv(@argv : Array(String)) end - def required_validate!(options : Options) - if options.responds_to?(:help) - return if options.help - end + def required_validate!(opts : Options) + return if opts.responds_to?(:help) && opts.help + return if opts.responds_to?(:version) && opts.version return if invalid_required_names.empty? raise ClimInvalidOptionException.new "Required arguments. \"#{invalid_required_names.join("\", \"")}\"" end From c4959570f3cedb817b00fdc94ca8b5ba0ddf4fad Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 9 Apr 2021 02:17:23 +0900 Subject: [PATCH 5/7] fix required_validate! in options --- src/clim/command/options.cr | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/clim/command/options.cr b/src/clim/command/options.cr index f243624b..d3ea231d 100644 --- a/src/clim/command/options.cr +++ b/src/clim/command/options.cr @@ -57,15 +57,10 @@ class Clim def required_validate! opts = self.dup - case - when opts.responds_to?(:help) - return if opts.help - when opts.responds_to?(:version) - return if opts.version - else - return if invalid_required_names.empty? - raise ClimInvalidOptionException.new "Required options. \"#{invalid_required_names.join("\", \"")}\"" - end + return if opts.responds_to?(:help) && opts.help + return if opts.responds_to?(:version) && opts.version + return if invalid_required_names.empty? + raise ClimInvalidOptionException.new "Required options. \"#{invalid_required_names.join("\", \"")}\"" end private def invalid_required_names From eff92846b10f8d20fe244fc618bf7903e2a602f9 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 9 Apr 2021 19:29:15 +0900 Subject: [PATCH 6/7] add bash completion to required_validate! --- src/clim/command/arguments.cr | 1 + src/clim/command/options.cr | 1 + 2 files changed, 2 insertions(+) diff --git a/src/clim/command/arguments.cr b/src/clim/command/arguments.cr index 5db9f513..2fa0f3f3 100644 --- a/src/clim/command/arguments.cr +++ b/src/clim/command/arguments.cr @@ -29,6 +29,7 @@ class Clim def required_validate!(opts : Options) return if opts.responds_to?(:help) && opts.help return if opts.responds_to?(:version) && opts.version + return if opts.responds_to?(:bash_completion) && opts.bash_completion return if invalid_required_names.empty? raise ClimInvalidOptionException.new "Required arguments. \"#{invalid_required_names.join("\", \"")}\"" end diff --git a/src/clim/command/options.cr b/src/clim/command/options.cr index d3ea231d..5c44d826 100644 --- a/src/clim/command/options.cr +++ b/src/clim/command/options.cr @@ -59,6 +59,7 @@ class Clim opts = self.dup return if opts.responds_to?(:help) && opts.help return if opts.responds_to?(:version) && opts.version + return if opts.responds_to?(:bash_completion) && opts.bash_completion return if invalid_required_names.empty? raise ClimInvalidOptionException.new "Required options. \"#{invalid_required_names.join("\", \"")}\"" end From 58873e08534d3ed6af1e2c5cfa7ef875a2180668 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 9 Apr 2021 19:29:54 +0900 Subject: [PATCH 7/7] add bash completion spec to stdout_spec --- spec/clim/stdout_spec/stdout_spec.cr | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/spec/clim/stdout_spec/stdout_spec.cr b/spec/clim/stdout_spec/stdout_spec.cr index 4b9ee652..22df3520 100644 --- a/spec/clim/stdout_spec/stdout_spec.cr +++ b/spec/clim/stdout_spec/stdout_spec.cr @@ -324,4 +324,58 @@ describe "STDOUT spec, " do DISPLAY end + it "For --bash-completion, options and arguments are not required." do + expected_regex = <<-'DISPLAY' + _.*\(\) + \{ + \ \ \ \ local\ program\=\$\{COMP_WORDS\[0\]\} + \ \ \ \ local\ cmd\=\$\{COMP_WORDS\[1\]\} + \ \ \ \ local\ cur\="\$\{COMP_WORDS\[COMP_CWORD\]\}" + \ \ \ \ local\ prev\="\$\{COMP_WORDS\[COMP_CWORD\-1\]\}" + \ \ \ \ local\ cword\="\$\{COMP_CWORD\}" + + \ \ \ \ + if\ \[\[\ "\$\{prev\}"\ \=\=\ ".*"\ \]\]\ ;\ then + \ \ \ \ COMPREPLY\=\(\ \$\(compgen\ \-W\ "\-v\ \-\-version\ \-\-prefix\ \-h\ \-\-help"\ \-\-\ \$\{cur\}\)\ \) + else + \ \ \ \ COMPREPLY\=\(\ \$\(compgen\ \-f\ \$\{cur\}\)\ \) + fi + + + \ \ \ \ return\ 0 + \} + + complete\ \-F\ _.* .* + + DISPLAY + + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --bash-completion`.should match(/#{expected_regex}/) + end + it "If --bash-completion is specified along with options or arguments, a bash completion string will be displayed." do + expected_regex = <<-'DISPLAY' + _.*\(\) + \{ + \ \ \ \ local\ program\=\$\{COMP_WORDS\[0\]\} + \ \ \ \ local\ cmd\=\$\{COMP_WORDS\[1\]\} + \ \ \ \ local\ cur\="\$\{COMP_WORDS\[COMP_CWORD\]\}" + \ \ \ \ local\ prev\="\$\{COMP_WORDS\[COMP_CWORD\-1\]\}" + \ \ \ \ local\ cword\="\$\{COMP_CWORD\}" + + \ \ \ \ + if\ \[\[\ "\$\{prev\}"\ \=\=\ ".*"\ \]\]\ ;\ then + \ \ \ \ COMPREPLY\=\(\ \$\(compgen\ \-W\ "\-v\ \-\-version\ \-\-prefix\ \-h\ \-\-help"\ \-\-\ \$\{cur\}\)\ \) + else + \ \ \ \ COMPREPLY\=\(\ \$\(compgen\ \-f\ \$\{cur\}\)\ \) + fi + + + \ \ \ \ return\ 0 + \} + + complete\ \-F\ _.* .* + + DISPLAY + + `crystal run spec/clim/stdout_spec/files/required.cr --no-color -- --prefix=foo arg1 --bash-completion`.should match(/#{expected_regex}/) + end end