preprocessArgs
的作用分析 private fun preprocessArgs(args: Array<String>): List<String> {
if (args.isEmpty()) {
return Collections.emptyList()
}
// a buggy DE may fail to strip an unused parameter from a .desktop file
if (args.size == 1 && args[0] == "%f") {
return Collections.emptyList()
}
@Suppress("SuspiciousPackagePrivateAccess")
if (AppMode.HELP_OPTION in args) {
println("""
Some of the common commands and options (sorry, the full list is not yet supported):
--help prints a short list of commands and options
--version shows version information
/project/dir
opens a project from the given directory
[/project/dir|--temp-project] [--wait] [--line <line>] [--column <column>] file
opens the file, either in a context of the given project or as a temporary single-file project,
optionally waiting until the editor tab is closed
diff <left> <right>
opens a diff window between <left> and <right> files/directories
merge <local> <remote> [base] <merged>
opens a merge window between <local> and <remote> files (with optional common <base>), saving the result to <merged>
""".trimIndent())
exitProcess(0)
}
@Suppress("SuspiciousPackagePrivateAccess")
if (AppMode.VERSION_OPTION in args) {
val appInfo = ApplicationInfoImpl.getShadowInstance()
val edition = ApplicationNamesInfo.getInstance().editionName?.let { " (${it})" } ?: ""
println("${appInfo.fullApplicationName}${edition}\nBuild #${appInfo.build.asString()}")
exitProcess(0)
}
val (propertyArgs, otherArgs) = args.partition { it.startsWith("-D") && it.contains('=') }
propertyArgs.forEach { arg ->
val (option, value) = arg.removePrefix("-D").split('=', limit = 2)
System.setProperty(option, value)
}
return otherArgs
}
args
为空时,直接返回空列表,避免后续无效操作。.desktop
文件参数"%f"
(图形界面启动应用的占位符),自动忽略该无效参数,避免干扰命令行逻辑--help
命令响应:
检测到AppMode.HELP_OPTION
(如--help
)时,打印结构化帮助信息并退出进程。内容涵盖常见命令(如打开项目/文件、差异对比、合并文件)及选项说明(如--wait
、--line
),提供用户友好的引导。--version
命令响应:
检测到AppMode.VERSION_OPTION
(如--version
)时,输出应用名称、版本号、构建编号等版本信息后退出。-D
开头且包含等号=
的参数(如-Didea.debug=true
),这些参数用于动态配置JVM系统属性。split('=', limit=2)
解析键值对(如"idea.debug"
→"true"
),并调用System.setProperty()
注入系统属性,影响运行时行为(如调试模式、日志级别)/project/dir
、选项--wait
)以List<String>
形式返回,供后续流程(如项目打开、文件加载)使用。mainImpl
中的AppMode.setFlags()
)的输入,驱动应用主流程。此函数是命令行应用启动流程的关键预处理层,通过参数分类、无效过滤、命令响应、属性注入四步操作,确保后续逻辑获得纯净、有效的输入,同时提升用户体验与系统可配置性。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。