示例#1
0
文件: prolix.go 项目: gaal/prolix-go
// A go-options compatible parser.
func myParse(s *options.OptionSpec, option string, value *string) {
	if value == nil {
		switch s.GetCanonical(option) {
		case "help":
			s.PrintUsageAndExit("")
		case "pipe":
			pipe = true
		case "verbose":
			verbose = true
		case "version":
			{
				fmt.Printf("prolix %s\n", versionString)
				os.Exit(0)
			}
		default:
			s.PrintUsageAndExit("Unknown option: " + option)
		}
	} else {
		switch s.GetCanonical(option) {
		case "log":
			log = *value
		case "ignore-re":
			ignoreRe = append(ignoreRe, *value)
		case "ignore-line":
			ignoreLine = append(ignoreLine, *value)
		case "ignore-substring":
			ignoreSubstring = append(ignoreSubstring, *value)
		case "snippet":
			snippet = append(snippet, *value)
		default:
			s.PrintUsageAndExit("Unknown option: " + option)
		}
	}
}
示例#2
0
// Example custom option callback
func MyParseCallback(spec *options.OptionSpec, option string, argument *string) {
	if argument != nil {
		switch spec.GetCanonical(option) {
		case "input-encoding":
			in = *argument
		case "output-encoding":
			out = *argument
		case "repeat":
			fmt.Sscanf(*argument, "%d", &r)
		case "cookie-chance":
			fmt.Sscanf(*argument, "%f", &c)
			cInt = int64(c * 1000)
		default:
			spec.PrintUsageAndExit("Unknown option: " + option)
		}
	} else {
		switch spec.GetCanonical(option) {
		case "number":
			n = true
		case "escape":
			e = true
		case "verbose":
			v++
		default:
			if option == "help" {
				spec.PrintUsageAndExit("") // No error
			} else {
				spec.PrintUsageAndExit("Unknown option: " + option)
			}
		}
	}
}