Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. enum OptionCode: Int32 {
  2. case h = 0x68
  3. case u = 0x75
  4. case p = 0x70
  5. case firstLongOption = 0x100
  6. case host
  7. }
  8.  
  9. extension StaticString {
  10. var ccharPointer: UnsafePointer<CChar> {
  11. let rawPointer = UnsafeRawPointer(utf8Start)
  12. return rawPointer.bindMemory(to: CChar.self, capacity: utf8CodeUnitCount)
  13. }
  14. }
  15.  
  16. let longOpts: [option] = [
  17. option(name: ("help" as StaticString).ccharPointer, has_arg: no_argument, flag: nil, val: OptionCode.h.rawValue),
  18. option(name: ("username" as StaticString).ccharPointer, has_arg: required_argument, flag: nil, val: OptionCode.u.rawValue),
  19. option(name: ("password" as StaticString).ccharPointer, has_arg: required_argument, flag: nil, val: OptionCode.p.rawValue),
  20. option(name: ("host" as StaticString).ccharPointer, has_arg: required_argument, flag: nil, val: OptionCode.host.rawValue),
  21. option()
  22. ]
  23.  
  24. while case let opt = getopt_long(CommandLine.argc, CommandLine.unsafeArgv, "hu:p:", longOpts, nil), opt != -1 {
  25. switch opt {
  26. case OptionCode.u.rawValue:
  27. userName = String(cString: optarg)
  28. case OptionCode.p.rawValue:
  29. password = String(cString: optarg)
  30. case OptionCode.host.rawValue:
  31. host = URL(string: String(cString: optarg))!
  32. case OptionCode.h.rawValue:
  33. print("""
  34. Options available:
  35. --username, -u: user name
  36. --password, -p: password
  37. --host: host
  38. --help, -h: show this help
  39. """)
  40. exit(0)
  41. default:
  42. fatalError()
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement