Skip to content

Commit

Permalink
Dont consume --power from ScalaCli.scala, instead add --power to Glob…
Browse files Browse the repository at this point in the history
…alOptions
  • Loading branch information
MaciejG604 committed Oct 9, 2023
1 parent 0502d25 commit d7fc8f8
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 43 deletions.
12 changes: 5 additions & 7 deletions modules/cli/src/main/scala/scala/cli/ScalaCli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,13 @@ object ScalaCli {
isSipScala = false
args0.toArray
else
// .ignoreUnrecognized drops '--'
val programArgs = args0.dropWhile(_ != "--")
val optionArgs = args0.takeWhile(_ != "--")
// Parse again to consume --power at any position
PowerOptions.parser.ignoreUnrecognized.parse(optionArgs) match {
case Right((powerOptions, unrecognizedArgs)) =>
// Parse again to register --power at any position
// Don't consume it, GlobalOptions parsing will do it
PowerOptions.parser.ignoreUnrecognized.parse(args0) match {
case Right((powerOptions, _)) =>
if powerOptions.power then
isSipScala = false
(unrecognizedArgs ++ programArgs).toArray
args0.toArray
case Left(e) =>
System.err.println(e.message)
sys.exit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract class ScalaCommand[T <: HasGlobalOptions](implicit myParser: Parser[T],
override def hasFullHelp = true
override def hidden = shouldExcludeInSip
protected var argvOpt = Option.empty[Array[String]]

private val shouldExcludeInSip =
(isRestricted || isExperimental) && !ScalaCli.allowRestrictedFeatures
override def setArgv(argv: Array[String]): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ package scala.cli.commands.shared

import caseapp.*

import scala.cli.launcher.PowerOptions

case class GlobalOptions(
@Recurse
logging: LoggingOptions = LoggingOptions(),
@Recurse
globalSuppress: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions()
globalSuppress: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions(),

/** Duplication of [[scala.cli.launcher.LauncherOptions.powerOptions]]. Thanks to this, our unit
* tests ensure that no subcommand defines an option that will clash with --power.
*/
@Recurse
powerOptions: PowerOptions = PowerOptions()
)

object GlobalOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import caseapp.*
import scala.cli.commands.shared.HelpGroup
import scala.cli.commands.tags

/** Options extracted from [[LauncherOptions]] to allow for parsing them separately. Thanks to this
* and additional parsing we can read the --power flag placed anywhere in the command invocation.
*
* This option is duplicated in [[scala.cli.commands.shared.GlobalOptions]] so that we can ensure
* that no subcommand defines its own --power option Checking for clashing names is done in unit
* tests.
*/
case class PowerOptions(
@Group(HelpGroup.Launcher.toString)
@HelpMessage("When called as 'scala', allow to use power commands too")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ class ConfigTests extends ScalaCliSuite {
)
expect(before.out.trim().isEmpty)

os.proc(TestUtil.cli, "--power", "config", "publish.user.name", name).call(
os.proc(TestUtil.cli, "config", "publish.user.name", name, "--power").call(
cwd = root,
env = configEnv
)
val res =
os.proc(TestUtil.cli, "--power", "config", "publish.user.name").call(
os.proc(TestUtil.cli, "config", "publish.user.name", "--power").call(
cwd = root,
env = configEnv
)
expect(res.out.trim() == name)

os.proc(TestUtil.cli, "--power", "config", "publish.user.name", "--unset").call(
os.proc(TestUtil.cli, "config", "publish.user.name", "--unset", "--power").call(
cwd = root,
env = configEnv
)
val after =
os.proc(TestUtil.cli, "--power", "config", "publish.user.name").call(
os.proc(TestUtil.cli, "config", "publish.user.name", "--power").call(
cwd = root,
env = configEnv
)
Expand Down
12 changes: 12 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,18 @@ Available in commands:

Key server to push / pull keys from

## Power options

Available in commands:

[`add-path`](./commands.md#add-path), [`bloop`](./commands.md#bloop), [`bloop exit`](./commands.md#bloop-exit), [`bloop output`](./commands.md#bloop-output), [`bloop start`](./commands.md#bloop-start), [`clean`](./commands.md#clean), [`config`](./commands.md#config), [`default-file`](./commands.md#default-file), [`directories`](./commands.md#directories), [`help`](./commands.md#help), [`install completions` , `install-completions`](./commands.md#install-completions), [`install-home`](./commands.md#install-home), [`new`](./commands.md#new), [`pgp pull`](./commands.md#pgp-pull), [`pgp push`](./commands.md#pgp-push), [`publish setup`](./commands.md#publish-setup), [`github secret create` , `gh secret create`](./commands.md#github-secret-create), [`github secret list` , `gh secret list`](./commands.md#github-secret-list), [`uninstall`](./commands.md#uninstall), [`uninstall completions` , `uninstall-completions`](./commands.md#uninstall-completions), [`update`](./commands.md#update), [`version`](./commands.md#version)

<!-- Automatically generated, DO NOT EDIT MANUALLY -->

### `--power`

When called as 'scala', allow to use power commands too

## Publish options

Available in commands:
Expand Down
Loading

0 comments on commit d7fc8f8

Please sign in to comment.