-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include launcher options in the help for the default and
help
sub-c…
…ommands (#1725)
- Loading branch information
Showing
9 changed files
with
98 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
modules/cli/src/main/scala/scala/cli/commands/ScalaCommandWithCustomHelp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package scala.cli.commands | ||
|
||
import caseapp.core.Error | ||
import caseapp.core.help.{Help, HelpCompanion, RuntimeCommandsHelp} | ||
import caseapp.core.parser.Parser | ||
|
||
import scala.cli.commands.default.DefaultOptions | ||
import scala.cli.commands.shared.{HasLoggingOptions, ScalaCliHelp} | ||
import scala.cli.commands.util.HelpUtils.* | ||
import scala.cli.launcher.LauncherOptions | ||
|
||
abstract class ScalaCommandWithCustomHelp[T <: HasLoggingOptions]( | ||
actualHelp: => RuntimeCommandsHelp | ||
)( | ||
implicit | ||
myParser: Parser[T], | ||
help: Help[T] | ||
) extends ScalaCommand[T] { | ||
private def launcherHelp: Help[LauncherOptions] = HelpCompanion.deriveHelp[LauncherOptions] | ||
|
||
protected def customHelp(showHidden: Boolean): String = { | ||
val helpString = actualHelp.help(helpFormat, showHidden) | ||
val launcherHelpString = launcherHelp.optionsHelp(helpFormat, showHidden) | ||
s"""$helpString | ||
| | ||
|$launcherHelpString""".stripMargin | ||
} | ||
|
||
protected def customHelpAsked(showHidden: Boolean): Nothing = { | ||
println(customHelp(showHidden)) | ||
sys.exit(0) | ||
} | ||
override def helpAsked(progName: String, maybeOptions: Either[Error, T]): Nothing = | ||
customHelpAsked(showHidden = false) | ||
override def fullHelpAsked(progName: String): Nothing = customHelpAsked(showHidden = true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
modules/cli/src/main/scala/scala/cli/commands/util/HelpUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package scala.cli.commands.util | ||
|
||
import caseapp.core.help.{Help, HelpFormat} | ||
|
||
object HelpUtils { | ||
extension (help: Help[_]) { | ||
private def abstractHelp( | ||
format: HelpFormat, | ||
showHidden: Boolean | ||
)(f: (StringBuilder, HelpFormat, Boolean) => Unit): String = { | ||
val b = new StringBuilder | ||
f(b, format, showHidden) | ||
b.result() | ||
} | ||
|
||
def optionsHelp(format: HelpFormat, showHidden: Boolean = false): String = | ||
abstractHelp(format, showHidden)(help.printOptions) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters