internal/cmdflag/cmdflag.go
func Parse() error {
	args := os.Args
	if len(args) == 1 {
		return nil
	}

	// Global flags.
	fset := flag.CommandLine
	fset.Usage = Usage
	out := fsetOutput(fset)

	if err := fset.Parse(args[1:]); err != nil {
		return err
	}

	// Handle version request.
	if f := fset.Lookup(VersionBoolFlag); f != nil {
		if v, ok := f.Value.(flag.Getter); ok {
			// All values implemented by the flag package implement the flag.Getter interface.
			if b, ok := v.Get().(bool); ok && b {
				// The flag was defined as a bool and is set.
				program := programName(args[0])
				fmt.Fprintf(out, "%s version %s %s/%s\n",
					program, buildinfo(),
					runtime.GOOS, runtime.GOARCH)
				return nil
			}
		}
	}

	// No cmdflag.
	if fset.NArg() == 0 {
		return nil
	}

	// Subcommand.
	idx := len(args) - fset.NArg()
	s := args[idx]
	args = args[idx+1:]
	for _, sub := range subs {
		if sub.name != s {
			continue
		}

		fs, handler := sub.init(out)
		if err := fs.Parse(args); err != nil {
			return err
		}
		return handler(args[len(args)-fs.NArg():]...)
	}

	return fmt.Errorf("%s is not a valid cmdflag", s)
}
func Parse() error {
	args := os.Args
	if len(args) == 1 {
		return nil
	}

	// Global flags.
	fset := flag.CommandLine
	fset.Usage = Usage
	out := fsetOutput(fset)

	if err := fset.Parse(args[1:]); err != nil {
		return err
	}

	// Handle version request.
	if f := fset.Lookup(VersionBoolFlag); f != nil {
		if v, ok := f.Value.(flag.Getter); ok {
			// All values implemented by the flag package implement the flag.Getter interface.
			if b, ok := v.Get().(bool); ok && b {
				// The flag was defined as a bool and is set.
				program := programName(args[0])
				fmt.Fprintf(out, "%s version %s %s/%s\n",
					program, buildinfo(),
					runtime.GOOS, runtime.GOARCH)
				return nil
			}
		}
	}

	// No cmdflag.
	if fset.NArg() == 0 {
		return nil
	}

	// Subcommand.
	idx := len(args) - fset.NArg()
	s := args[idx]
	args = args[idx+1:]
	for _, sub := range subs {
		if sub.name != s {
			continue
		}

		fs, handler := sub.init(out)
		if err := fs.Parse(args); err != nil {
			return err
		}
		return handler(args[len(args)-fs.NArg():]...)
	}

	return fmt.Errorf("%s is not a valid cmdflag", s)
}
func Parse ( ) error { args := os . Args if len ( args ) == 1 { return nil } // Global flags. fset := flag . CommandLine fset . Usage = Usage out := fsetOutput ( fset ) if err := fset . Parse ( args [ 1 : ] ) ; err != nil { return err } // Handle version request. if f := fset . Lookup ( VersionBoolFlag ) ; f != nil { if v , ok := f . Value . ( flag . Getter ) ; ok { // All values implemented by the flag package implement the flag.Getter interface. if b , ok := v . Get ( ) . ( bool ) ; ok && b { // The flag was defined as a bool and is set. program := programName ( args [ 0 ] ) fmt . Fprintf ( out , " \n " , program , buildinfo ( ) , runtime . GOOS , runtime . GOARCH ) return nil } } } // No cmdflag. if fset . NArg ( ) == 0 { return nil } // Subcommand. idx := len ( args ) - fset . NArg ( ) s := args [ idx ] args = args [ idx + 1 : ] for _ , sub := range subs { if sub . name != s { continue } fs , handler := sub . init ( out ) if err := fs . Parse ( args ) ; err != nil { return err } return handler ( args [ len ( args ) - fs . NArg ( ) : ] ... ) } return fmt . Errorf ( " " , s ) }
// Parse parses the command line arguments including the global flags and, if any, the cmdflag and its flags.
//
// If the VersionBoolFlag is defined as a global boolean flag, then the program version is displayed and the program stops.
Parse parses the command line arguments including the global flags and if any the cmdflag and its flags . If the VersionBoolFlag is defined as a global boolean flag then the program version is displayed and the program stops .