[CLI] create ArgumentParseErrors instances on request
spare memory for projects with big number of modules currently 500kb for IJ monorepo of mostly empty errors
This commit is contained in:
+11
-11
@@ -65,7 +65,7 @@ data class ArgumentParseErrors(
|
|||||||
|
|
||||||
// Parses arguments into the passed [result] object. Errors related to the parsing will be collected into [CommonToolArguments.errors].
|
// Parses arguments into the passed [result] object. Errors related to the parsing will be collected into [CommonToolArguments.errors].
|
||||||
fun <A : CommonToolArguments> parseCommandLineArguments(args: List<String>, result: A, overrideArguments: Boolean = false) {
|
fun <A : CommonToolArguments> parseCommandLineArguments(args: List<String>, result: A, overrideArguments: Boolean = false) {
|
||||||
val errors = result.errors ?: ArgumentParseErrors().also { result.errors = it }
|
val errors = lazy { result.errors ?: ArgumentParseErrors().also { result.errors = it } }
|
||||||
val preprocessed = preprocessCommandLineArguments(args, errors)
|
val preprocessed = preprocessCommandLineArguments(args, errors)
|
||||||
parsePreprocessedCommandLineArguments(preprocessed, result, errors, overrideArguments)
|
parsePreprocessedCommandLineArguments(preprocessed, result, errors, overrideArguments)
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ fun <A : CommonToolArguments> parseCommandLineArgumentsFromEnvironment(arguments
|
|||||||
private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(
|
private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(
|
||||||
args: List<String>,
|
args: List<String>,
|
||||||
result: A,
|
result: A,
|
||||||
errors: ArgumentParseErrors,
|
errors: Lazy<ArgumentParseErrors>,
|
||||||
overrideArguments: Boolean
|
overrideArguments: Boolean
|
||||||
) {
|
) {
|
||||||
data class ArgumentField(val property: KMutableProperty1<A, Any?>, val argument: Argument)
|
data class ArgumentField(val property: KMutableProperty1<A, Any?>, val argument: Argument)
|
||||||
@@ -103,13 +103,13 @@ private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(
|
|||||||
|
|
||||||
val deprecatedName = argument.deprecatedName
|
val deprecatedName = argument.deprecatedName
|
||||||
if (deprecatedName.isNotEmpty() && (deprecatedName == arg || arg.startsWith("$deprecatedName="))) {
|
if (deprecatedName.isNotEmpty() && (deprecatedName == arg || arg.startsWith("$deprecatedName="))) {
|
||||||
errors.deprecatedArguments[deprecatedName] = argument.value
|
errors.value.deprecatedArguments[deprecatedName] = argument.value
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argument.value == arg) {
|
if (argument.value == arg) {
|
||||||
if (argument.isAdvanced && property.returnType.classifier != Boolean::class) {
|
if (argument.isAdvanced && property.returnType.classifier != Boolean::class) {
|
||||||
errors.extraArgumentsPassedInObsoleteForm.add(arg)
|
errors.value.extraArgumentsPassedInObsoleteForm.add(arg)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -140,9 +140,9 @@ private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(
|
|||||||
val parser = matchingParsers.firstOrNull()
|
val parser = matchingParsers.firstOrNull()
|
||||||
|
|
||||||
if (parser == null) {
|
if (parser == null) {
|
||||||
errors.unknownExtraFlags += arg
|
errors.value.unknownExtraFlags += arg
|
||||||
} else {
|
} else {
|
||||||
val newInternalArgument = parser.parseInternalArgument(arg, errors) ?: continue
|
val newInternalArgument = parser.parseInternalArgument(arg, errors.value) ?: continue
|
||||||
// Manual language feature setting overrides the previous value of the same feature setting, if it exists.
|
// Manual language feature setting overrides the previous value of the same feature setting, if it exists.
|
||||||
internalArguments.removeIf {
|
internalArguments.removeIf {
|
||||||
(it as? ManualLanguageFeatureSetting)?.languageFeature ==
|
(it as? ManualLanguageFeatureSetting)?.languageFeature ==
|
||||||
@@ -157,8 +157,8 @@ private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(
|
|||||||
val argumentField = properties.firstOrNull { it.matches(arg) }
|
val argumentField = properties.firstOrNull { it.matches(arg) }
|
||||||
if (argumentField == null) {
|
if (argumentField == null) {
|
||||||
when {
|
when {
|
||||||
arg.startsWith(ADVANCED_ARGUMENT_PREFIX) -> errors.unknownExtraFlags.add(arg)
|
arg.startsWith(ADVANCED_ARGUMENT_PREFIX) -> errors.value.unknownExtraFlags.add(arg)
|
||||||
arg.startsWith("-") -> errors.unknownArgs.add(arg)
|
arg.startsWith("-") -> errors.value.unknownArgs.add(arg)
|
||||||
else -> freeArgs.add(arg)
|
else -> freeArgs.add(arg)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@@ -172,7 +172,7 @@ private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(
|
|||||||
when (arg.substring(argument.value.length + 1)) {
|
when (arg.substring(argument.value.length + 1)) {
|
||||||
"true" -> true
|
"true" -> true
|
||||||
"false" -> false
|
"false" -> false
|
||||||
else -> true.also { errors.booleanArgumentWithValue = arg }
|
else -> true.also { errors.value.booleanArgumentWithValue = arg }
|
||||||
}
|
}
|
||||||
} else true
|
} else true
|
||||||
}
|
}
|
||||||
@@ -183,7 +183,7 @@ private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(
|
|||||||
arg.substring(argument.deprecatedName.length + 1)
|
arg.substring(argument.deprecatedName.length + 1)
|
||||||
}
|
}
|
||||||
i == args.size -> {
|
i == args.size -> {
|
||||||
errors.argumentWithoutValue = arg
|
errors.value.argumentWithoutValue = arg
|
||||||
break@loop
|
break@loop
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
@@ -194,7 +194,7 @@ private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(
|
|||||||
if ((argumentField.property.returnType.classifier as? KClass<*>)?.java?.isArray == false
|
if ((argumentField.property.returnType.classifier as? KClass<*>)?.java?.isArray == false
|
||||||
&& !visitedArgs.add(argument.value) && value is String && property.get(result) != value
|
&& !visitedArgs.add(argument.value) && value is String && property.get(result) != value
|
||||||
) {
|
) {
|
||||||
errors.duplicateArguments[argument.value] = value
|
errors.value.duplicateArguments[argument.value] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
updateField(property, result, value, argument.delimiter, overrideArguments)
|
updateField(property, result, value, argument.delimiter, overrideArguments)
|
||||||
|
|||||||
+4
-4
@@ -23,14 +23,14 @@ private const val BACKSLASH = '\\'
|
|||||||
* This is done prior to *any* arguments parsing, and result of preprocessing
|
* This is done prior to *any* arguments parsing, and result of preprocessing
|
||||||
* will be used instead of actual passed arguments.
|
* will be used instead of actual passed arguments.
|
||||||
*/
|
*/
|
||||||
fun preprocessCommandLineArguments(args: List<String>, errors: ArgumentParseErrors): List<String> =
|
fun preprocessCommandLineArguments(args: List<String>, errors: Lazy<ArgumentParseErrors>): List<String> =
|
||||||
args.flatMap { arg ->
|
args.flatMap { arg ->
|
||||||
if (arg.isArgfileArgument) {
|
if (arg.isArgfileArgument) {
|
||||||
File(arg.argfilePath).expand(errors)
|
File(arg.argfilePath).expand(errors.value)
|
||||||
} else if (arg.isDeprecatedArgfileArgument) {
|
} else if (arg.isDeprecatedArgfileArgument) {
|
||||||
errors.deprecatedArguments[EXPERIMENTAL_ARGFILE_ARGUMENT] = ARGFILE_ARGUMENT
|
errors.value.deprecatedArguments[EXPERIMENTAL_ARGFILE_ARGUMENT] = ARGFILE_ARGUMENT
|
||||||
|
|
||||||
File(arg.deprecatedArgfilePath).expand(errors)
|
File(arg.deprecatedArgfilePath).expand(errors.value)
|
||||||
} else {
|
} else {
|
||||||
listOf(arg)
|
listOf(arg)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ fun main(args: Array<String>) {
|
|||||||
@TestOnly
|
@TestOnly
|
||||||
internal fun transformArgs(args: List<String>, messageCollector: MessageCollector, isTest: Boolean): List<String> {
|
internal fun transformArgs(args: List<String>, messageCollector: MessageCollector, isTest: Boolean): List<String> {
|
||||||
val parseErrors = ArgumentParseErrors()
|
val parseErrors = ArgumentParseErrors()
|
||||||
val kotlincTransformed = preprocessCommandLineArguments(args, parseErrors)
|
val kotlincTransformed = preprocessCommandLineArguments(args, lazy { parseErrors })
|
||||||
|
|
||||||
val errorMessage = validateArguments(parseErrors)
|
val errorMessage = validateArguments(parseErrors)
|
||||||
if (errorMessage != null) {
|
if (errorMessage != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user