Improved command line handling

This commit is contained in:
Alex Yatskov 2015-04-03 12:58:41 +09:00
parent 7ebecef0ee
commit eac74e82cb

View File

@ -69,11 +69,10 @@ func parse(filename string) (*config, error) {
return conf, nil return conf, nil
} }
func fatalUsage() { func usage() {
_, executable := path.Split(os.Args[0]) fmt.Printf("Usage: %s [options] conf_file [src_dir]\n\n", os.Args[0])
fmt.Printf("Usage: %s [options] conf_file [src_dir]\n\n", executable) fmt.Print("Parameters:\n")
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(1)
} }
func absPath(path string) string { func absPath(path string) string {
@ -92,12 +91,13 @@ func main() {
} }
taskName := flag.String("task", "default", "name of task to execute") taskName := flag.String("task", "default", "name of task to execute")
action := flag.String("action", "install", "'install' or 'uninstall' symlinks") action := flag.String("action", "install", "install or uninstall symlinks")
dstDir := flag.String("dest", currUsr.HomeDir, "target directory for symlinks") dstDir := flag.String("dest", currUsr.HomeDir, "target directory for symlinks")
force := flag.Bool("force", true, "create parent directories to target") force := flag.Bool("force", true, "create parent directories to target")
clobber := flag.Bool("clobber", false, "delete files and directories at target") clobber := flag.Bool("clobber", false, "delete files and directories at target")
verbose := flag.Bool("verbose", false, "verbose output") verbose := flag.Bool("verbose", false, "verbose output")
flag.Usage = usage
flag.Parse() flag.Parse()
flags := 0 flags := 0
@ -112,7 +112,8 @@ func main() {
} }
if flag.NArg() == 0 { if flag.NArg() == 0 {
fatalUsage() usage()
os.Exit(2)
} }
conf, err := parse(flag.Arg(0)) conf, err := parse(flag.Arg(0))
@ -127,13 +128,15 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {
fatalUsage() usage()
os.Exit(2)
} }
case "uninstall": case "uninstall":
if err := conf.uninstall(absPath(*dstDir), *taskName, flags); err != nil { if err := conf.uninstall(absPath(*dstDir), *taskName, flags); err != nil {
log.Fatal(err) log.Fatal(err)
} }
default: default:
fatalUsage() usage()
os.Exit(2)
} }
} }