Repository: apple/pkl
File: pkl-cli/src/main/kotlin/org/pkl/cli/commands/FormatterCommand.kt (Kotlin)

Code before:
import org.pkl.commons.cli.commands.BaseCommand

class FormatterCommand : NoOpCliktCommand(name = "format") {
  override fun help(context: Context) = "Run commands related to formatting"

  override fun helpEpilog(context: Context) = "For more information, visit $helpLink"

  init {
    subcommands(FormatterCheckCommand(), FormatterApplyCommand())
  }
}

class FormatterCheckCommand : BaseCommand(name = "check", helpLink = helpLink) {
  override val helpString: String =
    "Check if the given files are properly formatted, printing the file name to stdout in case they are not. Returns non-zero in case of failure."

  val paths: List<Path> by
    argument(name = "paths", help = "Files or directory to check.")
      .path(mustExist = true, canBeDir = true)
      .multiple()

  override fun run() {
    CliFormatterCheck(baseOptions.baseOptions(emptyList()), paths).run()
  }
}

class FormatterApplyCommand : BaseCommand(name = "apply", helpLink = helpLink) {
  override val helpString: String =
    "Overwrite all the files in place with the formatted version. Returns non-zero in case of failure."

  val paths: List<Path> by
    argument(name = "paths", help = "Files or directory to format.")
      .path(mustExist = true, canBeDir = true)
      .multiple()

  val silent: Boolean by
    option(
        names = arrayOf("-s", "--silent"),
        help = "Do not write the name of the files that failed formatting to stdout.",
      )
      .flag()

  override fun run() {
    CliFormatterApply(baseOptions.baseOptions(emptyList()), paths, silent).run()
  }
}


Review comment (suggestion):
We shouldn't conflict with the built-in `--version` flag that already exists.

Names are hard, but, maybe let's call this "grammar version"?

```suggestion
    option(names = arrayOf("--grammar-version"), help = compatVersionHelp)
```

I also don't think we need to offer a short version of this flag; descriptive flags help with readability of scripts.

Code after:
import org.pkl.cli.CliFormatterApply
import org.pkl.cli.CliFormatterCheck
import org.pkl.cli.commands.FormatterCheckCommand.Companion.grammarVersionHelp
import org.pkl.commons.cli.commands.BaseCommand
import org.pkl.formatter.GrammarVersion

class FormatterCommand : NoOpCliktCommand(name = "format") {
  override fun help(context: Context) = "Run commands related to formatting"

  override fun helpEpilog(context: Context) = "For more information, visit $helpLink"

  init {
    subcommands(FormatterCheckCommand(), FormatterApplyCommand())
  }
}

class FormatterCheckCommand : BaseCommand(name = "check", helpLink = helpLink) {
  override val helpString: String =
    "Check if the given files are properly formatted, printing the file name to stdout in case they are not. Returns non-zero in case of failure."

  val paths: List<Path> by
    argument(name = "paths", help = "Files or directory to check.")
      .path(mustExist = true, canBeDir = true)
      .multiple()

  val grammarVersion: GrammarVersion by
    option(names = arrayOf("--grammar-version"), help = grammarVersionHelp)
      .enum<GrammarVersion> { "${it.version}" }
      .default(GrammarVersion.latest())

  override fun run() {
    CliFormatterCheck(baseOptions.baseOptions(emptyList()), paths, grammarVersion).run()
  }

  companion object {
    internal val grammarVersionHelp =
      """
      The grammar compatibility version to use.$NEWLINE
      ${GrammarVersion.entries.joinToString("$NEWLINE", prefix = "  ") {
        val default = if (it == GrammarVersion.latest()) " `(default)`" else ""
        "`${it.version}`: ${it.versionSpan}$default"
      }}
      """
        .trimIndent()
  }
}

class FormatterApplyCommand : BaseCommand(name = "apply", helpLink = helpLink) {
  override val helpString: String =
    "Overwrite all the files in place with the formatted version. Returns non-zero in case of failure."