Skip to content

Commit

Permalink
Add --help option for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Cooper committed Jun 11, 2024
1 parent f9d4d25 commit 7ab6a88
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/src/main/java/com/facebook/ktfmt/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class Main(
val parsedArgs =
when (processArgs) {
is ParseResult.Ok -> processArgs.parsedValue
is ParseResult.ShowMessage -> {
err.println(processArgs.message)
return EXIT_CODE_SUCCESS
}
is ParseResult.Error -> {
err.println(processArgs.errorMessage)
return EXIT_CODE_FAILURE
Expand Down
49 changes: 48 additions & 1 deletion core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,50 @@ data class ParsedArgs(
return parseOptions(arguments)
}

val HELP_TEXT = """ktfmt - command line Kotlin source code pretty-printer
|
|Usage:
| ktfmt [--style=dropbox | --style=google | --style=kotlinlang ] [-n | --dry-run] [--set-exit-if-changed]
| [--do-not-remove-unused-imports] <File1.kt> <File2.kt> ...
| ktfmt @ARGFILE
|
| ktfmt formats Kotlin source code files in-place, reporting for each file whether the
| formatting succeeded or failed on standard error. If none of the style options are
|passed, Facebook's style is used.
|
|Alternatively, ktfmt can read Kotlin source code from standard in and write the
|formatted result on standard output.
|
|Example:
| $ ktfmt --dropbox-style Main.kt src/Parser.kt
| Done formatting Main.kt
| Error formatting src/Parser.kt: @@@ERROR@@@; skipping.
|
|Commands options:
| -n, --dry-run Don't write to files, only report files which
| would have changed
| --style=dropbox Use 4-space block indenting
| --style=google Google style
| --style=kotlinlang Kotlin language guidelines style
| --set-exit-if-changed Sets exit code to 1 if any input code was not
| correctly formatted
| --do-not-remove-unused-imports Leaves all imports in place, even if not used
|
|ARGFILE:
| If the only argument begins with '@', the remainder of the argument is treated
| as the name of a file to read options and arguments from, one per line.
|
| e.g.
| $ cat arg-file.txt
| --google-style
| -n
| File1.kt
| File2.kt
| $ ktfmt @arg-file1.txt
| Done formatting File1.kt
| Done formatting File2.kt
""".trimMargin()

/** parseOptions parses command-line arguments passed to ktfmt. */
fun parseOptions(args: Array<out String>): ParseResult {
val fileNames = mutableListOf<String>()
Expand All @@ -57,6 +101,9 @@ data class ParsedArgs(
var removeUnusedImports = true
var stdinName: String? = null

if (args.contains("--help"))
return ParseResult.ShowMessage(HELP_TEXT)

for (arg in args) {
when {
arg == "--dropbox-style" -> formattingOptions = Formatter.DROPBOX_FORMAT
Expand Down Expand Up @@ -107,6 +154,6 @@ data class ParsedArgs(

sealed interface ParseResult {
data class Ok(val parsedValue: ParsedArgs) : ParseResult

data class ShowMessage(val message: String): ParseResult
data class Error(val errorMessage: String) : ParseResult
}
13 changes: 13 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/cli/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,17 @@ class MainTest {
assertThat(exitCode).isEqualTo(0)
assertThat(file.readText(UTF_8)).isEqualTo("""fun f() = println("hello, world")""" + "\n")
}

@Test
fun `--help gives return code of 0`() {
val exitCode = Main(
emptyInput,
PrintStream(out),
PrintStream(err),
arrayOf("--help"),
)
.run()

assertThat(exitCode).isEqualTo(0)
}
}
12 changes: 12 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 @@ -133,6 +133,18 @@ class ParsedArgsTest {
assertThat(parseResult).isInstanceOf(ParseResult.Error::class.java)
}

@Test
fun `parseOptions recognises --help`() {
val parseResult = ParsedArgs.parseOptions(arrayOf("--help"))
assertThat(parseResult).isInstanceOf(ParseResult.ShowMessage::class.java)
}

@Test
fun `arg --help overrides all others`() {
val parseResult = ParsedArgs.parseOptions(arrayOf("--style=google", "@unknown", "--help", "file.kt"))
assertThat(parseResult).isInstanceOf(ParseResult.ShowMessage::class.java)
}

@Test
fun `processArgs use the @file option with non existing file`() {
val e =
Expand Down

0 comments on commit 7ab6a88

Please sign in to comment.