@@ -60,30 +60,30 @@ data class ParsedArgs(
6060 |formatting succeeded or failed on standard error. If none of the style options are
6161 |passed, Meta's style is used.
6262 |
63- |Alternatively, ktfmt can read Kotlin source code from standard input and write the
63+ |Alternatively, ktfmt can read Kotlin source code from standard input and write the
6464 |formatted result on standard output.
6565 |
6666 |Example:
6767 | $ ktfmt --kotlinlang-style Main.kt src/Parser.kt
6868 | Done formatting Main.kt
6969 | Error formatting src/Parser.kt: @@@ERROR@@@; skipping.
70- |
70+ |
7171 |Commands options:
7272 | -h, --help Show this help message
73- | -n, --dry-run Don't write to files, only report files which
73+ | -n, --dry-run Don't write to files, only report files which
7474 | would have changed
7575 | --meta-style Use 2-space block indenting (default)
7676 | --google-style Google internal style (2 spaces)
7777 | --kotlinlang-style Kotlin language guidelines style (4 spaces)
7878 | --stdin-name=<name> Name to report when formatting code from stdin
79- | --set-exit-if-changed Sets exit code to 1 if any input file was not
79+ | --set-exit-if-changed Sets exit code to 1 if any input file was not
8080 | formatted/touched
8181 | --do-not-remove-unused-imports Leaves all imports in place, even if not used
82- |
82+ |
8383 |ARGFILE:
8484 | If the only argument begins with '@', the remainder of the argument is treated
8585 | as the name of a file to read options and arguments from, one per line.
86- |
86+ |
8787 | e.g.
8888 | $ cat arg-file.txt
8989 | --google-style
@@ -109,9 +109,21 @@ data class ParsedArgs(
109109
110110 for (arg in args) {
111111 when {
112- arg == " --meta-style" -> formattingOptions = Formatter .META_FORMAT
113- arg == " --google-style" -> formattingOptions = Formatter .GOOGLE_FORMAT
114- arg == " --kotlinlang-style" -> formattingOptions = Formatter .KOTLINLANG_FORMAT
112+ arg.startsWith(" --style=" ) -> {
113+ val parsedStyle =
114+ parseKeyValueArg(" --style" , arg)
115+ ? : return ParseResult .Error (
116+ unexpectedArg(arg)
117+ )
118+ formattingOptions = when (parsedStyle) {
119+ " meta" -> Formatter .META_FORMAT
120+ " google" -> Formatter .GOOGLE_FORMAT
121+ " kotlinlang" -> Formatter .KOTLINLANG_FORMAT
122+ else -> return ParseResult .Error (
123+ " Unknown style '${parsedStyle} '. Style must be one of [dropbox, google, kotlinlang]."
124+ )
125+ }
126+ }
115127 arg == " --dry-run" || arg == " -n" -> dryRun = true
116128 arg == " --set-exit-if-changed" -> setExitIfChanged = true
117129 arg == " --do-not-remove-unused-imports" -> removeUnusedImports = false
@@ -120,8 +132,8 @@ data class ParsedArgs(
120132 parseKeyValueArg(" --stdin-name" , arg)
121133 ? : return ParseResult .Error (
122134 " Found option '${arg} ', expected '${" --stdin-name" } =<value>'" )
123- arg.startsWith(" --" ) -> return ParseResult .Error (" Unexpected option: $ arg" )
124- arg.startsWith(" @" ) -> return ParseResult .Error (" Unexpected option: $ arg" )
135+ arg.startsWith(" --" ) -> return ParseResult .Error (unexpectedArg( arg) )
136+ arg.startsWith(" @" ) -> return ParseResult .Error (unexpectedArg( arg) )
125137 else -> fileNames.add(arg)
126138 }
127139 }
@@ -148,6 +160,8 @@ data class ParsedArgs(
148160 ))
149161 }
150162
163+ private fun unexpectedArg (arg : String ) = " Unexpected option: $arg "
164+
151165 private fun parseKeyValueArg (key : String , arg : String ): String? {
152166 val parts = arg.split(' =' , limit = 2 )
153167 return parts[1 ].takeIf { parts[0 ] == key || parts.size == 2 }
0 commit comments