Make CommonToolArguments.nullable

#KT-28974 Fixed
This commit is contained in:
Alexander Udalov
2019-01-02 17:49:40 +01:00
parent 9559346199
commit 05c25342ad
3 changed files with 14 additions and 11 deletions
@@ -27,7 +27,7 @@ abstract class CommonToolArguments : Freezable(), Serializable {
var freeArgs: List<String> by FreezableVar(emptyList()) var freeArgs: List<String> by FreezableVar(emptyList())
@Transient @Transient
var errors: ArgumentParseErrors = ArgumentParseErrors() var errors: ArgumentParseErrors? = null
@Argument(value = "-help", shortName = "-h", description = "Print a synopsis of standard options") @Argument(value = "-help", shortName = "-h", description = "Print a synopsis of standard options")
var help: Boolean by FreezableVar(false) var help: Boolean by FreezableVar(false)
@@ -38,12 +38,12 @@ private const val ADVANCED_ARGUMENT_PREFIX = "-X"
private const val FREE_ARGS_DELIMITER = "--" private const val FREE_ARGS_DELIMITER = "--"
data class ArgumentParseErrors( data class ArgumentParseErrors(
val unknownArgs: MutableList<String> = SmartList<String>(), val unknownArgs: MutableList<String> = SmartList(),
val unknownExtraFlags: MutableList<String> = SmartList<String>(), val unknownExtraFlags: MutableList<String> = SmartList(),
// Names of extra (-X...) arguments which have been passed in an obsolete form ("-Xaaa bbb", instead of "-Xaaa=bbb") // Names of extra (-X...) arguments which have been passed in an obsolete form ("-Xaaa bbb", instead of "-Xaaa=bbb")
val extraArgumentsPassedInObsoleteForm: MutableList<String> = SmartList<String>(), val extraArgumentsPassedInObsoleteForm: MutableList<String> = SmartList(),
// Non-boolean arguments which have been passed multiple times, possibly with different values. // Non-boolean arguments which have been passed multiple times, possibly with different values.
// The key in the map is the name of the argument, the value is the last passed value. // The key in the map is the name of the argument, the value is the last passed value.
@@ -62,11 +62,12 @@ 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) { fun <A : CommonToolArguments> parseCommandLineArguments(args: List<String>, result: A) {
val preprocessed = preprocessCommandLineArguments(args, result.errors) val errors = result.errors ?: ArgumentParseErrors().also { result.errors = it }
parsePreprocessedCommandLineArguments(preprocessed, result) val preprocessed = preprocessCommandLineArguments(args, errors)
parsePreprocessedCommandLineArguments(preprocessed, result, errors)
} }
private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(args: List<String>, result: A) { private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(args: List<String>, result: A, errors: ArgumentParseErrors) {
data class ArgumentField(val property: KMutableProperty1<A, Any?>, val argument: Argument) data class ArgumentField(val property: KMutableProperty1<A, Any?>, val argument: Argument)
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@@ -76,7 +77,6 @@ private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(args
ArgumentField(property as KMutableProperty1<A, Any?>, argument) ArgumentField(property as KMutableProperty1<A, Any?>, argument)
} }
val errors = result.errors
val visitedArgs = mutableSetOf<String>() val visitedArgs = mutableSetOf<String>()
var freeArgsStarted = false var freeArgsStarted = false
@@ -198,7 +198,8 @@ private fun <A : CommonToolArguments> updateField(property: KMutableProperty1<A,
/** /**
* @return error message if arguments are parsed incorrectly, null otherwise * @return error message if arguments are parsed incorrectly, null otherwise
*/ */
fun validateArguments(errors: ArgumentParseErrors): String? { fun validateArguments(errors: ArgumentParseErrors?): String? {
if (errors == null) return null
if (errors.argumentWithoutValue != null) { if (errors.argumentWithoutValue != null) {
return "No value passed for argument ${errors.argumentWithoutValue}" return "No value passed for argument ${errors.argumentWithoutValue}"
} }
@@ -118,7 +118,10 @@ abstract class CLITool<A : CommonToolArguments> {
} }
private fun reportArgumentParseProblems(collector: MessageCollector, arguments: A) { private fun reportArgumentParseProblems(collector: MessageCollector, arguments: A) {
val errors = arguments.errors reportUnsafeInternalArgumentsIfAny(arguments, collector)
val errors = arguments.errors ?: return
for (flag in errors.unknownExtraFlags) { for (flag in errors.unknownExtraFlags) {
collector.report(STRONG_WARNING, "Flag is not supported by this version of the compiler: $flag") collector.report(STRONG_WARNING, "Flag is not supported by this version of the compiler: $flag")
} }
@@ -139,7 +142,6 @@ abstract class CLITool<A : CommonToolArguments> {
collector.report(STRONG_WARNING, argfileError) collector.report(STRONG_WARNING, argfileError)
} }
reportUnsafeInternalArgumentsIfAny(arguments, collector)
for (internalArgumentsError in errors.internalArgumentsParsingProblems) { for (internalArgumentsError in errors.internalArgumentsParsingProblems) {
collector.report(STRONG_WARNING, internalArgumentsError) collector.report(STRONG_WARNING, internalArgumentsError)
} }