Exemplo n.º 1
0
func main() {
	result := isatty.Check(os.Stdin.Fd())
	fmt.Println(result)
	if !result {
		os.Exit(1)
	}
}
Exemplo n.º 2
0
func flagParse() {
	flag.Usage = usage
	flag.Parse(os.Args[1:])
	flag_fmt = *_flag_fmt

	if *flag_usage {
		usageUsage()
	}

	index := 0 // flag.Arg(...)

	global.input = flag_input
	if global.input == "" {
		if !isatty.Check(os.Stdin.Fd()) {
			global.input = "-"
		} else {
			global.input = flag.Arg(index)
			if global.input == "" {
				if isatty.Check(os.Stdin.Fd()) {
					usage()
					os.Exit(2)
				}
				global.input = "-"
			} else {
				// Consume argument
				index += 1
			}
		}
	}

	if global.input != "-" {
		global.inputPath = global.input
		global.bagName = filepath.Base(global.inputPath)
		global.bagIdentifier = regexp.MustCompile(`[^\w+]`).ReplaceAllString(global.bagName, "_")
	}

	global.bagPackage = *flag_package
	if global.bagPackage == "" {
		pkg, err := build.ImportDir(".", 0)
		if err == nil {
			global.bagPackage = pkg.Name
			dbgf(`No package name specified: Using "%s"`, global.bagPackage)
		} else {
			dbgf(`No package name specified: Using "main"`)
			global.bagPackage = "main"
		}
	}

	global.bagFunction = *flag_function
	if global.bagFunction == "" {
		global.bagFunction = regexp.MustCompile(`^(\d)`).ReplaceAllString(global.bagIdentifier, "_$1")
		dbgf(`No function name specified: Using "%s"`, global.bagFunction)
	}

	global.output = flag_output
	if global.output == "" {
		global.output = flag.Arg(index)
		if global.output == "" {
			global.output = "-"
		} else {
			// Consume argument
			index += 1
		}
	}
	if global.output != "-" {
		file, _ := os.Stat(global.output)
		if file != nil && file.IsDir() {
			// If we're here, bagName should ALWAYS != "", but just in case
			if global.bagName == "" {
				dbgf(`%/fatal//Cannot output file to: %s: already exists and is a directory`, global.output)
			} else {
				global.output = filepath.Join(global.output, global.bagName+".go")
				dbgf(`Using output file: %s`, global.output)
			}
		}
	}
}