diff --git a/core/src/main/java/com/facebook/ktfmt/cli/Main.kt b/core/src/main/java/com/facebook/ktfmt/cli/Main.kt index fba648b3..87931f17 100644 --- a/core/src/main/java/com/facebook/ktfmt/cli/Main.kt +++ b/core/src/main/java/com/facebook/ktfmt/cli/Main.kt @@ -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=] [--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=] [--stdin-name=] [--do-not-remove-unused-imports] File1.kt File2.kt ...") err.println("Or: ktfmt @file") return 1 } diff --git a/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt b/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt index 0a5f7bdc..c6e6bd65 100644 --- a/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt +++ b/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt @@ -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 { @@ -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"}='") arg.startsWith("--stdin-name=") -> stdinName = parseKeyValueArg("--stdin-name", arg) @@ -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? { diff --git a/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt b/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt index 467bb419..576d7c67 100644 --- a/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt @@ -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")))