Skip to content

Commit 6def0c0

Browse files
authored
Merge pull request #1313 from Gedochao/fix-default-e-script-snippets
Fix backwards compat for `-e` and `--scalac-help`
2 parents f21cf98 + f12177e commit 6def0c0

File tree

8 files changed

+119
-22
lines changed

8 files changed

+119
-22
lines changed

modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ final case class SharedOptions(
4242
@Name("scalaBin")
4343
@Name("B")
4444
scalaBinaryVersion: Option[String] = None,
45-
45+
4646
@Group("Scala")
4747
@HelpMessage("Show help for scalac. This is an alias for --scalac-option -help")
48+
@Name("helpScalac")
4849
scalacHelp: Boolean = false,
4950

5051
@Recurse
5152
snippet: SnippetOptions = SnippetOptions(),
52-
53+
5354
@Recurse
5455
markdown: MarkdownOptions = MarkdownOptions(),
5556

modules/cli-options/src/main/scala/scala/cli/commands/SnippetOptions.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,32 @@ import caseapp._
66
final case class SnippetOptions(
77
@Group("Scala")
88
@HelpMessage("Allows to execute a passed string as a Scala script")
9-
@Name("e")
10-
@Name("executeScript")
9+
scriptSnippet: List[String] = List.empty,
10+
11+
@Group("Scala")
12+
@HelpMessage("A synonym to --script-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly")
13+
@Hidden
1114
@Name("executeScalaScript")
1215
@Name("executeSc")
13-
scriptSnippet: List[String] = List.empty,
16+
@Name("e")
17+
executeScript: List[String] = List.empty,
1418

1519
@Group("Scala")
1620
@HelpMessage("Allows to execute a passed string as Scala code")
17-
@Name("executeScala")
1821
scalaSnippet: List[String] = List.empty,
1922

23+
@Group("Scala")
24+
@HelpMessage("A synonym to --scala-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly")
25+
@Hidden
26+
executeScala: List[String] = List.empty,
27+
2028
@Group("Java")
2129
@HelpMessage("Allows to execute a passed string as Java code")
22-
@Name("executeJava")
2330
javaSnippet: List[String] = List.empty,
31+
32+
@Group("Java")
33+
@HelpMessage("A synonym to --scala-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly")
34+
executeJava: List[String] = List.empty,
2435
)
2536
// format: on
2637

modules/cli/src/main/scala/scala/cli/commands/Default.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ class Default(
3333
CurrentParams.verbosity = options.shared.logging.verbosity
3434
if options.version then println(Version.versionInfo(isSipScala))
3535
else
36-
(
37-
if args.remaining.nonEmpty then RunOptions.parser
38-
else ReplOptions.parser
39-
).parse(rawArgs) match
36+
{
37+
val shouldDefaultToRun =
38+
args.remaining.nonEmpty || options.shared.snippet.executeScript.nonEmpty ||
39+
options.shared.snippet.executeScala.nonEmpty || options.shared.snippet.executeJava.nonEmpty
40+
if shouldDefaultToRun then RunOptions.parser else ReplOptions.parser
41+
}.parse(rawArgs) match
4042
case Left(e) => error(e)
4143
case Right((replOptions: ReplOptions, _)) => Repl.run(replOptions, args)
4244
case Right((runOptions: RunOptions, _)) => Run.run(runOptions, args)

modules/cli/src/main/scala/scala/cli/commands/Repl.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scala.build.errors.BuildException
99
import scala.build.internal.Runner
1010
import scala.build.options.{BuildOptions, JavaOpt, Scope}
1111
import scala.cli.CurrentParams
12+
import scala.cli.commands.Run.maybePrintSimpleScalacOutput
1213
import scala.cli.commands.util.CommonOps._
1314
import scala.cli.commands.util.SharedOptionsUtil._
1415
import scala.util.Properties
@@ -60,7 +61,9 @@ object Repl extends ScalaCommand[ReplOptions] {
6061
CurrentParams.workspaceOpt = Some(inputs.workspace)
6162

6263
val initialBuildOptions = buildOptions(options)
63-
val threads = BuildThreads.create()
64+
maybePrintSimpleScalacOutput(options, initialBuildOptions)
65+
66+
val threads = BuildThreads.create()
6467

6568
val compilerMaker = options.shared.compilerMaker(threads)
6669

modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,16 @@ object SharedOptionsUtil extends CommandHelpers {
342342
workspace.forcedWorkspaceOpt,
343343
input.defaultForbiddenDirectories,
344344
input.forbid,
345-
scriptSnippetList = v.snippet.scriptSnippet,
346-
scalaSnippetList = v.snippet.scalaSnippet,
347-
javaSnippetList = v.snippet.javaSnippet,
345+
scriptSnippetList = allScriptSnippets,
346+
scalaSnippetList = allScalaSnippets,
347+
javaSnippetList = allJavaSnippets,
348348
enableMarkdown = v.markdown.enableMarkdown
349349
)
350350

351+
def allScriptSnippets: List[String] = v.snippet.scriptSnippet ++ v.snippet.executeScript
352+
def allScalaSnippets: List[String] = v.snippet.scalaSnippet ++ v.snippet.executeScala
353+
def allJavaSnippets: List[String] = v.snippet.javaSnippet ++ v.snippet.executeJava
354+
351355
def validateInputArgs(args: Seq[String]): Seq[Either[String, Seq[Inputs.Element]]] =
352356
Inputs.validateArgs(
353357
args,

modules/integration/src/test/scala/scala/cli/integration/DefaultTests.scala

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,66 @@ class DefaultTests extends ScalaCliSuite {
3434
}
3535
}
3636

37+
test("default to the run sub-command when a script snippet is passed with -e") {
38+
TestInputs.empty.fromRoot { root =>
39+
val msg = "Hello world"
40+
val quotation = TestUtil.argQuotationMark
41+
val res =
42+
os.proc(TestUtil.cli, "-e", s"println($quotation$msg$quotation)", TestUtil.extraOptions)
43+
.call(cwd = root)
44+
expect(res.out.text().trim == msg)
45+
}
46+
}
47+
48+
test("default to the run sub-command when a scala snippet is passed with --execute-scala") {
49+
TestInputs.empty.fromRoot { root =>
50+
val msg = "Hello world"
51+
val quotation = TestUtil.argQuotationMark
52+
val res =
53+
os.proc(
54+
TestUtil.cli,
55+
"--execute-scala",
56+
s"@main def main() = println($quotation$msg$quotation)",
57+
TestUtil.extraOptions
58+
)
59+
.call(cwd = root)
60+
expect(res.out.text().trim == msg)
61+
}
62+
}
63+
64+
test("default to the run sub-command when a java snippet is passed with --execute-java") {
65+
TestInputs.empty.fromRoot { root =>
66+
val msg = "Hello world"
67+
val quotation = TestUtil.argQuotationMark
68+
val res =
69+
os.proc(
70+
TestUtil.cli,
71+
"--execute-java",
72+
s"public class Main { public static void main(String[] args) { System.out.println($quotation$msg$quotation); } }",
73+
TestUtil.extraOptions
74+
)
75+
.call(cwd = root)
76+
expect(res.out.text().trim == msg)
77+
}
78+
}
79+
80+
test("running scala-cli with a script snippet passed with -e shouldn't allow repl-only options") {
81+
TestInputs.empty.fromRoot { root =>
82+
val replSpecificOption = "--repl-dry-run"
83+
val res =
84+
os.proc(
85+
TestUtil.cli,
86+
"-e",
87+
"println()",
88+
replSpecificOption,
89+
TestUtil.extraOptions
90+
)
91+
.call(cwd = root, mergeErrIntoOut = true, check = false)
92+
expect(res.exitCode == 1)
93+
expect(res.out.trim == unrecognizedArgMessage(replSpecificOption))
94+
}
95+
}
96+
3797
private def unrecognizedArgMessage(argName: String) = {
3898
val scalaCli = if (TestUtil.isNativeCli) TestUtil.cliPath else "scala-cli"
3999
s"""

modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,13 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
19681968
val msg = "Hello world"
19691969
val quotation = TestUtil.argQuotationMark
19701970
val res =
1971-
os.proc(TestUtil.cli, "run", "-e", s"println($quotation$msg$quotation)", extraOptions)
1971+
os.proc(
1972+
TestUtil.cli,
1973+
"run",
1974+
"--script-snippet",
1975+
s"println($quotation$msg$quotation)",
1976+
extraOptions
1977+
)
19721978
.call(cwd = root)
19731979
expect(res.out.text().trim == msg)
19741980
}

website/docs/reference/cli-options.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,8 @@ Set the Scala binary version
17931793

17941794
#### `--scalac-help`
17951795

1796+
Aliases: `--help-scalac`
1797+
17961798
Show help for scalac. This is an alias for --scalac-option -help
17971799

17981800
#### `--extra-jars`
@@ -1857,22 +1859,30 @@ Available in commands:
18571859

18581860
#### `--script-snippet`
18591861

1860-
Aliases: `-e`, `--execute-script`, `--execute-scala-script`, `--execute-sc`
1861-
18621862
Allows to execute a passed string as a Scala script
18631863

1864-
#### `--scala-snippet`
1864+
#### `--execute-script`
18651865

1866-
Aliases: `--execute-scala`
1866+
Aliases: `--execute-scala-script`, `--execute-sc`, `-e`
1867+
1868+
A synonym to --script-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly
1869+
1870+
#### `--scala-snippet`
18671871

18681872
Allows to execute a passed string as Scala code
18691873

1870-
#### `--java-snippet`
1874+
#### `--execute-scala`
1875+
1876+
A synonym to --scala-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly
18711877

1872-
Aliases: `--execute-java`
1878+
#### `--java-snippet`
18731879

18741880
Allows to execute a passed string as Java code
18751881

1882+
#### `--execute-java`
1883+
1884+
A synonym to --scala-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly
1885+
18761886
## Test options
18771887

18781888
Available in commands:

0 commit comments

Comments
 (0)