diff --git a/spec/integration/subcommand_spec.cr b/spec/integration/subcommand_spec.cr new file mode 100644 index 00000000..89226a04 --- /dev/null +++ b/spec/integration/subcommand_spec.cr @@ -0,0 +1,41 @@ +require "./spec_helper" + +describe "subcommand" do + it "forwards all arguments to subcommand" do + create_shard("dummy", "0.1.0") + {% if flag?(:win32) %} + create_executable "dummy", "bin/shards-dummy", %(print ARGV.join(" ")) + {% else %} + path = create_file("dummy", "bin/shards-dummy", "#!/bin/sh\necho $@\n") + File.chmod(path, 0o755) + {% end %} + + with_path(git_path("dummy/bin")) do + output = run("shards dummy --no-color --verbose --unknown other argument") + output.should contain(%(--no-color --verbose --unknown other argument)) + end + end + + it "correctly forwards '--help' option to subcommand" do + create_shard("dummy", "0.1.0") + {% if flag?(:win32) %} + create_executable "dummy", "bin/shards-dummy", %(print ARGV.join(" ")) + {% else %} + path = create_file("dummy", "bin/shards-dummy", "#!/bin/sh\necho $@\n") + File.chmod(path, 0o755) + {% end %} + + with_path(git_path("dummy/bin")) do + output = run("shards dummy --help") + output.should contain(%(--help)) + end + end +end + +private def with_path(path) + old_path = ENV["PATH"] + ENV["PATH"] = "#{File.expand_path(path)}#{Process::PATH_DELIMITER}#{ENV["PATH"]}" + yield +ensure + ENV["PATH"] = old_path +end diff --git a/src/cli.cr b/src/cli.cr index 668c825e..beb8ad24 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -26,6 +26,8 @@ module Shards end def self.run + display_help = false + OptionParser.parse(cli_options) do |opts| path = Dir.current @@ -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.") { display_help = true } opts.unknown_args do |args, options| case args[0]? || DEFAULT_COMMAND @@ -101,12 +103,16 @@ module Shards else program_name = "shards-#{args[0]}" if program_path = Process.find_executable(program_name) - run_shards_subcommand(program_path, args) + run_shards_subcommand(program_path, cli_options) else display_help_and_exit(opts) end end + if display_help + display_help_and_exit(opts) + end + exit end end @@ -119,7 +125,7 @@ module Shards {% else %} shards_opts = ENV.fetch("SHARDS_OPTS", "").split {% end %} - ARGV.concat(shards_opts) + ARGV.dup.concat(shards_opts) end def self.run_shards_subcommand(process_name, args)