FlagValue
public protocol FlagValue
Protocol that defines the needs to be implemented for a type to be used as a flag value
Example
Flag with User type
struct User: FlagValue {
let name: String
static func fromString(flagValue value: String) throws -> User {
return User(name: value)
}
static var typeDescription: String {
return "A user structure"
}
}
We can then use user to define flag type:
let flag = try! Flag(longName: "debug", type: User.self)
-
Create a command string convertible from string
Throws
exception thrown if cannot convert from string. The exception of type FlagValueError
Example
Create a correct user from string
struct User: FlagValue { let name: String static func fromString(flagValue value: String) throws -> User { return User(name: value) } static var typeDescription: String { return "A user structure" } }
Return a parsing error
struct User: FlagValue { let name: String static func fromString(flagValue value: String) throws -> User { if value == "wrong" { throw FlagValueError.conversionError("wrong user passed") } return User(name: value) } static var typeDescription: String { return "A user structure" } }
Declaration
Swift
static func fromString(flagValue value: String) throws -> Self
Parameters
value
the string to be converted
-
Return a decription to be printed when priting the command help
Example
struct User: FlagValue { let name: String static func fromString(flagValue value: String) throws -> User { return User(name: value) } static var typeDescription: String { return "A user structure" } }
When printing the help, the
typeDescription
will be used.Flags: -u, --user A user structure Use "command [command] --help" for more information about a command.
Declaration
Swift
static var typeDescription: String { get }