File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Sources/swift-format/Subcommands Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1111//===----------------------------------------------------------------------===//
1212
1313import ArgumentParser
14+ import Foundation
1415
1516extension SwiftFormatCommand {
1617 /// Formats one or more files containing Swift code.
@@ -36,9 +37,27 @@ extension SwiftFormatCommand {
3637 var performanceMeasurementOptions : PerformanceMeasurementsOptions
3738
3839 func validate( ) throws {
40+ #if os(Windows)
3941 if inPlace && formatOptions. paths. isEmpty {
4042 throw ValidationError ( " '--in-place' is only valid when formatting files " )
4143 }
44+ #else
45+ let stdinIsPiped : Bool = {
46+ let standardInput = FileHandle . standardInput
47+ return isatty ( standardInput. fileDescriptor) == 0
48+ } ( )
49+ if !stdinIsPiped, formatOptions. paths. isEmpty {
50+ throw ValidationError (
51+ """
52+ No input files specified. Please provide input in one of the following ways:
53+ - Provide the path to a directory along with the '--recursive' option to format all Swift files within it.
54+ - Provide the path to a specific Swift source code file.
55+ - Or, pipe Swift code into the command (e.g., echo " let a = 1 " | swift-format).
56+ Additionally, if you want to overwrite files in-place, use '--in-place'.
57+ """
58+ )
59+ }
60+ #endif
4261 }
4362
4463 func run( ) throws {
Original file line number Diff line number Diff line change 1111//===----------------------------------------------------------------------===//
1212
1313import ArgumentParser
14+ import Foundation
1415
1516extension SwiftFormatCommand {
1617 /// Emits style diagnostics for one or more files containing Swift code.
@@ -32,6 +33,25 @@ extension SwiftFormatCommand {
3233 @OptionGroup ( visibility: . hidden)
3334 var performanceMeasurementOptions : PerformanceMeasurementsOptions
3435
36+ func validate( ) throws {
37+ #if !os(Windows)
38+ let stdinIsPiped : Bool = {
39+ let standardInput = FileHandle . standardInput
40+ return isatty ( standardInput. fileDescriptor) == 0
41+ } ( )
42+ if !stdinIsPiped, lintOptions. paths. isEmpty {
43+ throw ValidationError (
44+ """
45+ No input files specified. Use one of the following:
46+ - Provide the path to a directory along with the '--recursive' option to lint all Swift files within it.
47+ - Provide the path to a specific Swift source code file.
48+ - Or, pipe Swift code into the command (e.g., echo " let a = 1 " | swift-format lint).
49+ """
50+ )
51+ }
52+ #endif
53+ }
54+
3555 func run( ) throws {
3656 try performanceMeasurementOptions. printingInstructionCountIfRequested {
3757 let frontend = LintFrontend ( lintFormatOptions: lintOptions)
You can’t perform that action at this time.
0 commit comments