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

Code before:
        help = "External reader registrations for module URI schemes",
      )
      .parseExternalReader("=")
      .multiple()
      .toMap()

  val externalResourceReaders: Map<String, ExternalReader> by
    option(
        names = arrayOf("--external-resource-reader"),
        metavar = "<scheme>='<executable> [<arguments>]'",
        help = "External reader registrations for resource URI schemes",
      )
      .parseExternalReader("=")
      .multiple()
      .toMap()

  val traceMode: TraceMode by
    option(
        names = arrayOf("--trace-mode"),
        metavar = "when",
        help =
          "Specifies how calls to trace() are formatted. Possible values of <when> are 'default', 'pretty' and 'hidden'.",
      )
      .enum<TraceMode> { it.name.lowercase() }
      .single()
      .default(TraceMode.DEFAULT)

  // hidden option used by native tests
  private val testPort: Int by
    option(names = arrayOf("--test-port"), help = "Internal test option", hidden = true)
      .single()
      .int()
      .default(-1)

  fun baseOptions(
    modules: List<URI>,
    projectOptions: ProjectOptions? = null,
    testMode: Boolean = false,
  ): CliBaseOptions {
    return CliBaseOptions(
      sourceModules = modules,
      allowedModules = allowedModules.ifEmpty { null },
      allowedResources = allowedResources.ifEmpty { null },
      environmentVariables = envVars.ifEmpty { null },
      externalProperties = properties.ifEmpty { null },
      modulePath = modulePath.ifEmpty { null },
      workingDir = workingDir,
      settings = settings,
      rootDir = rootDir,
      projectDir = projectOptions?.projectDir,
      timeout = timeout,

Review comment (bug):
This should be nullable; we want this to fall back to the setting in `pkl:Project` if available.

Take a look at `org.pkl.commons.cli.CliCommand` and see how many of those options are set, and how the evaluator is built based on this.

This should also fix the broken tests on main currently.

Code after:
        help = "External reader registrations for module URI schemes",
      )
      .parseExternalReader("=")
      .multiple()
      .toMap()

  val externalResourceReaders: Map<String, ExternalReader> by
    option(
        names = arrayOf("--external-resource-reader"),
        metavar = "<scheme>='<executable> [<arguments>]'",
        help = "External reader registrations for resource URI schemes",
      )
      .parseExternalReader("=")
      .multiple()
      .toMap()

  val traceMode: TraceMode? by
    option(
        names = arrayOf("--trace-mode"),
        metavar = "style",
        help =
          "Specifies how calls to trace() are formatted. Possible values of <style> are 'compact' and 'pretty'.",
      )
      .enum<TraceMode> { it.name.lowercase() }
      .single()

  // hidden option used by native tests
  private val testPort: Int by
    option(names = arrayOf("--test-port"), help = "Internal test option", hidden = true)
      .single()
      .int()
      .default(-1)

  fun baseOptions(
    modules: List<URI>,
    projectOptions: ProjectOptions? = null,
    testMode: Boolean = false,
  ): CliBaseOptions {
    return CliBaseOptions(
      sourceModules = modules,
      allowedModules = allowedModules.ifEmpty { null },
      allowedResources = allowedResources.ifEmpty { null },
      environmentVariables = envVars.ifEmpty { null },
      externalProperties = properties.ifEmpty { null },
      modulePath = modulePath.ifEmpty { null },
      workingDir = workingDir,
      settings = settings,
      rootDir = rootDir,
      projectDir = projectOptions?.projectDir,
      timeout = timeout,
      moduleCacheDir = cacheDir ?: defaults.normalizedModuleCacheDir,