options.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. // TODO regex on type.. user/organization can be treated as the same:
  7. // hittps://github.com/<user or org>
  8. // hittps://github.com/<user or org>/repo
  9. const usage = `usage: gogethunt [options]
  10. Options:
  11. -u --user Target user
  12. -r --repo Target repo
  13. -o --org Target organization
  14. -h --help Display this message
  15. -e --entropy Enable entropy detection
  16. -r --regex Enable regex detection
  17. `
  18. type Options struct {
  19. User string
  20. Repo string
  21. Org string
  22. }
  23. func help() {
  24. os.Stderr.WriteString(usage)
  25. os.Exit(1)
  26. }
  27. func optionsNextString(args []string, i *int) string {
  28. if len(args) > *i+1 {
  29. *i++
  30. } else {
  31. help()
  32. }
  33. return args[*i]
  34. }
  35. func parseOptions(args []string) *Options {
  36. opts := &Options{}
  37. for i := 0; i < len(args); i++ {
  38. arg := args[i]
  39. switch arg {
  40. case "-o", "--org":
  41. opts.Org = optionsNextString(args, &i)
  42. case "-r", "--repo":
  43. opts.Repo = optionsNextString(args, &i)
  44. case "-u", "--user":
  45. opts.User = optionsNextString(args, &i)
  46. case "-h", "--help":
  47. help()
  48. return nil
  49. default:
  50. fmt.Printf("Uknown option %s\n\n", arg)
  51. help()
  52. return nil
  53. }
  54. }
  55. return opts
  56. }