Skip to content

Commit 48b7295

Browse files
authored
Forward unmodified ARGV to subcommand (#631)
* Pass all ARGV arguments verbatim to subcommand Avoid altering original `ARGV` when combining it with possible `SHARDS_OPTS` variable and pass them verbatim to the subcommand. Introduce a naive integration test for subcommand to validate the change works correctly. * Fixes passing --help to subcommand Avoid OptionParser to short-circuit `--help` and return immediately by setting a flag for it and evaluating at the end of the processing of unknown options. This is only done for the CLI invocation and is not part of Shards module (as the help and usage options are only available in this context). * Only use dummy executable on Windows Follow the pattern used in other tests and use simple `sh` script on non-Windows platforms. * Avoid usage of class properties to handle help behavior Revert the usage of class property introduced in 5a6dc6c and leverage instead on a pure instance variable in the context of `Shards.run`.
1 parent 655a3b0 commit 48b7295

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

spec/integration/subcommand_spec.cr

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require "./spec_helper"
2+
3+
describe "subcommand" do
4+
it "forwards all arguments to subcommand" do
5+
create_shard("dummy", "0.1.0")
6+
{% if flag?(:win32) %}
7+
create_executable "dummy", "bin/shards-dummy", %(print ARGV.join(" "))
8+
{% else %}
9+
path = create_file("dummy", "bin/shards-dummy", "#!/bin/sh\necho $@\n")
10+
File.chmod(path, 0o755)
11+
{% end %}
12+
13+
with_path(git_path("dummy/bin")) do
14+
output = run("shards dummy --no-color --verbose --unknown other argument")
15+
output.should contain(%(--no-color --verbose --unknown other argument))
16+
end
17+
end
18+
19+
it "correctly forwards '--help' option to subcommand" do
20+
create_shard("dummy", "0.1.0")
21+
{% if flag?(:win32) %}
22+
create_executable "dummy", "bin/shards-dummy", %(print ARGV.join(" "))
23+
{% else %}
24+
path = create_file("dummy", "bin/shards-dummy", "#!/bin/sh\necho $@\n")
25+
File.chmod(path, 0o755)
26+
{% end %}
27+
28+
with_path(git_path("dummy/bin")) do
29+
output = run("shards dummy --help")
30+
output.should contain(%(--help))
31+
end
32+
end
33+
end
34+
35+
private def with_path(path)
36+
old_path = ENV["PATH"]
37+
ENV["PATH"] = "#{File.expand_path(path)}#{Process::PATH_DELIMITER}#{ENV["PATH"]}"
38+
yield
39+
ensure
40+
ENV["PATH"] = old_path
41+
end

src/cli.cr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module Shards
2626
end
2727

2828
def self.run
29+
display_help = false
30+
2931
OptionParser.parse(cli_options) do |opts|
3032
path = Dir.current
3133

@@ -53,7 +55,7 @@ module Shards
5355
opts.on("--ignore-crystal-version", "Has no effect. Kept for compatibility, to be removed in the future.") { }
5456
opts.on("-v", "--verbose", "Increase the log verbosity, printing all debug statements.") { self.set_debug_log_level }
5557
opts.on("-q", "--quiet", "Decrease the log verbosity, printing only warnings and errors.") { self.set_warning_log_level }
56-
opts.on("-h", "--help", "Print usage synopsis.") { self.display_help_and_exit(opts) }
58+
opts.on("-h", "--help", "Print usage synopsis.") { display_help = true }
5759

5860
opts.unknown_args do |args, options|
5961
case args[0]? || DEFAULT_COMMAND
@@ -101,12 +103,16 @@ module Shards
101103
else
102104
program_name = "shards-#{args[0]}"
103105
if program_path = Process.find_executable(program_name)
104-
run_shards_subcommand(program_path, args)
106+
run_shards_subcommand(program_path, cli_options)
105107
else
106108
display_help_and_exit(opts)
107109
end
108110
end
109111

112+
if display_help
113+
display_help_and_exit(opts)
114+
end
115+
110116
exit
111117
end
112118
end
@@ -119,7 +125,7 @@ module Shards
119125
{% else %}
120126
shards_opts = ENV.fetch("SHARDS_OPTS", "").split
121127
{% end %}
122-
ARGV.concat(shards_opts)
128+
ARGV.dup.concat(shards_opts)
123129
end
124130

125131
def self.run_shards_subcommand(process_name, args)

0 commit comments

Comments
 (0)