Esempio n. 1
0
// Parse does the argument parsing
func (prog *Program) Parse() *Program {
	if len(os.Args) == 1 {
		prog.ShowHelp()
		return prog
	}

	var commandName = os.Args[1]
	var cmd command
	var found = false

	for _, v := range prog.commands {
		if v.short == commandName || v.long == commandName {
			cmd = v
			found = true
			break
		}
	}

	if found == false {
		fmt.Println("INVALID OPTION: " + commandName)
		fmt.Println("Try \"help\" for a list of available commands")
		return prog
	}

	args := minimist.ParseArgv(os.Args[2:])
	cmd.handler(Args{args})

	return prog
}
Esempio n. 2
0
File: runner.go Progetto: THEY/godo
func godo(tasksFunc func(*Project), argv []string) {
	if argv == nil {
		argm = minimist.Parse()
	} else {
		argm = minimist.ParseArgv(argv)
	}

	help = argm.ZeroBool("help", "h", "?")
	verbose = argm.ZeroBool("verbose", "v")
	version = argm.ZeroBool("version", "V")
	watching = argm.ZeroBool("watch", "w")
	deprecatedWarnings = argm.ZeroBool("D")
	contextArgm = minimist.ParseArgv(argm.Unparsed())

	project := NewProject(tasksFunc)

	if help {
		Usage(project.usage())
		os.Exit(0)
	}

	if version {
		fmt.Printf("godo %s\n", Version)
		os.Exit(0)
	}

	// Run each task including their dependencies.
	args := []string{}
	for _, v := range argm.Leftover() {
		args = append(args, fmt.Sprintf("%v", v))
	}

	if len(args) == 0 {
		if project.Tasks["default"] != nil {
			args = append(args, "default")
		} else {
			Usage(project.usage())
			os.Exit(0)
		}
	}

	// quick fix to make cascading watch work on default task
	if len(args) == 1 && args[0] == "default" {
		args = project.Tasks["default"].Dependencies
	}

	for _, name := range args {
		err := project.Run(name)
		if err != nil {
			util.Error("ERR", "%s\n", err.Error())
			os.Exit(1)
		}
	}

	if watching {
		if project.Watch(args, true) {
			waitgroup.Add(1)
			waitExit = true
		} else {
			fmt.Println("Nothing to watch. Use W{} or Watch{} to specify glob patterns")
			os.Exit(0)
		}
	}

	if waitExit {
		waitgroup.Wait()
	}
}
Esempio n. 3
0
// used for testing to switch out exitFn
func godoExit(tasksFunc func(*Project), argv []string, exitFn func(int)) {
	if argv == nil {
		argm = minimist.Parse()
	} else {
		argm = minimist.ParseArgv(argv)
	}

	dump := argm.AsBool("dump")
	help = argm.AsBool("help", "h", "?")
	verbose = argm.AsBool("verbose", "v")
	version = argm.AsBool("version", "V")
	watching = argm.AsBool("watch", "w")
	deprecatedWarnings = argm.AsBool("D")
	contextArgm := minimist.ParseArgv(argm.Unparsed())

	project := NewProject(tasksFunc, exitFn, contextArgm)

	if help {
		Usage(project.usage())
		exitFn(0)
	}

	if version {
		fmt.Printf("godo %s\n", Version)
		exitFn(0)
	}

	if dump {
		project.dump(os.Stdout, "", "  ")
		exitFn(0)
	}

	// env vars are any nonflag key=value pair
	addToOSEnviron(argm.NonFlags())

	// Run each task including their dependencies.
	args := []string{}
	for _, s := range argm.NonFlags() {
		// skip env vars
		if !strings.Contains(s, "=") {
			args = append(args, s)
		}
	}

	if len(args) == 0 {
		if project.Tasks["default"] != nil {
			args = append(args, "default")
		} else {
			Usage(project.usage())
			exitFn(0)
		}
	}

	for _, name := range args {
		err := project.Run(name)
		if err != nil {
			util.Error("ERR", "%s\n", err.Error())
			exitFn(1)
		}
	}

	if watching {
		if project.Watch(args, true) {
			runnerWaitGroup.Add(1)
			waitExit = true
		} else {
			fmt.Println("Nothing to watch. Use Task#Src() to specify watch patterns")
			exitFn(0)
		}
	}

	if waitExit {
		// Ctrl+C handler
		csig := make(chan os.Signal, 1)
		signal.Notify(csig, syscall.SIGQUIT)
		go func() {
			for sig := range csig {
				fmt.Println("SIG caught")
				if sig == syscall.SIGQUIT {
					fmt.Println("SIG caught B")
					project.Exit(0)
					break
				}
			}
		}()

		runnerWaitGroup.Wait()
	}
	exitFn(0)
}