17840: Check for unparsed command line arguments.
[arvados.git] / services / crunchstat / crunchstat.go
index 6bf2e3399e61b4ec172e4ddb3dd76cee37c201d4..3ff3912e8c892ab1e33f936170157eaa2886df46 100644 (file)
@@ -32,15 +32,22 @@ func main() {
                Logger: log.New(os.Stderr, "crunchstat: ", 0),
        }
 
-       flag.StringVar(&reporter.CgroupRoot, "cgroup-root", "", "Root of cgroup tree")
-       flag.StringVar(&reporter.CgroupParent, "cgroup-parent", "", "Name of container parent under cgroup")
-       flag.StringVar(&reporter.CIDFile, "cgroup-cid", "", "Path to container id file")
-       flag.IntVar(&signalOnDeadPPID, "signal-on-dead-ppid", signalOnDeadPPID, "Signal to send child if crunchstat's parent process disappears (0 to disable)")
-       flag.DurationVar(&ppidCheckInterval, "ppid-check-interval", ppidCheckInterval, "Time between checks for parent process disappearance")
-       pollMsec := flag.Int64("poll", 1000, "Reporting interval, in milliseconds")
-       getVersion := flag.Bool("version", false, "Print version information and exit.")
-
-       flag.Parse()
+       flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
+       flags.StringVar(&reporter.CgroupRoot, "cgroup-root", "", "Root of cgroup tree")
+       flags.StringVar(&reporter.CgroupParent, "cgroup-parent", "", "Name of container parent under cgroup")
+       flags.StringVar(&reporter.CIDFile, "cgroup-cid", "", "Path to container id file")
+       flags.IntVar(&signalOnDeadPPID, "signal-on-dead-ppid", signalOnDeadPPID, "Signal to send child if crunchstat's parent process disappears (0 to disable)")
+       flags.DurationVar(&ppidCheckInterval, "ppid-check-interval", ppidCheckInterval, "Time between checks for parent process disappearance")
+       pollMsec := flags.Int64("poll", 1000, "Reporting interval, in milliseconds")
+       getVersion := flags.Bool("version", false, "Print version information and exit.")
+
+       err := flags.Parse(os.Args[1:])
+       if err == flag.ErrHelp {
+               return
+       } else if err != nil {
+               reporter.Logger.Print(err)
+               os.Exit(2)
+       }
 
        // Print version information if requested
        if *getVersion {
@@ -48,6 +55,12 @@ func main() {
                return
        }
 
+       if flags.NArg() == 0 {
+               fmt.Fprintf(flags.Output(), "Usage: %s [options] program [args...]\n\nOptions:\n", os.Args[0])
+               flags.PrintDefaults()
+               os.Exit(2)
+       }
+
        reporter.Logger.Printf("crunchstat %s started", version)
 
        if reporter.CgroupRoot == "" {
@@ -58,7 +71,7 @@ func main() {
        reporter.PollPeriod = time.Duration(*pollMsec) * time.Millisecond
 
        reporter.Start()
-       err := runCommand(flag.Args(), reporter.Logger)
+       err = runCommand(flags.Args(), reporter.Logger)
        reporter.Stop()
 
        if err, ok := err.(*exec.ExitError); ok {