Skip to content

Commit

Permalink
Merge pull request #81 from aduzsardi/fix-version
Browse files Browse the repository at this point in the history
Skip options validation when asking for version
  • Loading branch information
at-grandpa authored Apr 9, 2021
2 parents f88585e + 58873e0 commit c1f2111
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Hello
class Cli < Clim
main do
usage "hello <name>"
help short: "-h"
version "Version 0.1.0", short: "-v"
option "--prefix <text>", type: String, desc: "Prefix.", required: true
argument "arg1", type: String, desc: "argument1", required: true
run do |opts, args|
Expand Down
172 changes: 169 additions & 3 deletions spec/clim/stdout_spec/stdout_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -189,27 +189,193 @@ 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 <text>"
Please see the `--help`.
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`.
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 <name>
Options:
--prefix <text> 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 <name>
Options:
--prefix <text> 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 <name>
Options:
--prefix <text> 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 <name>
Options:
--prefix <text> 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
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
8 changes: 4 additions & 4 deletions src/clim/command/arguments.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ 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 opts.responds_to?(:bash_completion) && opts.bash_completion
return if invalid_required_names.empty?
raise ClimInvalidOptionException.new "Required arguments. \"#{invalid_required_names.join("\", \"")}\""
end
Expand Down
6 changes: 3 additions & 3 deletions src/clim/command/options.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class Clim

def required_validate!
opts = self.dup
if opts.responds_to?(:help)
return if opts.help
end
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
Expand Down

0 comments on commit c1f2111

Please sign in to comment.