Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum OptionCode: Int32 {
- case h = 0x68
- case u = 0x75
- case p = 0x70
- case firstLongOption = 0x100
- case host
- }
- extension StaticString {
- var ccharPointer: UnsafePointer<CChar> {
- let rawPointer = UnsafeRawPointer(utf8Start)
- return rawPointer.bindMemory(to: CChar.self, capacity: utf8CodeUnitCount)
- }
- }
- let longOpts: [option] = [
- option(name: ("help" as StaticString).ccharPointer, has_arg: no_argument, flag: nil, val: OptionCode.h.rawValue),
- option(name: ("username" as StaticString).ccharPointer, has_arg: required_argument, flag: nil, val: OptionCode.u.rawValue),
- option(name: ("password" as StaticString).ccharPointer, has_arg: required_argument, flag: nil, val: OptionCode.p.rawValue),
- option(name: ("host" as StaticString).ccharPointer, has_arg: required_argument, flag: nil, val: OptionCode.host.rawValue),
- option()
- ]
- while case let opt = getopt_long(CommandLine.argc, CommandLine.unsafeArgv, "hu:p:", longOpts, nil), opt != -1 {
- switch opt {
- case OptionCode.u.rawValue:
- userName = String(cString: optarg)
- case OptionCode.p.rawValue:
- password = String(cString: optarg)
- case OptionCode.host.rawValue:
- host = URL(string: String(cString: optarg))!
- case OptionCode.h.rawValue:
- print("""
- Options available:
- --username, -u: user name
- --password, -p: password
- --host: host
- --help, -h: show this help
- """)
- exit(0)
- default:
- fatalError()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement