File tree 3 files changed +18
-3
lines changed
clikt/src/main/kotlin/com/github/ajalt/clikt/core 3 files changed +18
-3
lines changed Original file line number Diff line number Diff line change 1
1
# Changelog
2
2
3
3
## [ Unreleased]
4
+ ### Changed
5
+ - Shell completion code is now printed by throwing a ` PrintCompletionMessage ` (a subclass of ` PrintMessage ` ) rather than calling ` echo ` directly.
4
6
5
7
## [ 2.2.0] - 2019-09-25
6
8
### Added
Original file line number Diff line number Diff line change @@ -93,8 +93,7 @@ abstract class CliktCommand(
93
93
}
94
94
val envval = System .getenv(envvar) ? : return
95
95
val completion = CompletionGenerator .generateCompletion(command = this , zsh = " zsh" in envval)
96
- echo(completion, lineSeparator = " \n " )
97
- exitProcess(1 )
96
+ throw PrintCompletionMessage (completion, forceUnixLineEndings = true )
98
97
}
99
98
100
99
/* *
@@ -232,6 +231,10 @@ abstract class CliktCommand(
232
231
} catch (e: PrintHelpMessage ) {
233
232
echo(e.command.getFormattedHelp())
234
233
exitProcess(0 )
234
+ } catch (e: PrintCompletionMessage ) {
235
+ val s = if (e.forceUnixLineEndings) " \n " else context.console.lineSeparator
236
+ echo(e.message, lineSeparator = s)
237
+ exitProcess(0 )
235
238
} catch (e: PrintMessage ) {
236
239
echo(e.message)
237
240
exitProcess(0 )
Original file line number Diff line number Diff line change @@ -30,7 +30,17 @@ class PrintHelpMessage(val command: CliktCommand) : CliktError()
30
30
*
31
31
* Execution should be immediately halted without an error.
32
32
*/
33
- class PrintMessage (message : String ) : CliktError(message)
33
+ open class PrintMessage (message : String ) : CliktError(message)
34
+
35
+ /* *
36
+ * An exception that indicates that shell completion code should be printed.
37
+ *
38
+ * Execution should be immediately halted without an error.
39
+ *
40
+ * @param forceUnixLineEndings if true, all line endings in the message should be `\n`, regardless
41
+ * of the current operating system.
42
+ */
43
+ class PrintCompletionMessage (message : String , val forceUnixLineEndings : Boolean ): PrintMessage(message)
34
44
35
45
/* *
36
46
* An internal exception that signals a usage error.
You can’t perform that action at this time.
0 commit comments