From b2e5bb9586a1bf9a1611a5d70f74009a4f138bd2 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 20 Jul 2026 15:40:09 +0200 Subject: [PATCH] IJPL-250718 [platform]: refactoring command line arguments handling to avoid future regressions (cherry picked from commit f0675d8f124d5ef4c868bb924aa1f2392109cd2e) IJ-CR-214513 GitOrigin-RevId: 26695ae659b3a492b2d1fe2b68fc35023fabc38d --- .../bootstrap/src/com/intellij/idea/Main.kt | 3 ++- .../com/intellij/ide/CommandLineProcessor.kt | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/platform/bootstrap/src/com/intellij/idea/Main.kt b/platform/bootstrap/src/com/intellij/idea/Main.kt index 8bd2f35c69f2..f8714e007707 100644 --- a/platform/bootstrap/src/com/intellij/idea/Main.kt +++ b/platform/bootstrap/src/com/intellij/idea/Main.kt @@ -9,6 +9,7 @@ import com.intellij.concurrency.IdeaForkJoinWorkerThreadFactory import com.intellij.diagnostic.CoroutineTracerShim import com.intellij.diagnostic.StartUpMeasurer import com.intellij.ide.BootstrapBundle +import com.intellij.ide.CommandLineProcessor import com.intellij.ide.plugins.PluginManagerCore import com.intellij.ide.plugins.PluginModuleDescriptor import com.intellij.ide.startup.StartupActionScriptManager @@ -310,7 +311,7 @@ private fun preprocessArgs(rawArgs: Array): List { printVersion() exitProcess(0) } - firstArg != null && firstArg.startsWith('-') && firstArg !in setOf("--temp-project", "--wait", "--line", "--column") -> { + firstArg != null && firstArg.startsWith('-') && !CommandLineProcessor.isSupportedOption(firstArg) -> { println("unrecognized option: ${firstArg}") exitProcess(1) } diff --git a/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.kt b/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.kt index ab7cbf415907..0b4fd10fbc3b 100644 --- a/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.kt +++ b/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.kt @@ -63,7 +63,13 @@ import java.util.concurrent.CancellationException object CommandLineProcessor { private val LOG = logger() + private const val OPTION_WAIT = "--wait" + private const val OPTION_EDIT = "--edit" + private const val OPTION_LINE = "--line" + private const val OPTION_COLUMN = "--column" + private const val OPTION_TEMP_PROJECT = "--temp-project" + private const val OPTION_PROJECT = "--project" @JvmField val OK_FUTURE: Deferred = CompletableDeferred(value = CliResult.OK) @@ -396,6 +402,12 @@ object CommandLineProcessor { return result ?: error("Parsing result shouldn't be null at this point; args are not empty") } + @ApiStatus.Internal + fun isSupportedOption(option: String): Boolean = option in setOf( + OPTION_WAIT, OPTION_LINE, "-l", OPTION_COLUMN, "-c", OPTION_TEMP_PROJECT, OPTION_EDIT, "-e", OPTION_PROJECT, "-p" + ) + + // on any change, please update `isSupportedOption` accordingly private fun parseArgs(args: List, currentDirectory: String?): Result> { val openProjectResults = mutableListOf() var line = -1 @@ -410,31 +422,31 @@ object CommandLineProcessor { i++ continue } - if (arg == "-l" || arg == "--line") { + if (arg == OPTION_LINE || arg == "-l") { i++ if (i == args.size) break line = args[i].toIntOrNull() ?: -1 i++ continue } - if (arg == "-c" || arg == "--column") { + if (arg == OPTION_COLUMN || arg == "-c") { i++ if (i == args.size) break column = args[i].toIntOrNull() ?: -1 i++ continue } - if (arg == "--temp-project") { + if (arg == OPTION_TEMP_PROJECT) { tempProject = true i++ continue } - if (arg == "-e" || arg == "--edit") { + if (arg == OPTION_EDIT || arg == "-e") { lightEditMode = true i++ continue } - if (arg == "-p" || arg == "--project") { + if (arg == OPTION_PROJECT || arg == "-p") { tempProject = false i++ continue