@@ -17,7 +17,7 @@ import SwiftFormatCore
1717import SwiftFormatPrettyPrint
1818import Utility
1919
20- enum Mode : String , Codable , ArgumentKind {
20+ fileprivate enum Mode : String , Codable , ArgumentKind {
2121 case format
2222 case lint
2323 case dumpConfiguration = " dump-configuration "
@@ -31,6 +31,14 @@ enum Mode: String, Codable, ArgumentKind {
3131 ] )
3232 }
3333
34+ /// Indicates whether the mode requires at least one input file passed as a positional argument.
35+ var requiresFiles : Bool {
36+ switch self {
37+ case . format, . lint: return true
38+ case . dumpConfiguration, . version: return false
39+ }
40+ }
41+
3442 init ( argument: String ) throws {
3543 guard let mode = Mode ( rawValue: argument) else {
3644 throw ArgumentParserError . invalidValue ( argument: argument, error: . unknown( value: argument) )
@@ -39,16 +47,17 @@ enum Mode: String, Codable, ArgumentKind {
3947 }
4048}
4149
42- struct CommandLineOptions : Codable {
50+ fileprivate struct CommandLineOptions {
4351 var configurationPath : String ? = nil
4452 var paths : [ String ] = [ ]
4553 var verboseLevel = 0
4654 var mode : Mode = . format
47- var prettyPrint : Bool = false
48- var printTokenStream : Bool = false
55+ var debugOptions : DebugOptions = [ ]
4956}
5057
51- func processArguments( commandName: String , _ arguments: [ String ] ) -> CommandLineOptions {
58+ fileprivate func processArguments(
59+ commandName: String , _ arguments: [ String ]
60+ ) -> CommandLineOptions {
5261 let parser = ArgumentParser ( commandName: commandName,
5362 usage: " [options] <filename or path> ... " ,
5463 overview: " Format or lint Swift source code. " )
@@ -84,34 +93,38 @@ func processArguments(commandName: String, _ arguments: [String]) -> CommandLine
8493 }
8594 binder. bind (
8695 option: parser. add (
87- option: " --pretty-print " ,
88- shortName: " -p " ,
89- kind: Bool . self,
90- usage: " Pretty-print the output and automatically apply line-wrapping. "
96+ option: " --configuration " ,
97+ kind: String . self,
98+ usage: " The path to a JSON file containing the configuration of the linter/formatter. "
9199 ) ) {
92- $0. prettyPrint = $1
100+ $0. configurationPath = $1
93101 }
102+
103+ // Add advanced debug/developer options. These intentionally have no usage strings, which omits
104+ // them from the `--help` screen to avoid noise for the general user.
94105 binder. bind (
95106 option: parser. add (
96- option: " --token-stream " ,
97- kind: Bool . self,
98- usage: " Print out the pretty-printer token stream. "
107+ option: " --debug-disable-pretty-print " ,
108+ kind: Bool . self
99109 ) ) {
100- $0. printTokenStream = $1
110+ $0. debugOptions . set ( . disablePrettyPrint , enabled : $1)
101111 }
102112 binder. bind (
103113 option: parser. add (
104- option: " --configuration " ,
105- kind: String . self,
106- usage: " The path to a JSON file containing the configuration of the linter/formatter. "
114+ option: " --debug-dump-token-stream " ,
115+ kind: Bool . self
107116 ) ) {
108- $0. configurationPath = $1
117+ $0. debugOptions . set ( . dumpTokenStream , enabled : $1)
109118 }
110119
111120 var opts = CommandLineOptions ( )
112121 do {
113122 let args = try parser. parse ( arguments)
114123 binder. fill ( args, into: & opts)
124+
125+ if opts. mode. requiresFiles && opts. paths. isEmpty {
126+ throw ArgumentParserError . expectedArguments ( parser, [ " filenames or paths " ] )
127+ }
115128 } catch {
116129 stderrStream. write ( " error: \( error) \n \n " )
117130 parser. printUsage ( on: stderrStream)
@@ -131,9 +144,7 @@ func main(_ arguments: [String]) -> Int32 {
131144 ret |= formatMain (
132145 configuration: configuration,
133146 path: path,
134- prettyPrint: options. prettyPrint,
135- printTokenStream: options. printTokenStream
136- )
147+ debugOptions: options. debugOptions)
137148 }
138149 return Int32 ( ret)
139150 case . lint:
0 commit comments