// Parse parses the command line for flags and arguments.
// v should be a pointer to a struct, else it panics.
Parse parses the command line for flags and arguments.
v should be a pointer to a struct, else it panics.
// Parse parses the command line for flags and arguments . // v should be a pointer to a struct else it panics .
func Parse(v interface{}, opts ...ParseOpt) (fs *flag.FlagSet, err error) {
	if v == nil {
		v = &struct{}{}
	}
	options := &parseOptions{
		errorHandling: flag.ExitOnError,
	}
	for _, o := range opts {
		o(options)
	}
	ctx := getParsingContext()
	fs = ctx.getFlagSet()
	fs.Init("", options.errorHandling)
	if err = ctx.parseTags(reflect.ValueOf(v).Elem()); err != nil {
		return fs, err
	}
	if options.cmdName != nil {
		ctx.name = *options.cmdName
	}
	osArgs := os.Args[1:]
	if ctx.args != nil {
		osArgs = *ctx.args
	}
	if options.args != nil {
		osArgs = *options.args
	}
	if hasBoolFlag(showHiddenFlag, osArgs) {
		ctx.showHidden = true
		fs.BoolVar(&ctx.showHidden, showHiddenFlag, true, "show hidden commands and flags")
	}
	if err = ctx.readEnvForNonSliceValues(); err != nil {
		return fs, err
	}
	if err = fs.Parse(osArgs); err != nil {
		return fs, err
	}
	if err = ctx.parseNonflags(); err != nil {
		return fs, err
	}
	if err = ctx.checkRequired(); err != nil {
		return fs, err
	}
	ctx.tidyFlagSet()
	return fs, err
}
func Parse ( v interface { } , opts ... ParseOpt ) ( fs * flag . FlagSet , err error ) { if v == nil { v = & struct { } { } } options := & parseOptions { errorHandling : flag . ExitOnError , } for _ , o := range opts { o ( options ) } ctx := getParsingContext ( ) fs = ctx . getFlagSet ( ) fs . Init ( " " , options . errorHandling ) if err = ctx . parseTags ( reflect . ValueOf ( v ) . Elem ( ) ) ; err != nil { return fs , err } if options . cmdName != nil { ctx . name = * options . cmdName } osArgs := os . Args [ 1 : ] if ctx . args != nil { osArgs = * ctx . args } if options . args != nil { osArgs = * options . args } if hasBoolFlag ( showHiddenFlag , osArgs ) { ctx . showHidden = true fs . BoolVar ( & ctx . showHidden , showHiddenFlag , true , " " ) } if err = ctx . readEnvForNonSliceValues ( ) ; err != nil { return fs , err } if err = fs . Parse ( osArgs ) ; err != nil { return fs , err } if err = ctx . parseNonflags ( ) ; err != nil { return fs , err } if err = ctx . checkRequired ( ) ; err != nil { return fs , err } ctx . tidyFlagSet ( ) return fs , err }
Parse parses the command line for flags and arguments.
Parse parses the command line for flags and arguments .
// Read env for non-slice values before parsing command line // flags and arguments.