From 5a1e6f4e132863af9301bcd9df1965c469c9c5d6 Mon Sep 17 00:00:00 2001 From: RX14 Date: Thu, 9 Apr 2020 19:31:14 +0100 Subject: [PATCH 1/2] Improve compiler single-file run syntax Now the compiler passes all arguments after the first filename to the program. --- spec/compiler/compiler_spec.cr | 11 +++++++++++ spec/compiler/data/args_test.cr | 7 +++++++ src/compiler/crystal/command.cr | 7 +++++++ 3 files changed, 25 insertions(+) create mode 100644 spec/compiler/data/args_test.cr diff --git a/spec/compiler/compiler_spec.cr b/spec/compiler/compiler_spec.cr index 56746736b7b1..71cca87529e1 100644 --- a/spec/compiler/compiler_spec.cr +++ b/spec/compiler/compiler_spec.cr @@ -27,4 +27,15 @@ describe "Compiler" do end end end + + it "treats all arguments post-filename as program arguments" do + with_tempfile "args_test" do |path| + `bin/crystal '#{compiler_datapath}/args_test.cr' -Dother_flag -- bar '#{path}'` + + File.read(path).should eq(<<-FILE) + ["-Dother_flag", "--", "bar"] + {other_flag: false} + FILE + end + end end diff --git a/spec/compiler/data/args_test.cr b/spec/compiler/data/args_test.cr new file mode 100644 index 000000000000..86efc6f59fbb --- /dev/null +++ b/spec/compiler/data/args_test.cr @@ -0,0 +1,7 @@ +# Last argument is file path to write +test_path = ARGV.pop +File.open(test_path, "w") do |f| + ARGV.inspect(f) + f.puts + {other_flag: {{flag?(:other_flag)}}}.inspect(f) +end diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index 497682293486..7a6d5b3f60aa 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -462,6 +462,13 @@ class Crystal::Command filenames << stdin_filename end + if single_file + opts.before_each do |arg| + opts.stop if !arg.starts_with?('-') && arg.ends_with?(".cr") + opts.stop if File.file?(arg) + end + end + opts.unknown_args do |before, after| opt_filenames = before opt_arguments = after From a5515df581e9e6980eed9a3677b631a7d21f7303 Mon Sep 17 00:00:00 2001 From: Stephanie Wilde-Hobbs Date: Tue, 19 May 2020 12:14:43 +0100 Subject: [PATCH 2/2] Fix args_test.cr being required as a spec --- spec/compiler/compiler_spec.cr | 2 +- spec/compiler/data/{args_test.cr => args_test} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename spec/compiler/data/{args_test.cr => args_test} (100%) diff --git a/spec/compiler/compiler_spec.cr b/spec/compiler/compiler_spec.cr index 71cca87529e1..071bba3df1c2 100644 --- a/spec/compiler/compiler_spec.cr +++ b/spec/compiler/compiler_spec.cr @@ -30,7 +30,7 @@ describe "Compiler" do it "treats all arguments post-filename as program arguments" do with_tempfile "args_test" do |path| - `bin/crystal '#{compiler_datapath}/args_test.cr' -Dother_flag -- bar '#{path}'` + `bin/crystal '#{compiler_datapath}/args_test' -Dother_flag -- bar '#{path}'` File.read(path).should eq(<<-FILE) ["-Dother_flag", "--", "bar"] diff --git a/spec/compiler/data/args_test.cr b/spec/compiler/data/args_test similarity index 100% rename from spec/compiler/data/args_test.cr rename to spec/compiler/data/args_test