Skip to content

Commit 56bb298

Browse files
authored
Merge pull request #72258 from Azoy/commandline-fixes
[stdlib] Deprecate CommandLine.arguments setter
2 parents 82743ba + 9d31822 commit 56bb298

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

stdlib/public/core/CommandLine.swift

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,52 @@ public enum CommandLine {
5454
return _unsafeArgv
5555
}
5656

57-
/// Access to the Swift command line arguments.
58-
// Use lazy initialization of static properties to
59-
// safely initialize the swift arguments.
60-
public static var arguments: [String]
61-
= (0..<Int(argc)).map { String(cString: _unsafeArgv[$0]!) }
57+
// This is extremely unsafe and allows for concurrent writes with no
58+
// synchronization to the underlying data. In a future version of Swift you
59+
// will not be able to write to 'CommandLine.arguments'.
60+
static nonisolated(unsafe) var _arguments: [String] = (0 ..< Int(argc)).map {
61+
String(cString: _unsafeArgv[$0]!)
62+
}
63+
64+
/// An array that provides access to this program's command line arguments.
65+
///
66+
/// Use `CommandLine.arguments` to access the command line arguments used
67+
/// when executing the current program. The name of the executed program is
68+
/// the first argument.
69+
///
70+
/// The following example shows a command line executable that squares the
71+
/// integer given as an argument.
72+
///
73+
/// if CommandLine.arguments.count == 2,
74+
/// let number = Int(CommandLine.arguments[1]) {
75+
/// print("\(number) x \(number) is \(number * number)")
76+
/// } else {
77+
/// print(
78+
/// """
79+
/// Error: Please provide a number to square.
80+
/// Usage: command <number>
81+
/// """
82+
/// )
83+
/// }
84+
///
85+
/// Running the program results in the following output:
86+
///
87+
/// $ command 5
88+
/// 5 x 5 is 25
89+
/// $ command ZZZ
90+
/// Error: Please provide a number to square.
91+
/// Usage: command <number>
92+
public static var arguments: [String] {
93+
get {
94+
_arguments
95+
}
96+
97+
@available(*, deprecated, message: "Do not modify CommandLine.arguments. It will become read-only in a future version of Swift.")
98+
@available(swift, obsoleted: 6.0)
99+
set {
100+
_arguments = newValue
101+
}
102+
}
62103
}
63104

64105
@available(*, unavailable)

0 commit comments

Comments
 (0)