@@ -11,35 +11,7 @@ import java.nio.file.FileSystems
11
11
import java.nio.file.Files
12
12
import java.nio.file.StandardOpenOption.*
13
13
14
- private fun convertToOutputStream (
15
- s : String ,
16
- createIfNotExist : Boolean ,
17
- truncateExisting : Boolean ,
18
- fileSystem : FileSystem ,
19
- context : Context ,
20
- fail : (String ) -> Unit
21
- ): OutputStream {
22
- return if (s == " -" ) {
23
- UnclosableOutputStream (System .out )
24
- } else {
25
- val path = convertToPath(
26
- s,
27
- mustExist = ! createIfNotExist,
28
- canBeFile = true ,
29
- canBeFolder = false ,
30
- mustBeWritable = ! createIfNotExist,
31
- mustBeReadable = false ,
32
- canBeSymlink = true ,
33
- fileSystem = fileSystem,
34
- context = context
35
- ) { fail(it) }
36
- val openType = if (truncateExisting) TRUNCATE_EXISTING else APPEND
37
- val options = arrayOf(WRITE , CREATE , openType)
38
- Files .newOutputStream(path, * options)
39
- }
40
- }
41
-
42
- // <editor-fold desc="options">
14
+ // region ========== Options ==========
43
15
44
16
/* *
45
17
* Convert the option to an [OutputStream].
@@ -55,9 +27,9 @@ private fun convertToOutputStream(
55
27
* @param truncateExisting If true, existing files will be truncated when opened. By default, the file will be appended to.
56
28
*/
57
29
fun RawOption.outputStream (
58
- createIfNotExist : Boolean = true,
59
- truncateExisting : Boolean = false,
60
- fileSystem : FileSystem = FileSystems .getDefault()
30
+ createIfNotExist : Boolean = true,
31
+ truncateExisting : Boolean = false,
32
+ fileSystem : FileSystem = FileSystems .getDefault(),
61
33
): NullableOption <OutputStream , OutputStream > {
62
34
return convert({ localization.fileMetavar() }, CompletionCandidates .Path ) { s ->
63
35
convertToOutputStream(s, createIfNotExist, truncateExisting, fileSystem, context) { fail(it) }
@@ -71,8 +43,8 @@ fun NullableOption<OutputStream, OutputStream>.defaultStdout(): OptionWithValues
71
43
return default(UnclosableOutputStream (System .out ), " -" )
72
44
}
73
45
74
- // </editor-fold>
75
- // <editor-fold desc="arguments">
46
+ // endregion
47
+ // region ========== Arguments ==========
76
48
77
49
/* *
78
50
* Convert the argument to an [OutputStream].
@@ -88,9 +60,9 @@ fun NullableOption<OutputStream, OutputStream>.defaultStdout(): OptionWithValues
88
60
* @param truncateExisting If true, existing files will be truncated when opened. By default, the file will be appended to.
89
61
*/
90
62
fun RawArgument.outputStream (
91
- createIfNotExist : Boolean = true,
92
- truncateExisting : Boolean = false,
93
- fileSystem : FileSystem = FileSystems .getDefault()
63
+ createIfNotExist : Boolean = true,
64
+ truncateExisting : Boolean = false,
65
+ fileSystem : FileSystem = FileSystems .getDefault(),
94
66
): ProcessedArgument <OutputStream , OutputStream > {
95
67
return convert(completionCandidates = CompletionCandidates .Path ) { s ->
96
68
convertToOutputStream(s, createIfNotExist, truncateExisting, fileSystem, context) { fail(it) }
@@ -104,12 +76,43 @@ fun ProcessedArgument<OutputStream, OutputStream>.defaultStdout(): ArgumentDeleg
104
76
return default(UnclosableOutputStream (System .out ))
105
77
}
106
78
107
- // </editor-fold>
79
+ // endregion
108
80
109
81
/* *
110
- * Checks whether this stream is an unclosable [System.out] proxy.
82
+ * Checks whether this stream was returned from an [outputStream] parameter, and that it is
83
+ * writing to [System.out] (because `-` was given, or no value was given and the parameter uses
84
+ * [defaultStdout]).
111
85
*/
112
- fun OutputStream.isCliktParameterDefaultStdout (): Boolean = this is UnclosableOutputStream
86
+ val OutputStream .isCliktParameterDefaultStdout: Boolean
87
+ get() = this is UnclosableOutputStream
88
+
89
+ private fun convertToOutputStream (
90
+ s : String ,
91
+ createIfNotExist : Boolean ,
92
+ truncateExisting : Boolean ,
93
+ fileSystem : FileSystem ,
94
+ context : Context ,
95
+ fail : (String ) -> Unit ,
96
+ ): OutputStream {
97
+ return if (s == " -" ) {
98
+ UnclosableOutputStream (System .out )
99
+ } else {
100
+ val path = convertToPath(
101
+ s,
102
+ mustExist = ! createIfNotExist,
103
+ canBeFile = true ,
104
+ canBeFolder = false ,
105
+ mustBeWritable = ! createIfNotExist,
106
+ mustBeReadable = false ,
107
+ canBeSymlink = true ,
108
+ fileSystem = fileSystem,
109
+ context = context
110
+ ) { fail(it) }
111
+ val openType = if (truncateExisting) TRUNCATE_EXISTING else APPEND
112
+ val options = arrayOf(WRITE , CREATE , openType)
113
+ Files .newOutputStream(path, * options)
114
+ }
115
+ }
113
116
114
117
private class UnclosableOutputStream (private var delegate : OutputStream ? ) : OutputStream() {
115
118
private val stream get() = delegate ? : throw IOException (" Stream closed" )
0 commit comments