diff --git a/Sources/swift-format/Subcommands/Format.swift b/Sources/swift-format/Subcommands/Format.swift index 42c2da16..935bcd63 100644 --- a/Sources/swift-format/Subcommands/Format.swift +++ b/Sources/swift-format/Subcommands/Format.swift @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// import ArgumentParser +import Foundation extension SwiftFormatCommand { /// Formats one or more files containing Swift code. @@ -36,9 +37,27 @@ extension SwiftFormatCommand { var performanceMeasurementOptions: PerformanceMeasurementsOptions func validate() throws { + #if os(Windows) if inPlace && formatOptions.paths.isEmpty { throw ValidationError("'--in-place' is only valid when formatting files") } + #else + let stdinIsPiped: Bool = { + let standardInput = FileHandle.standardInput + return isatty(standardInput.fileDescriptor) == 0 + }() + if !stdinIsPiped, formatOptions.paths.isEmpty { + throw ValidationError( + """ + No input files specified. Please provide input in one of the following ways: + - Provide the path to a directory along with the '--recursive' option to format all Swift files within it. + - Provide the path to a specific Swift source code file. + - Or, pipe Swift code into the command (e.g., echo "let a = 1" | swift-format). + Additionally, if you want to overwrite files in-place, use '--in-place'. + """ + ) + } + #endif } func run() throws { diff --git a/Sources/swift-format/Subcommands/Lint.swift b/Sources/swift-format/Subcommands/Lint.swift index 3002c591..59ff8e1c 100644 --- a/Sources/swift-format/Subcommands/Lint.swift +++ b/Sources/swift-format/Subcommands/Lint.swift @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// import ArgumentParser +import Foundation extension SwiftFormatCommand { /// Emits style diagnostics for one or more files containing Swift code. @@ -32,6 +33,25 @@ extension SwiftFormatCommand { @OptionGroup(visibility: .hidden) var performanceMeasurementOptions: PerformanceMeasurementsOptions + func validate() throws { + #if !os(Windows) + let stdinIsPiped: Bool = { + let standardInput = FileHandle.standardInput + return isatty(standardInput.fileDescriptor) == 0 + }() + if !stdinIsPiped, lintOptions.paths.isEmpty { + throw ValidationError( + """ + No input files specified. Use one of the following: + - Provide the path to a directory along with the '--recursive' option to lint all Swift files within it. + - Provide the path to a specific Swift source code file. + - Or, pipe Swift code into the command (e.g., echo "let a = 1" | swift-format lint). + """ + ) + } + #endif + } + func run() throws { try performanceMeasurementOptions.printingInstructionCountIfRequested { let frontend = LintFrontend(lintFormatOptions: lintOptions)