2014-11-05 11:57:51 -05:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParseFlags is a utility function that adds a help flag if withHelp is true,
|
2014-11-24 11:34:13 -05:00
|
|
|
// calls cmd.Parse(args) and prints a relevant error message if there are
|
|
|
|
// incorrect number of arguments. It returns error only if error handling is
|
|
|
|
// set to ContinueOnError and parsing fails. If error handling is set to
|
|
|
|
// ExitOnError, it's safe to ignore the return value.
|
2014-11-05 11:57:51 -05:00
|
|
|
// TODO: move this to a better package than utils
|
|
|
|
func ParseFlags(cmd *flag.FlagSet, args []string, withHelp bool) error {
|
|
|
|
var help *bool
|
|
|
|
if withHelp {
|
|
|
|
help = cmd.Bool([]string{"#help", "-help"}, false, "Print usage")
|
|
|
|
}
|
|
|
|
if err := cmd.Parse(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if help != nil && *help {
|
|
|
|
cmd.Usage()
|
|
|
|
// just in case Usage does not exit
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if str := cmd.CheckArgs(); str != "" {
|
|
|
|
if withHelp {
|
2014-11-13 09:39:51 -05:00
|
|
|
if os.Args[0] == cmd.Name() {
|
|
|
|
str += ". See '" + os.Args[0] + " --help'"
|
2014-11-19 03:42:10 -05:00
|
|
|
} else {
|
|
|
|
str += ". See '" + os.Args[0] + " " + cmd.Name() + " --help'"
|
2014-11-13 09:39:51 -05:00
|
|
|
}
|
2014-11-05 11:57:51 -05:00
|
|
|
}
|
|
|
|
fmt.Fprintf(cmd.Out(), "docker: %s.\n", str)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|