|
| 1 | +//===--- CommandLineArguments.swift - Command Line Argument Manipulation --===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +import TSCBasic |
| 13 | + |
| 14 | +/// Utilities for manipulating a list of command line arguments, including |
| 15 | +/// constructing one from a set of ParsedOptions. |
| 16 | +extension Array where Element == Job.ArgTemplate { |
| 17 | + /// Append a fixed flag to the command line arguments. |
| 18 | + /// |
| 19 | + /// When possible, use the more semantic forms `appendFlag` or |
| 20 | + /// `append(_: Option)`. |
| 21 | + mutating func appendFlag<StringType: StringProtocol>(_ string: StringType) { |
| 22 | + append(.flag(String(string))) |
| 23 | + } |
| 24 | + |
| 25 | + /// Append multiple flags to the command line arguments. |
| 26 | + /// |
| 27 | + /// When possible, use the more semantic forms `appendFlag` or |
| 28 | + /// `append(_: Option)`. |
| 29 | + mutating func appendFlags(_ flags: String...) { |
| 30 | + appendFlags(flags) |
| 31 | + } |
| 32 | + |
| 33 | + /// Append multiple flags to the command line arguments. |
| 34 | + /// |
| 35 | + /// When possible, use the more semantic forms `appendFlag` or |
| 36 | + /// `append(_: Option)`. |
| 37 | + mutating func appendFlags(_ flags: [String]) { |
| 38 | + for flag in flags { |
| 39 | + append(.flag(flag)) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Append a virtual path to the command line arguments. |
| 44 | + mutating func appendPath(_ path: VirtualPath) { |
| 45 | + append(.path(path)) |
| 46 | + } |
| 47 | + |
| 48 | + /// Append an absolute path to the command line arguments. |
| 49 | + mutating func appendPath(_ path: AbsolutePath) { |
| 50 | + append(.path(.absolute(path))) |
| 51 | + } |
| 52 | + |
| 53 | + /// Append an option's spelling to the command line arguments. |
| 54 | + mutating func appendFlag(_ option: Option) { |
| 55 | + switch option.kind { |
| 56 | + case .flag, .joinedOrSeparate, .remaining, .separate: |
| 57 | + break |
| 58 | + case .commaJoined, .input, .joined: |
| 59 | + fatalError("Option cannot be appended as a flag: \(option)") |
| 60 | + } |
| 61 | + |
| 62 | + append(.flag(option.spelling)) |
| 63 | + } |
| 64 | + |
| 65 | + /// Append a single argument from the given option. |
| 66 | + private mutating func appendSingleArgument(option: Option, argument: String) throws { |
| 67 | + if option.attributes.contains(.argumentIsPath) { |
| 68 | + append(.path(try VirtualPath(path: argument))) |
| 69 | + } else { |
| 70 | + appendFlag(argument) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Append a parsed option to the array of argument templates, expanding |
| 75 | + /// until multiple arguments if required. |
| 76 | + mutating func append(_ parsedOption: ParsedOption) throws { |
| 77 | + let option = parsedOption.option |
| 78 | + let argument = parsedOption.argument |
| 79 | + |
| 80 | + switch option.kind { |
| 81 | + case .input: |
| 82 | + try appendSingleArgument(option: option, argument: argument.asSingle) |
| 83 | + |
| 84 | + case .flag: |
| 85 | + appendFlag(option) |
| 86 | + |
| 87 | + case .separate, .joinedOrSeparate: |
| 88 | + appendFlag(option.spelling) |
| 89 | + try appendSingleArgument(option: option, argument: argument.asSingle) |
| 90 | + |
| 91 | + case .commaJoined: |
| 92 | + assert(!option.attributes.contains(.argumentIsPath)) |
| 93 | + appendFlag(option.spelling + argument.asMultiple.joined(separator: ",")) |
| 94 | + |
| 95 | + case .remaining: |
| 96 | + appendFlag(option.spelling) |
| 97 | + for arg in argument.asMultiple { |
| 98 | + try appendSingleArgument(option: option, argument: arg) |
| 99 | + } |
| 100 | + |
| 101 | + case .joined: |
| 102 | + if option.attributes.contains(.argumentIsPath) { |
| 103 | + fatalError("Not currently implementable") |
| 104 | + } else { |
| 105 | + appendFlag(option.spelling + argument.asSingle) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /// Append the last parsed option that matches one of the given options |
| 111 | + /// to this command line. |
| 112 | + mutating func appendLast(_ options: Option..., from parsedOptions: inout ParsedOptions) throws { |
| 113 | + guard let parsedOption = parsedOptions.last(for: options) else { |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + try append(parsedOption) |
| 118 | + } |
| 119 | + |
| 120 | + /// Append the last parsed option from the given group to this command line. |
| 121 | + mutating func appendLast(in group: Option.Group, from parsedOptions: inout ParsedOptions) throws { |
| 122 | + guard let parsedOption = parsedOptions.getLast(in: group) else { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + try append(parsedOption) |
| 127 | + } |
| 128 | + |
| 129 | + mutating func append(contentsOf options: [ParsedOption]) throws { |
| 130 | + for option in options { |
| 131 | + try append(option) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// Append all parsed options that match one of the given options |
| 136 | + /// to this command line. |
| 137 | + mutating func appendAll(_ options: Option..., from parsedOptions: inout ParsedOptions) throws { |
| 138 | + for matching in parsedOptions.arguments(for: options) { |
| 139 | + try append(matching) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /// Append just the arguments from all parsed options that match one of the given options |
| 144 | + /// to this command line. |
| 145 | + mutating func appendAllArguments(_ options: Option..., from parsedOptions: inout ParsedOptions) throws { |
| 146 | + for matching in parsedOptions.arguments(for: options) { |
| 147 | + try self.appendSingleArgument(option: matching.option, argument: matching.argument.asSingle) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /// Append the last of the given flags that appears in the parsed options, |
| 152 | + /// or the flag that corresponds to the default value if neither |
| 153 | + /// appears. |
| 154 | + mutating func appendFlag(true trueFlag: Option, false falseFlag: Option, default defaultValue: Bool, from parsedOptions: inout ParsedOptions) { |
| 155 | + let isTrue = parsedOptions.hasFlag( |
| 156 | + positive: trueFlag, |
| 157 | + negative: falseFlag, |
| 158 | + default: defaultValue |
| 159 | + ) |
| 160 | + appendFlag(isTrue ? trueFlag : falseFlag) |
| 161 | + } |
| 162 | + |
| 163 | + /// A shell-escaped string representation of the arguments, as they would appear on the command line. |
| 164 | + var joinedArguments: String { |
| 165 | + return self.map { |
| 166 | + switch $0 { |
| 167 | + case .flag(let string): |
| 168 | + return string.spm_shellEscaped() |
| 169 | + case .path(let path): |
| 170 | + return path.name.spm_shellEscaped() |
| 171 | + } |
| 172 | + }.joined(separator: " ") |
| 173 | + } |
| 174 | +} |
0 commit comments