Skip to content

Commit 93568fa

Browse files
author
Joseph Cooper
committed
CLI: Use --style=STYLE option to select formatter
1 parent cf8eac3 commit 93568fa

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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 }

core/src/test/java/com/facebook/ktfmt/cli/MainTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class MainTest {
225225
}
226226

227227
@Test
228-
fun `kotlinlang-style is passed to formatter (file)`() {
228+
fun `--style=kotlinlang is passed to formatter (file)`() {
229229
val code =
230230
"""fun f() {
231231
for (child in
@@ -241,14 +241,14 @@ class MainTest {
241241
emptyInput,
242242
PrintStream(out),
243243
PrintStream(err),
244-
arrayOf("--kotlinlang-style", fooBar.toString()))
244+
arrayOf("--style=kotlinlang", fooBar.toString()))
245245
.run()
246246

247247
assertThat(fooBar.readText()).isEqualTo(code)
248248
}
249249

250250
@Test
251-
fun `kotlinlang-style is passed to formatter (stdin)`() {
251+
fun `--style=kotlinlang is passed to formatter (stdin)`() {
252252
val code =
253253
"""fun f() {
254254
|for (child in
@@ -271,7 +271,7 @@ class MainTest {
271271
code.byteInputStream(),
272272
PrintStream(out),
273273
PrintStream(err),
274-
arrayOf("--kotlinlang-style", "-"))
274+
arrayOf("--style=kotlinlang", "-"))
275275
.run()
276276

277277
assertThat(out.toString(UTF_8)).isEqualTo(formatted)

core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,29 @@ class ParsedArgsTest {
6161
}
6262

6363
@Test
64-
fun `parseOptions recognizes --meta-style`() {
65-
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--meta-style", "foo.kt")))
64+
fun `parseOptions recognizes --style=meta`() {
65+
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--style=meta", "foo.kt")))
6666
assertThat(parsed.formattingOptions).isEqualTo(Formatter.META_FORMAT)
6767
}
6868

6969
@Test
70-
fun `parseOptions recognizes --dropbox-style`() {
71-
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--kotlinlang-style", "foo.kt")))
70+
fun `parseOptions recognizes --style=kotlinlang`() {
71+
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--style=kotlinlang", "foo.kt")))
7272
assertThat(parsed.formattingOptions).isEqualTo(Formatter.KOTLINLANG_FORMAT)
7373
}
7474

7575
@Test
76-
fun `parseOptions recognizes --google-style`() {
77-
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--google-style", "foo.kt")))
76+
fun `parseOptions recognizes --style=google`() {
77+
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--style=google", "foo.kt")))
7878
assertThat(parsed.formattingOptions).isEqualTo(Formatter.GOOGLE_FORMAT)
7979
}
8080

81+
@Test
82+
fun `parseOptions rejects unknown style`() {
83+
val parseResult = ParsedArgs.parseOptions(arrayOf("--style=custom-style", "foo.kt"))
84+
assertThat(parseResult).isInstanceOf(ParseResult.Error::class.java)
85+
}
86+
8187
@Test
8288
fun `parseOptions recognizes --dry-run`() {
8389
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--dry-run", "foo.kt")))
@@ -170,7 +176,7 @@ class ParsedArgsTest {
170176
@Test
171177
fun `processArgs use the @file option with file containing arguments`() {
172178
val file = root.resolve("existing-file")
173-
file.writeText("--google-style\n--dry-run\n--set-exit-if-changed\nFile1.kt\nFile2.kt\n")
179+
file.writeText("--style=google\n--dry-run\n--set-exit-if-changed\nFile1.kt\nFile2.kt\n")
174180

175181
val result = ParsedArgs.processArgs(arrayOf("@" + file.absolutePath))
176182
assertThat(result).isInstanceOf(ParseResult.Ok::class.java)
@@ -187,7 +193,7 @@ class ParsedArgsTest {
187193
fun `parses multiple args successfully`() {
188194
val testResult =
189195
ParsedArgs.parseOptions(
190-
arrayOf("--google-style", "--dry-run", "--set-exit-if-changed", "File.kt"),
196+
arrayOf("--style=google", "--dry-run", "--set-exit-if-changed", "File.kt"),
191197
)
192198
assertThat(testResult)
193199
.isEqualTo(
@@ -202,7 +208,7 @@ class ParsedArgsTest {
202208
@Test
203209
fun `last style in args wins`() {
204210
val testResult =
205-
ParsedArgs.parseOptions(arrayOf("--google-style", "--kotlinlang-style", "File.kt"))
211+
ParsedArgs.parseOptions(arrayOf("--style=google", "--style=kotlinlang", "File.kt"))
206212
assertThat(testResult)
207213
.isEqualTo(
208214
parseResultOk(
@@ -213,7 +219,7 @@ class ParsedArgsTest {
213219

214220
@Test
215221
fun `error when parsing multiple args and one is unknown`() {
216-
val testResult = ParsedArgs.parseOptions(arrayOf("@unknown", "--google-style", "File.kt"))
222+
val testResult = ParsedArgs.parseOptions(arrayOf("@unknown", "--style=google", "File.kt"))
217223
assertThat(testResult).isEqualTo(ParseResult.Error("Unexpected option: @unknown"))
218224
}
219225

0 commit comments

Comments
 (0)