Skip to content

Commit

Permalink
feat(cli): support max width arg
Browse files Browse the repository at this point in the history
  • Loading branch information
awe123343 committed Jun 5, 2024
1 parent 26a24ae commit 38796db
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/com/facebook/ktfmt/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Main(
}
if (parsedArgs.fileNames.isEmpty()) {
err.println(
"Usage: ktfmt [--dropbox-style | --google-style | --kotlinlang-style] [--dry-run] [--set-exit-if-changed] [--stdin-name=<name>] [--do-not-remove-unused-imports] File1.kt File2.kt ...")
"Usage: ktfmt [--dropbox-style | --google-style | --kotlinlang-style] [--dry-run] [--set-exit-if-changed] [--max-width=<number>] [--stdin-name=<name>] [--do-not-remove-unused-imports] File1.kt File2.kt ...")
err.println("Or: ktfmt @file")
return 1
}
Expand Down
24 changes: 17 additions & 7 deletions core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ data class ParsedArgs(
var setExitIfChanged = false
var removeUnusedImports = true
var stdinName: String? = null
var maxWidth = FormattingOptions.DEFAULT_MAX_WIDTH

for (arg in args) {
when {
Expand All @@ -63,6 +64,11 @@ data class ParsedArgs(
arg == "--dry-run" || arg == "-n" -> dryRun = true
arg == "--set-exit-if-changed" -> setExitIfChanged = true
arg == "--do-not-remove-unused-imports" -> removeUnusedImports = false
arg.startsWith("--max-width=") ->
maxWidth = parseKeyValueArg("--max-width", arg)?.let {
try { it.toInt() } catch (e: NumberFormatException) { null }
}
?: return ParseResult.Error("Found option '${arg}', expected '${"--max-width"}=<number>'")
arg.startsWith("--stdin-name=") ->
stdinName =
parseKeyValueArg("--stdin-name", arg)
Expand All @@ -75,13 +81,17 @@ data class ParsedArgs(
}

return ParseResult.Ok(
ParsedArgs(
fileNames,
formattingOptions.copy(removeUnusedImports = removeUnusedImports),
dryRun,
setExitIfChanged,
stdinName,
))
ParsedArgs(
fileNames,
formattingOptions.copy(
removeUnusedImports = removeUnusedImports,
maxWidth = maxWidth,
),
dryRun,
setExitIfChanged,
stdinName,
)
)
}

private fun parseKeyValueArg(key: String, arg: String): String? {
Expand Down
18 changes: 18 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ class ParsedArgsTest {
assertThat(parsed.formattingOptions.removeUnusedImports).isFalse()
}

@Test
fun `parseOptions recognizes --max-width`() {
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--max-width=120")))
assertThat(parsed.formattingOptions.maxWidth).isEqualTo(120)
}

@Test
fun `parseOptions accepts --max-width with empty value`() {
val parseResult = ParsedArgs.parseOptions(arrayOf("--max-width="))
assertThat(parseResult).isInstanceOf(ParseResult.Error::class.java)
}

@Test
fun `parseOptions --max-width without value`() {
val parseResult = ParsedArgs.parseOptions(arrayOf("--max-width"))
assertThat(parseResult).isInstanceOf(ParseResult.Error::class.java)
}

@Test
fun `parseOptions recognizes --stdin-name`() {
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--stdin-name=my/foo.kt")))
Expand Down

0 comments on commit 38796db

Please sign in to comment.