Skip to content

Commit

Permalink
Print a usage description when the user enters the swift-format comma…
Browse files Browse the repository at this point in the history
…nd without arguments
  • Loading branch information
TTOzzi committed Jan 13, 2025
1 parent 6e64b26 commit 08f3f57
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/swift-format/Subcommands/Format.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import ArgumentParser
import Foundation

extension SwiftFormatCommand {
/// Formats one or more files containing Swift code.
Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions Sources/swift-format/Subcommands/Lint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import ArgumentParser
import Foundation

extension SwiftFormatCommand {
/// Emits style diagnostics for one or more files containing Swift code.
Expand All @@ -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)
Expand Down

0 comments on commit 08f3f57

Please sign in to comment.