@@ -48,6 +48,54 @@ data class ParsedArgs(
4848 return parseOptions(arguments)
4949 }
5050
51+ val HELP_TEXT =
52+ """
53+ |ktfmt - command line Kotlin source code pretty-printer
54+ |
55+ |Usage:
56+ | ktfmt [OPTIONS] <File1.kt> <File2.kt> ...
57+ | ktfmt @ARGFILE
58+ |
59+ |ktfmt formats Kotlin source code files in-place, reporting for each file whether the
60+ |formatting succeeded or failed on standard error. If none of the style options are
61+ |passed, Meta's style is used.
62+ |
63+ |Alternatively, ktfmt can read Kotlin source code from standard input and write the
64+ |formatted result on standard output.
65+ |
66+ |Example:
67+ | $ ktfmt --kotlinlang-style Main.kt src/Parser.kt
68+ | Done formatting Main.kt
69+ | Error formatting src/Parser.kt: @@@ERROR@@@; skipping.
70+ |
71+ |Commands options:
72+ | -h, --help Show this help message
73+ | -n, --dry-run Don't write to files, only report files which
74+ | would have changed
75+ | --meta-style Use 2-space block indenting (default)
76+ | --google-style Google internal style (2 spaces)
77+ | --kotlinlang-style Kotlin language guidelines style (4 spaces)
78+ | --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
80+ | formatted/touched
81+ | --do-not-remove-unused-imports Leaves all imports in place, even if not used
82+ |
83+ |ARGFILE:
84+ | If the only argument begins with '@', the remainder of the argument is treated
85+ | as the name of a file to read options and arguments from, one per line.
86+ |
87+ | e.g.
88+ | $ cat arg-file.txt
89+ | --google-style
90+ | -n
91+ | File1.kt
92+ | File2.kt
93+ | $ ktfmt @arg-file1.txt
94+ | Done formatting File1.kt
95+ | Done formatting File2.kt
96+ |"""
97+ .trimMargin()
98+
5199 /* * parseOptions parses command-line arguments passed to ktfmt. */
52100 fun parseOptions (args : Array <out String >): ParseResult {
53101 val fileNames = mutableListOf<String >()
@@ -57,6 +105,8 @@ data class ParsedArgs(
57105 var removeUnusedImports = true
58106 var stdinName: String? = null
59107
108+ if (" --help" in args || " -h" in args) return ParseResult .ShowMessage (HELP_TEXT )
109+
60110 for (arg in args) {
61111 when {
62112 arg == " --meta-style" -> formattingOptions = Formatter .META_FORMAT
@@ -108,5 +158,7 @@ data class ParsedArgs(
108158sealed interface ParseResult {
109159 data class Ok (val parsedValue : ParsedArgs ) : ParseResult
110160
161+ data class ShowMessage (val message : String ) : ParseResult
162+
111163 data class Error (val errorMessage : String ) : ParseResult
112164}
0 commit comments