本文整理汇总了Golang中flag.FlagSet.Var方法的典型用法代码示例。如果您正苦于以下问题:Golang FlagSet.Var方法的具体用法?Golang FlagSet.Var怎么用?Golang FlagSet.Var使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flag.FlagSet
的用法示例。
在下文中一共展示了FlagSet.Var方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Apply
// Apply populates the flag given the flag set and environment
func (f IntSliceFlag) Apply(set *flag.FlagSet) {
if f.EnvVars != nil {
for _, envVar := range f.EnvVars {
if envVal := os.Getenv(envVar); envVal != "" {
newVal := NewIntSlice()
for _, s := range strings.Split(envVal, ",") {
s = strings.TrimSpace(s)
err := newVal.Set(s)
if err != nil {
fmt.Fprintf(ErrWriter, err.Error())
}
}
f.Value = newVal
break
}
}
}
if f.Value == nil {
f.Value = NewIntSlice()
}
for _, name := range f.Names() {
set.Var(f.Value, name, f.Usage)
}
}
示例2: parseArgs
func (a *app) parseArgs(fs *flag.FlagSet, args []string) error {
fs.SetOutput(a.out)
fs.Usage = func() {
fmt.Fprintf(a.out, help, args[0], args[0])
fs.PrintDefaults()
}
fs.Var(&a.typeNames, "type",
"A generated proto.Message type to generate stubs for (required, repeatable)")
fs.StringVar(&a.outFile, "out", "proto_gae.gen.go",
"The name of the output file")
fs.StringVar(&a.header, "header", copyright, "Header text to put at the top of "+
"the generated file. Defaults to the Chromium Authors copyright.")
if err := fs.Parse(args[1:]); err != nil {
return err
}
fail := errors.MultiError(nil)
if a.typeNames.Data == nil || a.typeNames.Data.Len() == 0 {
fail = append(fail, errors.New("must specify one or more -type"))
}
if !strings.HasSuffix(a.outFile, ".go") {
fail = append(fail, errors.New("-output must end with '.go'"))
}
if len(fail) > 0 {
for _, e := range fail {
fmt.Fprintln(a.out, "error:", e)
}
fmt.Fprintln(a.out)
fs.Usage()
return fail
}
return nil
}
示例3: Register
func (cmd *mark) Register(ctx context.Context, f *flag.FlagSet) {
cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
cmd.HostSystemFlag.Register(ctx, f)
f.Var(flags.NewOptionalBool(&cmd.ssd), "ssd", "Mark as SSD")
f.Var(flags.NewOptionalBool(&cmd.local), "local", "Mark as local")
}
示例4: Register
func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
cmd.HostSystemFlag.Register(ctx, f)
modes := []string{
string(types.HostMountModeReadOnly),
string(types.HostMountModeReadWrite),
}
f.StringVar(&cmd.Name, "name", "", "Datastore name")
f.Var(&cmd.Type, "type", fmt.Sprintf("Datastore type (%s)", strings.Join(allTypes, "|")))
f.BoolVar(&cmd.Force, "force", false, "Ignore DuplicateName error if datastore is already mounted on a host")
// Options for NAS
f.StringVar(&cmd.RemoteHost, "remote-host", "", "Remote hostname of the NAS datastore")
f.StringVar(&cmd.RemotePath, "remote-path", "", "Remote path of the NFS mount point")
f.StringVar(&cmd.AccessMode, "mode", modes[0],
fmt.Sprintf("Access mode for the mount point (%s)", strings.Join(modes, "|")))
f.StringVar(&cmd.UserName, "username", "", "Username to use when connecting (CIFS only)")
f.StringVar(&cmd.Password, "password", "", "Password to use when connecting (CIFS only)")
// Options for VMFS
f.StringVar(&cmd.DiskCanonicalName, "disk", "", "Canonical name of disk (VMFS only)")
// Options for Local
f.StringVar(&cmd.Path, "path", "", "Local directory path for the datastore (local only)")
}
示例5: DefineFlags
func (cmd *add) DefineFlags(fs *flag.FlagSet) {
fs.BoolVar(&cmd.json, "json", false, "Output result as JSON")
fs.StringVar(&cmd.uri, "uri", "", "Drain URI (eg: udp://logs.loggly.com:12345)")
fs.Var(&cmd.filters, "filter", "Message filter")
cmd.params = make(map[string]string)
fs.Var(&cmd.params, "o", "Drain options (eg: -o 'limit=100' or -o 'format={{.Text}}'")
}
示例6: Register
func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
cmd.HostSystemFlag.Register(ctx, f)
f.StringVar(&cmd.spec.VswitchName, "vswitch", "", "vSwitch Name")
f.Var(flags.NewInt32(&cmd.spec.VlanId), "vlan", "VLAN ID")
}
示例7: Register
func (cmd *change) Register(f *flag.FlagSet) {
f.Int64Var(&cmd.MemoryMB, "m", 0, "Size in MB of memory")
f.IntVar(&cmd.NumCPUs, "c", 0, "Number of CPUs")
f.StringVar(&cmd.GuestId, "g", "", "Guest OS")
f.StringVar(&cmd.Name, "name", "", "Display name")
f.Var(&cmd.extraConfig, "e", "ExtraConfig. <key>=<value>")
}
示例8: NewFlagOverwrite
func NewFlagOverwrite(
set *flag.FlagSet,
config *Config,
name, path, def, usage string,
) *string {
if config == nil {
panic("Missing configuration")
}
if path == "" {
panic("empty path")
}
if def != "" {
err := config.SetString(path, -1, def)
if err != nil {
panic(err)
}
}
f := &flagOverwrite{
config: config.access(),
path: path,
value: def,
}
if set == nil {
flag.Var(f, name, usage)
} else {
set.Var(f, name, usage)
}
return &f.value
}
示例9: FlagByType
// FlagByType sets the appropriate flag for its type.
func FlagByType(fs *flag.FlagSet, structName string, fval reflect.Value, ftype reflect.StructField) {
// Get a pointer; FlagSet needs a pointer to set the struct's field
if fval.Kind() == reflect.Ptr {
// Short-circuit
log.Printf("Skipping field %s: %s", ftype.Name, ftype.Type.String())
return
}
//log.Printf("Getting pointer to %s", ftype.Name)
fval = fval.Addr()
flagName := NameToFlag(ftype.Name)
flagHelp := fmt.Sprintf("%s:%s", structName, ftype.Name)
log.Printf("Converting %s => %s", ftype.Name, flagName)
//log.Printf("Switching on type %s...", ftype.Type.String())
switch fval := fval.Interface().(type) {
case *int:
fs.IntVar(fval, flagName, 0, flagHelp)
case *float64:
fs.Float64Var(fval, flagName, 0.0, flagHelp)
case *string:
fs.StringVar(fval, flagName, "", flagHelp)
case *bool:
fs.BoolVar(fval, flagName, false, flagHelp)
case *time.Time:
t := (*time.Time)(fval) // Get a *time.Time pointer to fval
*t = time.Now() // Set a default of time.Now()
fs.Var((*TimeFlag)(fval), flagName, flagHelp)
default:
log.Printf("unexpected type %s\n", ftype.Type.String())
}
}
示例10: Register
func (cmd *start) Register(ctx context.Context, f *flag.FlagSet) {
cmd.GuestFlag, ctx = newGuestFlag(ctx)
cmd.GuestFlag.Register(ctx, f)
f.StringVar(&cmd.dir, "C", "", "The absolute path of the working directory for the program to start")
f.Var(&cmd.vars, "e", "Set environment variable (key=val)")
}
示例11: influxdbCommandLine
func influxdbCommandLine(cli *flag.FlagSet) CommandLineHandler {
ix := &influx{
logger: NewLogger("[ostent sendto-influxdb] "),
RefreshFlag: flags.Period{Duration: 10 * time.Second}, // 10s default
// ServerAddr: flags.NewBind(8086),
}
cli.Var(&ix.RefreshFlag, "influxdb-refresh", "InfluxDB refresh interval")
cli.StringVar(&ix.ServerAddr, "sendto-influxdb", "", "InfluxDB server address")
cli.StringVar(&ix.Database, "influxdb-database", "ostent", "InfluxDB database")
cli.StringVar(&ix.Username, "influxdb-username", "", "InfluxDB username")
cli.StringVar(&ix.Password, "influxdb-password", "", "InfluxDB password")
return func() (AtexitHandler, bool, error) {
if ix.ServerAddr == "" {
return nil, false, nil
}
ostent.AddBackground(func(defaultPeriod flags.Period) {
/* if ix.RefreshFlag.Duration == 0 { // if .RefreshFlag had no default
ix.RefreshFlag = defaultPeriod
} */
go influxdb.Influxdb(ostent.Reg1s.Registry, ix.RefreshFlag.Duration, &influxdb.Config{
Host: ix.ServerAddr, //.String(),
Database: ix.Database,
Username: ix.Username,
Password: ix.Password,
})
})
return nil, false, nil
}
}
示例12: Register
func (s *ResourceConfigSpecFlag) Register(f *flag.FlagSet) {
opts := []struct {
name string
units string
*types.ResourceAllocationInfo
}{
{"CPU", "MHz", &s.CpuAllocation},
{"Memory", "MB", &s.MemoryAllocation},
}
for _, opt := range opts {
prefix := strings.ToLower(opt.name)[:3]
shares := (*sharesInfo)(opt.Shares)
expandableReservation := false
if v := opt.ExpandableReservation; v != nil {
expandableReservation = *v
}
// Initialize bool pointer
opt.ExpandableReservation = types.NewBool(false)
f.Int64Var(&opt.Limit, prefix+".limit", 0, opt.name+" limit in "+opt.units)
f.Int64Var(&opt.Reservation, prefix+".reservation", 0, opt.name+" reservation in "+opt.units)
f.BoolVar(opt.ExpandableReservation, prefix+".expandable", expandableReservation, opt.name+" expandable reservation")
f.Var(shares, prefix+".shares", opt.name+" shares level or number")
}
}
示例13: registerFlag
func registerFlag(set *goflag.FlagSet, v goflag.Value, name, usage string) {
if set != nil {
set.Var(v, name, usage)
} else {
goflag.Var(v, name, usage)
}
}
示例14: flRun
func flRun(fl *flag.FlagSet) {
flPodManifest(fl)
SaveIDFlag(fl)
fl.Var(&flAppName, "app", "Specify app to run for a multi-app pod")
fl.BoolVar(&flDestroy, "destroy", false, "Destroy pod when done")
fl.BoolVar(&flTerminal, "t", false, "Attach app to the terminal (single-app containers only)")
}
示例15: Flag
// Flag configures the given FlagSet with the flags to configure
// all active experiments.
func Flag(fs *flag.FlagSet) {
for _, id := range All {
desc := id.Flag()
key := fmt.Sprintf("X%s", id.Flag())
fs.Var(&idValue{X: id}, key, desc)
}
}