mirror of
https://github.com/apple/pkl.git
synced 2026-03-21 00:29:07 +01:00
Bump clikt to 5.0.3 (#947)
This bumps Clikt from version 3 to version 5, which, among other things, improves the help text formatting with colors. Also: * Add `--version` flag to pkldoc, pkl-codegen-java, pkl-codegen-kotlin * Add help text to pkldoc, pkl-codegen-java, pkl-codegen-kotlin
This commit is contained in:
@@ -16,9 +16,15 @@
|
||||
package org.pkl.commons.cli.commands
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.core.Context
|
||||
import com.github.ajalt.clikt.parameters.groups.provideDelegate
|
||||
|
||||
abstract class BaseCommand(name: String, helpLink: String, help: String = "") :
|
||||
CliktCommand(name = name, help = help, epilog = "For more information, visit $helpLink") {
|
||||
abstract class BaseCommand(name: String, private val helpLink: String) : CliktCommand(name = name) {
|
||||
abstract val helpString: String
|
||||
|
||||
override fun help(context: Context) = helpString
|
||||
|
||||
final override fun helpEpilog(context: Context) = "For more information, visit $helpLink"
|
||||
|
||||
val baseOptions: BaseOptions by BaseOptions()
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ class BaseOptions : OptionGroup() {
|
||||
names = arrayOf("--allowed-modules"),
|
||||
help = "URI patterns that determine which modules can be loaded and evaluated.",
|
||||
)
|
||||
.convert("<pattern1,pattern2>") { Pattern.compile(it) }
|
||||
.convert("pattern1,pattern2") { Pattern.compile(it) }
|
||||
.splitAll()
|
||||
|
||||
val allowedResources: List<Pattern> by
|
||||
@@ -111,7 +111,7 @@ class BaseOptions : OptionGroup() {
|
||||
names = arrayOf("--allowed-resources"),
|
||||
help = "URI patterns that determine which external resources can be read.",
|
||||
)
|
||||
.convert("<pattern1,pattern2>") { Pattern.compile(it) }
|
||||
.convert("pattern1,pattern2") { Pattern.compile(it) }
|
||||
.splitAll()
|
||||
|
||||
val rootDir: Path? by
|
||||
@@ -140,7 +140,7 @@ class BaseOptions : OptionGroup() {
|
||||
val properties: Map<String, String> by
|
||||
option(
|
||||
names = arrayOf("-p", "--property"),
|
||||
metavar = "<name=value>",
|
||||
metavar = "name=value",
|
||||
help = "External property to set (repeatable).",
|
||||
)
|
||||
.associateProps()
|
||||
@@ -148,7 +148,7 @@ class BaseOptions : OptionGroup() {
|
||||
val color: Color by
|
||||
option(
|
||||
names = arrayOf("--color"),
|
||||
metavar = "<when>",
|
||||
metavar = "when",
|
||||
help =
|
||||
"Whether to format messages in ANSI color. Possible values of <when> are 'never', 'auto', and 'always'.",
|
||||
)
|
||||
@@ -171,7 +171,7 @@ class BaseOptions : OptionGroup() {
|
||||
val envVars: Map<String, String> by
|
||||
option(
|
||||
names = arrayOf("-e", "--env-var"),
|
||||
metavar = "<name=value>",
|
||||
metavar = "name=value",
|
||||
help = "Environment variable to set (repeatable).",
|
||||
)
|
||||
.associate()
|
||||
@@ -179,7 +179,7 @@ class BaseOptions : OptionGroup() {
|
||||
val modulePath: List<Path> by
|
||||
option(
|
||||
names = arrayOf("--module-path"),
|
||||
metavar = "<path1${File.pathSeparator}path2>",
|
||||
metavar = "path1${File.pathSeparator}path2",
|
||||
help =
|
||||
"Directories, ZIP archives, or JAR archives to search when resolving `modulepath:` URIs.",
|
||||
)
|
||||
@@ -194,7 +194,7 @@ class BaseOptions : OptionGroup() {
|
||||
val timeout: Duration? by
|
||||
option(
|
||||
names = arrayOf("-t", "--timeout"),
|
||||
metavar = "<number>",
|
||||
metavar = "number",
|
||||
help = "Duration, in seconds, after which evaluation of a source module will be timed out.",
|
||||
)
|
||||
.single()
|
||||
@@ -204,7 +204,7 @@ class BaseOptions : OptionGroup() {
|
||||
val caCertificates: List<Path> by
|
||||
option(
|
||||
names = arrayOf("--ca-certificates"),
|
||||
metavar = "<path>",
|
||||
metavar = "path",
|
||||
help = "Only trust CA certificates from the provided file(s).",
|
||||
)
|
||||
.path()
|
||||
@@ -213,7 +213,7 @@ class BaseOptions : OptionGroup() {
|
||||
val proxy: URI? by
|
||||
option(
|
||||
names = arrayOf("--http-proxy"),
|
||||
metavar = "<address>",
|
||||
metavar = "address",
|
||||
help = "Proxy to use for HTTP(S) connections.",
|
||||
)
|
||||
.single()
|
||||
@@ -229,7 +229,7 @@ class BaseOptions : OptionGroup() {
|
||||
val noProxy: List<String>? by
|
||||
option(
|
||||
names = arrayOf("--http-no-proxy"),
|
||||
metavar = "<pattern1,pattern2>",
|
||||
metavar = "pattern1,pattern",
|
||||
help = "Hostnames that should not be connected to via a proxy.",
|
||||
)
|
||||
.single()
|
||||
@@ -238,7 +238,7 @@ class BaseOptions : OptionGroup() {
|
||||
val externalModuleReaders: Map<String, ExternalReader> by
|
||||
option(
|
||||
names = arrayOf("--external-module-reader"),
|
||||
metavar = "<scheme>='<executable>[ <arguments>]'",
|
||||
metavar = "<scheme>='<executable> [<arguments>]'",
|
||||
help = "External reader registrations for module URI schemes",
|
||||
)
|
||||
.parseExternalReader("=")
|
||||
@@ -248,7 +248,7 @@ class BaseOptions : OptionGroup() {
|
||||
val externalResourceReaders: Map<String, ExternalReader> by
|
||||
option(
|
||||
names = arrayOf("--external-resource-reader"),
|
||||
metavar = "<scheme>='<executable>[ <arguments>]'",
|
||||
metavar = "<scheme>='<executable> [<arguments>]'",
|
||||
help = "External reader registrations for resource URI schemes",
|
||||
)
|
||||
.parseExternalReader("=")
|
||||
|
||||
@@ -21,10 +21,10 @@ import com.github.ajalt.clikt.parameters.arguments.multiple
|
||||
import com.github.ajalt.clikt.parameters.groups.provideDelegate
|
||||
import java.net.URI
|
||||
|
||||
abstract class ModulesCommand(name: String, helpLink: String, help: String = "") :
|
||||
BaseCommand(name = name, help = help, helpLink = helpLink) {
|
||||
abstract class ModulesCommand(name: String, helpLink: String) :
|
||||
BaseCommand(name = name, helpLink = helpLink) {
|
||||
open val modules: List<URI> by
|
||||
argument(name = "<modules>", help = "Module paths or URIs to evaluate.")
|
||||
argument(name = "modules", help = "Module paths or URIs to evaluate.")
|
||||
.convert { BaseOptions.parseModuleName(it) }
|
||||
.multiple(required = true)
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ fun <EachT : Any, ValueT> NullableOption<EachT, ValueT>.splitAll(
|
||||
transformEach = { it },
|
||||
transformAll = { it.flatten().ifEmpty { default } },
|
||||
validator = {},
|
||||
nvalues = 1,
|
||||
valueSplit = Regex.fromLiteral(separator),
|
||||
nvalues = 1..1,
|
||||
valueSplit = { it.split(Regex.fromLiteral(separator)) },
|
||||
)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ class ProjectOptions : OptionGroup() {
|
||||
val projectDir: Path? by
|
||||
option(
|
||||
names = arrayOf("--project-dir"),
|
||||
metavar = "<path>",
|
||||
metavar = "path",
|
||||
help =
|
||||
"The project directory to use for this command. By default, searches up from the working directory for a PklProject file.",
|
||||
)
|
||||
|
||||
@@ -26,7 +26,7 @@ class TestOptions : OptionGroup() {
|
||||
private val junitReportDir: Path? by
|
||||
option(
|
||||
names = arrayOf("--junit-reports"),
|
||||
metavar = "<dir>",
|
||||
metavar = "dir",
|
||||
help = "Directory where to store JUnit reports.",
|
||||
)
|
||||
.path()
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.commons.cli.commands
|
||||
|
||||
import com.github.ajalt.clikt.core.BaseCliktCommand
|
||||
import com.github.ajalt.clikt.core.context
|
||||
import com.github.ajalt.clikt.core.installMordantMarkdown
|
||||
import com.github.ajalt.clikt.core.terminal
|
||||
import com.github.ajalt.clikt.parameters.options.versionOption
|
||||
import com.github.ajalt.mordant.rendering.TextStyle
|
||||
import com.github.ajalt.mordant.rendering.Theme
|
||||
import com.github.ajalt.mordant.terminal.Terminal
|
||||
import org.pkl.core.Release
|
||||
|
||||
private val theme = Theme { styles["markdown.code.span"] = TextStyle(bold = true) }
|
||||
|
||||
fun <T : BaseCliktCommand<T>> T.installCommonOptions() {
|
||||
installMordantMarkdown()
|
||||
|
||||
versionOption(Release.current().versionInfo, names = setOf("-v", "--version"), message = { it })
|
||||
|
||||
context { terminal = Terminal(theme = theme) }
|
||||
}
|
||||
@@ -17,6 +17,7 @@ package org.pkl.commons.cli
|
||||
|
||||
import com.github.ajalt.clikt.core.BadParameterValue
|
||||
import com.github.ajalt.clikt.core.PrintHelpMessage
|
||||
import com.github.ajalt.clikt.core.parse
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import java.util.regex.Pattern
|
||||
@@ -31,12 +32,14 @@ class BaseCommandTest {
|
||||
private val cmd =
|
||||
object : BaseCommand("test", "") {
|
||||
override fun run() = Unit
|
||||
|
||||
override val helpString: String = ""
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `invalid timeout`() {
|
||||
val e = assertThrows<BadParameterValue> { cmd.parse(arrayOf("--timeout", "abc")) }
|
||||
assertThat(e).hasMessageContaining("timeout")
|
||||
assertThat(e.message).isEqualTo("abc is not a valid integer")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.pkl.commons.cli
|
||||
|
||||
import com.github.ajalt.clikt.core.parse
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.pkl.commons.cli.commands.BaseCommand
|
||||
@@ -32,6 +33,8 @@ class CliCommandTest {
|
||||
private val cmd =
|
||||
object : BaseCommand("test", "") {
|
||||
override fun run() = Unit
|
||||
|
||||
override val helpString: String = ""
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user