Skip to content

Commit 3a3b1b9

Browse files
authored
Allow to override prog name with a launcher arg (#2891)
1 parent 7fc6714 commit 3a3b1b9

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

modules/cli/src/main/scala/scala/cli/ScalaCli.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ object ScalaCli {
2727
// the Scala CLI native image, no need to manually load it.
2828
coursier.jniutils.LoadWindowsLibrary.assumeInitialized()
2929

30-
val progName = {
31-
val argv0 = (new Argv0).get("scala-cli")
30+
private val defaultProgName = "scala-cli"
31+
var progName: String = {
32+
val argv0 = (new Argv0).get(defaultProgName)
3233
val last = Paths.get(argv0).getFileName.toString
3334
last match {
3435
case s".${name}.aux" =>
@@ -243,6 +244,9 @@ object ScalaCli {
243244
&& sys.props.get("scala-cli.kind").exists(_.startsWith("jvm")) =>
244245
JavaLauncherCli.runAndExit(args)
245246
case None =>
247+
launcherOpts.progName.foreach { pn =>
248+
progName = pn
249+
}
246250
if launcherOpts.cliUserScalaVersion.nonEmpty then
247251
defaultScalaVersion = launcherOpts.cliUserScalaVersion
248252
if launcherOpts.cliPredefinedRepository.nonEmpty then

modules/cli/src/main/scala/scala/cli/launcher/LauncherOptions.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ final case class LauncherOptions(
3737
@Name("repository")
3838
@Name("predefinedRepository")
3939
cliPredefinedRepository: List[String] = Nil,
40+
@Group(HelpGroup.Launcher.toString)
41+
@HelpMessage("This allows to override the program name identified by Scala CLI as itself (the default is 'scala-cli')")
42+
@Hidden
43+
@Tag(tags.implementation)
44+
progName: Option[String] = None,
4045
@Recurse
4146
powerOptions: PowerOptions = PowerOptions()
4247
)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,18 @@ class SipScalaTests extends ScalaCliSuite with SbtTestHelper with MillTestHelper
667667
}
668668
}
669669

670+
test("prog name override launcher arg allows to change the identified launcher name") {
671+
TestInputs.empty.fromRoot { root =>
672+
val progName = "some-weird-launcher-name-some-dev-thought-of"
673+
val invalidSubCommandName = "foo"
674+
val res = os.proc(TestUtil.cli, "--prog-name", progName, invalidSubCommandName)
675+
.call(cwd = root, stderr = os.Pipe, check = false)
676+
expect(res.exitCode == 1)
677+
expect(res.err.trim().contains(progName))
678+
expect(res.err.trim().contains(invalidSubCommandName))
679+
}
680+
}
681+
670682
test(s"default Scala version override launcher option is respected by the Mill export") {
671683
val input = "printVersion.sc"
672684
val code = """println(s"Default version: ${scala.util.Properties.versionNumberString}")"""

0 commit comments

Comments
 (0)