1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
enum Name {
/// A name (usually multi-character) prefixed with `--` (2 dashes) or equivalent.
case long(String)
/// A single character name prefixed with `-` (1 dash) or equivalent.
///
/// Usually supports mixing multiple short names with a single dash, i.e. `-ab` is equivalent to `-a -b`.
case short(Character, allowingJoined: Bool = false)
/// A name (usually multi-character) prefixed with `-` (1 dash).
case longWithSingleDash(String)
}
extension Name {
init(_ baseName: Substring) {
assert(baseName.first == "-", "Attempted to create name for unprefixed argument")
if baseName.hasPrefix("--") {
self = .long(String(baseName.dropFirst(2)))
} else if baseName.count == 2 { // single character "-x" style
self = .short(baseName.last!)
} else { // long name with single dash
self = .longWithSingleDash(String(baseName.dropFirst()))
}
}
}
// short argument names based on the synopsisString
// this will put the single - options before the -- options
extension Name: Comparable {
static func < (lhs: Name, rhs: Name) -> Bool {
return lhs.synopsisString < rhs.synopsisString
}
}
extension Name: Hashable { }
extension Name {
enum Case: Equatable {
case long
case short
case longWithSingleDash
}
var `case`: Case {
switch self {
case .short:
return .short
case .longWithSingleDash:
return .longWithSingleDash
case .long:
return .long
}
}
}
extension Name {
var synopsisString: String {
switch self {
case .long(let n):
return "--\(n)"
case .short(let n, _):
return "-\(n)"
case .longWithSingleDash(let n):
return "-\(n)"
}
}
var valueString: String {
switch self {
case .long(let n):
return n
case .short(let n, _):
return String(n)
case .longWithSingleDash(let n):
return n
}
}
var allowsJoined: Bool {
switch self {
case .short(_, let allowingJoined):
return allowingJoined
default:
return false
}
}
/// The instance to match against user input -- this always has
/// `allowingJoined` as `false`, since that's the way input is parsed.
var nameToMatch: Name {
switch self {
case .long, .longWithSingleDash: return self
case .short(let c, _): return .short(c)
}
}
}
extension BidirectionalCollection where Element == Name {
var preferredName: Name? {
first { $0.case != .short } ?? first
}
var partitioned: [Name] {
filter { $0.case == .short } + filter { $0.case != .short }
}
}
|