@@ -43,9 +43,10 @@ const DEFAULT_SPACE_IDENT_SIZE: usize = 4;
43
43
author,
44
44
version,
45
45
about,
46
- after_help = "By default, the `format` command will print a single formatted WDL \
47
- document.\n \n Use the `--overwrite` option to replace a WDL document, or a \
48
- directory containing WDL documents, with the formatted source."
46
+ after_help = "Use the `--overwrite` option to replace a WDL document or a directory \
47
+ containing WDL documents with the formatted source.\n Use the `--check` option \
48
+ to verify that a document or a directory containing WDL documents is already \
49
+ formatted and print the diff if not."
49
50
) ]
50
51
pub struct FormatArgs {
51
52
/// The path to the WDL document to format (`-` for STDIN); the path may be
@@ -70,12 +71,21 @@ pub struct FormatArgs {
70
71
#[ arg( long, value_name = "SIZE" ) ]
71
72
pub indentation_size : Option < usize > ,
72
73
74
+ /// Argument group defining the mode of behavior
75
+ #[ command( flatten) ]
76
+ mode : ModeGroup ,
77
+ }
78
+
79
+ /// Argument group defining the mode of behavior
80
+ #[ derive( Parser , Debug ) ]
81
+ #[ group( required = true , multiple = false ) ]
82
+ pub struct ModeGroup {
73
83
/// Overwrite the WDL documents with the formatted versions
74
84
#[ arg( long, conflicts_with = "check" ) ]
75
85
pub overwrite : bool ,
76
86
77
87
/// Check if files are formatted correctly and print diff if not
78
- #[ arg( long, conflicts_with = "overwrite" ) ]
88
+ #[ arg( long) ]
79
89
pub check : bool ,
80
90
}
81
91
@@ -98,20 +108,23 @@ fn read_source(path: &Path) -> Result<String> {
98
108
99
109
/// Formats a document.
100
110
///
111
+ /// If `check_only` is true, checks if the document is formatted correctly and
112
+ /// prints the diff if not then exits. Else will format and overwrite the
113
+ /// document.
114
+ ///
101
115
/// If the document failed to parse, this emits the diagnostics and returns
102
116
/// `Ok(count)` of the diagnostics to the caller.
103
117
///
104
118
/// A return value of `Ok(0)` indicates the document was formatted.
105
119
fn format_document (
106
120
config : Config ,
107
121
path : & Path ,
108
- overwrite : bool ,
109
122
report_mode : Mode ,
110
123
no_color : bool ,
111
- check : bool ,
124
+ check_only : bool ,
112
125
) -> Result < usize > {
113
126
if path. to_str ( ) != Some ( "-" ) {
114
- let action = if check { "checking" } else { "formatting" } ;
127
+ let action = if check_only { "checking" } else { "formatting" } ;
115
128
println ! (
116
129
"{action_colored} `{path}`" ,
117
130
action_colored = if no_color {
@@ -147,7 +160,7 @@ fn format_document(
147
160
let formatter = Formatter :: new ( config) ;
148
161
let formatted = formatter. format ( & document) ?;
149
162
150
- if check {
163
+ if check_only {
151
164
if formatted != source {
152
165
print ! ( "{}" , StrComparison :: new( & source, & formatted) ) ;
153
166
return Ok ( 1 ) ;
@@ -156,12 +169,9 @@ fn format_document(
156
169
return Ok ( 0 ) ;
157
170
}
158
171
159
- if overwrite {
160
- fs:: write ( path, formatted)
161
- . with_context ( || format ! ( "failed to write `{path}`" , path = path. display( ) ) ) ?;
162
- } else {
163
- print ! ( "{formatted}" ) ;
164
- }
172
+ // write file because check is not true
173
+ fs:: write ( path, formatted)
174
+ . with_context ( || format ! ( "failed to write `{path}`" , path = path. display( ) ) ) ?;
165
175
166
176
Ok ( 0 )
167
177
}
@@ -188,10 +198,6 @@ pub fn format(args: FormatArgs) -> Result<()> {
188
198
189
199
let mut diagnostics = 0 ;
190
200
if args. path . to_str ( ) != Some ( "-" ) && args. path . is_dir ( ) {
191
- if !args. overwrite && !args. check {
192
- bail ! ( "formatting a directory requires the `--overwrite` or `--check` option" ) ;
193
- }
194
-
195
201
for entry in WalkDir :: new ( & args. path ) {
196
202
let entry = entry. with_context ( || {
197
203
format ! (
@@ -207,20 +213,18 @@ pub fn format(args: FormatArgs) -> Result<()> {
207
213
diagnostics += format_document (
208
214
config,
209
215
path,
210
- args. overwrite ,
211
216
args. report_mode ,
212
217
args. no_color ,
213
- args. check ,
218
+ args. mode . check ,
214
219
) ?;
215
220
}
216
221
} else {
217
222
diagnostics += format_document (
218
223
config,
219
224
& args. path ,
220
- args. overwrite ,
221
225
args. report_mode ,
222
226
args. no_color ,
223
- args. check ,
227
+ args. mode . check ,
224
228
) ?;
225
229
}
226
230
0 commit comments