Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print a usage description when the user enters the swift format command without arguments #914

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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