Turn CLI commands into objects, self register subcommands (#935)

Usages of `RootCommand` no longer need to initialize a new instance, nor register subcommands.
This commit is contained in:
Daniel Chao
2025-02-05 09:18:23 -08:00
committed by GitHub
parent aadcccd0fc
commit 9784cd7265
15 changed files with 229 additions and 253 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* 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.
@@ -28,10 +28,10 @@ import org.pkl.core.Release
/** Main method for the Java code generator CLI. */
internal fun main(args: Array<String>) {
cliMain { PklJavaCodegenCommand().main(args) }
cliMain { PklJavaCodegenCommand.main(args) }
}
class PklJavaCodegenCommand :
object PklJavaCodegenCommand :
ModulesCommand(
name = "pkl-codegen-java",
helpLink = Release.current().documentation().homepage(),
@@ -43,7 +43,7 @@ class PklJavaCodegenCommand :
option(
names = arrayOf("-o", "--output-dir"),
metavar = "<path>",
help = "The directory where generated source code is placed."
help = "The directory where generated source code is placed.",
)
.path()
.default(defaults.outputDir)
@@ -52,7 +52,7 @@ class PklJavaCodegenCommand :
option(
names = arrayOf("--indent"),
metavar = "<chars>",
help = "The characters to use for indenting generated source code."
help = "The characters to use for indenting generated source code.",
)
.default(defaults.indent)
@@ -61,21 +61,21 @@ class PklJavaCodegenCommand :
names = arrayOf("--generate-getters"),
help =
"Whether to generate public getter methods and " +
"private final fields instead of public final fields."
"private final fields instead of public final fields.",
)
.flag()
private val generateJavadoc: Boolean by
option(
names = arrayOf("--generate-javadoc"),
help = "Whether to preserve Pkl doc comments by generating corresponding Javadoc comments."
help = "Whether to preserve Pkl doc comments by generating corresponding Javadoc comments.",
)
.flag()
private val generateSpringBoot: Boolean by
option(
names = arrayOf("--generate-spring-boot"),
help = "Whether to generate config classes for use with Spring Boot."
help = "Whether to generate config classes for use with Spring Boot.",
)
.flag()
@@ -83,7 +83,7 @@ class PklJavaCodegenCommand :
option(
names = arrayOf("--params-annotation"),
help =
"Fully qualified name of the annotation type to use for annotating constructor parameters with their name."
"Fully qualified name of the annotation type to use for annotating constructor parameters with their name.",
)
.defaultLazy(
"`none` if `--generate-spring-boot` is set, `org.pkl.config.java.mapper.Named` otherwise"
@@ -100,13 +100,13 @@ class PklJavaCodegenCommand :
The specified annotation type must be annotated with `@java.lang.annotation.Target(ElementType.TYPE_USE)`
or the generated code may not compile.
"""
.trimIndent()
.trimIndent(),
)
private val implementSerializable: Boolean by
option(
names = arrayOf("--implement-serializable"),
help = "Whether to generate classes that implement java.io.Serializable."
help = "Whether to generate classes that implement java.io.Serializable.",
)
.flag()
@@ -121,7 +121,7 @@ class PklJavaCodegenCommand :
With this option, you can override or modify the default names, renaming entire
classes or just their packages.
"""
.trimIndent()
.trimIndent(),
)
.associate()
@@ -137,7 +137,7 @@ class PklJavaCodegenCommand :
paramsAnnotation = if (paramsAnnotation == "none") null else paramsAnnotation,
nonNullAnnotation = nonNullAnnotation,
implementSerializable = implementSerializable,
renames = renames
renames = renames,
)
CliJavaCodeGenerator(options).run()
}