Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward unmodified ARGV to subcommand #631

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spec/integration/subcommand_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ describe "subcommand" do
output.should contain(%(args: ["--no-color", "--verbose", "--unknown", "other", "argument"]))
end
end

it "correctly forwards '--help' option to subcommand" do
create_shard("dummy", "0.1.0")
create_executable "dummy", "bin/shards-dummy", %(print "args: "; print ARGV)

with_path(git_path("dummy/bin")) do
output = run("shards dummy --help")
output.should contain(%(args: ["--help"]))
end
end
end

private def with_path(path)
Expand Down
8 changes: 7 additions & 1 deletion src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require "option_parser"
require "./commands/*"

module Shards
class_property? display_help : Bool = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: What's the purpose of using a class variable?
This property is entirely scoped to the .run method, so it looks like it could be a local variable there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly didn't think it too much, I followed the pattern used for other options like --no-color, but thinking it now it definitely can be scoped just to .run.

I will change it later.


def self.display_help_and_exit(opts)
puts <<-HELP
shards [<options>...] [<command>]
Expand Down Expand Up @@ -53,7 +55,7 @@ module Shards
opts.on("--ignore-crystal-version", "Has no effect. Kept for compatibility, to be removed in the future.") { }
opts.on("-v", "--verbose", "Increase the log verbosity, printing all debug statements.") { self.set_debug_log_level }
opts.on("-q", "--quiet", "Decrease the log verbosity, printing only warnings and errors.") { self.set_warning_log_level }
opts.on("-h", "--help", "Print usage synopsis.") { self.display_help_and_exit(opts) }
opts.on("-h", "--help", "Print usage synopsis.") { self.display_help = true }

opts.unknown_args do |args, options|
case args[0]? || DEFAULT_COMMAND
Expand Down Expand Up @@ -107,6 +109,10 @@ module Shards
end
end

if display_help?
display_help_and_exit(opts)
end

exit
end
end
Expand Down
Loading