Skip to content

Fix backwards compat for -e and --scalac-help #1313

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

Merged
merged 3 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ final case class SharedOptions(
@Name("scalaBin")
@Name("B")
scalaBinaryVersion: Option[String] = None,

@Group("Scala")
@HelpMessage("Show help for scalac. This is an alias for --scalac-option -help")
@Name("helpScalac")
scalacHelp: Boolean = false,

@Recurse
snippet: SnippetOptions = SnippetOptions(),

@Recurse
markdown: MarkdownOptions = MarkdownOptions(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ import caseapp._
final case class SnippetOptions(
@Group("Scala")
@HelpMessage("Allows to execute a passed string as a Scala script")
@Name("e")
@Name("executeScript")
scriptSnippet: List[String] = List.empty,

@Group("Scala")
@HelpMessage("A synonym to --script-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly")
@Hidden
@Name("executeScalaScript")
@Name("executeSc")
scriptSnippet: List[String] = List.empty,
@Name("e")
executeScript: List[String] = List.empty,

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

@Group("Scala")
@HelpMessage("A synonym to --scala-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly")
@Hidden
executeScala: List[String] = List.empty,

@Group("Java")
@HelpMessage("Allows to execute a passed string as Java code")
@Name("executeJava")
javaSnippet: List[String] = List.empty,

@Group("Java")
@HelpMessage("A synonym to --scala-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly")
executeJava: List[String] = List.empty,
)
// format: on

Expand Down
10 changes: 6 additions & 4 deletions modules/cli/src/main/scala/scala/cli/commands/Default.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class Default(
CurrentParams.verbosity = options.shared.logging.verbosity
if options.version then println(Version.versionInfo(isSipScala))
else
(
if args.remaining.nonEmpty then RunOptions.parser
else ReplOptions.parser
).parse(rawArgs) match
{
val shouldDefaultToRun =
args.remaining.nonEmpty || options.shared.snippet.executeScript.nonEmpty ||
options.shared.snippet.executeScala.nonEmpty || options.shared.snippet.executeJava.nonEmpty
if shouldDefaultToRun then RunOptions.parser else ReplOptions.parser
}.parse(rawArgs) match
case Left(e) => error(e)
case Right((replOptions: ReplOptions, _)) => Repl.run(replOptions, args)
case Right((runOptions: RunOptions, _)) => Run.run(runOptions, args)
Expand Down
5 changes: 4 additions & 1 deletion modules/cli/src/main/scala/scala/cli/commands/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import scala.build.errors.BuildException
import scala.build.internal.Runner
import scala.build.options.{BuildOptions, JavaOpt, Scope}
import scala.cli.CurrentParams
import scala.cli.commands.Run.maybePrintSimpleScalacOutput
import scala.cli.commands.util.CommonOps._
import scala.cli.commands.util.SharedOptionsUtil._
import scala.util.Properties
Expand Down Expand Up @@ -60,7 +61,9 @@ object Repl extends ScalaCommand[ReplOptions] {
CurrentParams.workspaceOpt = Some(inputs.workspace)

val initialBuildOptions = buildOptions(options)
val threads = BuildThreads.create()
maybePrintSimpleScalacOutput(options, initialBuildOptions)

val threads = BuildThreads.create()

val compilerMaker = options.shared.compilerMaker(threads)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,16 @@ object SharedOptionsUtil extends CommandHelpers {
workspace.forcedWorkspaceOpt,
input.defaultForbiddenDirectories,
input.forbid,
scriptSnippetList = v.snippet.scriptSnippet,
scalaSnippetList = v.snippet.scalaSnippet,
javaSnippetList = v.snippet.javaSnippet,
scriptSnippetList = allScriptSnippets,
scalaSnippetList = allScalaSnippets,
javaSnippetList = allJavaSnippets,
enableMarkdown = v.markdown.enableMarkdown
)

def allScriptSnippets: List[String] = v.snippet.scriptSnippet ++ v.snippet.executeScript
def allScalaSnippets: List[String] = v.snippet.scalaSnippet ++ v.snippet.executeScala
def allJavaSnippets: List[String] = v.snippet.javaSnippet ++ v.snippet.executeJava

def validateInputArgs(args: Seq[String]): Seq[Either[String, Seq[Inputs.Element]]] =
Inputs.validateArgs(
args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,66 @@ class DefaultTests extends ScalaCliSuite {
}
}

test("default to the run sub-command when a script snippet is passed with -e") {
TestInputs.empty.fromRoot { root =>
val msg = "Hello world"
val quotation = TestUtil.argQuotationMark
val res =
os.proc(TestUtil.cli, "-e", s"println($quotation$msg$quotation)", TestUtil.extraOptions)
.call(cwd = root)
expect(res.out.text().trim == msg)
}
}

test("default to the run sub-command when a scala snippet is passed with --execute-scala") {
TestInputs.empty.fromRoot { root =>
val msg = "Hello world"
val quotation = TestUtil.argQuotationMark
val res =
os.proc(
TestUtil.cli,
"--execute-scala",
s"@main def main() = println($quotation$msg$quotation)",
TestUtil.extraOptions
)
.call(cwd = root)
expect(res.out.text().trim == msg)
}
}

test("default to the run sub-command when a java snippet is passed with --execute-java") {
TestInputs.empty.fromRoot { root =>
val msg = "Hello world"
val quotation = TestUtil.argQuotationMark
val res =
os.proc(
TestUtil.cli,
"--execute-java",
s"public class Main { public static void main(String[] args) { System.out.println($quotation$msg$quotation); } }",
TestUtil.extraOptions
)
.call(cwd = root)
expect(res.out.text().trim == msg)
}
}

test("running scala-cli with a script snippet passed with -e shouldn't allow repl-only options") {
TestInputs.empty.fromRoot { root =>
val replSpecificOption = "--repl-dry-run"
val res =
os.proc(
TestUtil.cli,
"-e",
"println()",
replSpecificOption,
TestUtil.extraOptions
)
.call(cwd = root, mergeErrIntoOut = true, check = false)
expect(res.exitCode == 1)
expect(res.out.trim == unrecognizedArgMessage(replSpecificOption))
}
}

private def unrecognizedArgMessage(argName: String) = {
val scalaCli = if (TestUtil.isNativeCli) TestUtil.cliPath else "scala-cli"
s"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,13 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
val msg = "Hello world"
val quotation = TestUtil.argQuotationMark
val res =
os.proc(TestUtil.cli, "run", "-e", s"println($quotation$msg$quotation)", extraOptions)
os.proc(
TestUtil.cli,
"run",
"--script-snippet",
s"println($quotation$msg$quotation)",
extraOptions
)
.call(cwd = root)
expect(res.out.text().trim == msg)
}
Expand Down
22 changes: 16 additions & 6 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,8 @@ Set the Scala binary version

#### `--scalac-help`

Aliases: `--help-scalac`

Show help for scalac. This is an alias for --scalac-option -help

#### `--extra-jars`
Expand Down Expand Up @@ -1857,22 +1859,30 @@ Available in commands:

#### `--script-snippet`

Aliases: `-e`, `--execute-script`, `--execute-scala-script`, `--execute-sc`

Allows to execute a passed string as a Scala script

#### `--scala-snippet`
#### `--execute-script`

Aliases: `--execute-scala`
Aliases: `--execute-scala-script`, `--execute-sc`, `-e`

A synonym to --script-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly

#### `--scala-snippet`

Allows to execute a passed string as Scala code

#### `--java-snippet`
#### `--execute-scala`

A synonym to --scala-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly

Aliases: `--execute-java`
#### `--java-snippet`

Allows to execute a passed string as Java code

#### `--execute-java`

A synonym to --scala-snippet, which defaults the sub-command to `run` when no sub-command is passed explicitly

## Test options

Available in commands:
Expand Down