urfave/cli · error

no such flag -%s

Error message

no such flag -%s

What it means

Command.Set looks up a registered flag by name on the command (and its persistent/parent flags). When no flag with that name exists, it returns 'no such flag -<name>' instead of silently ignoring the assignment. It is the programmatic equivalent of passing an undefined flag on the command line.

Source

Thrown at command.go:539

func (cmd *Command) setMultiValueParsingConfig(f Flag) {
	tracef("setMultiValueParsingConfig %T, %+v", f, f)
	if cf, ok := f.(multiValueParsingConfigSetter); ok {
		cf.setMultiValueParsingConfig(multiValueParsingConfig{
			SliceFlagSeparator:        cmd.SliceFlagSeparator,
			DisableSliceFlagSeparator: cmd.DisableSliceFlagSeparator,
			MapFlagKeyValueSeparator:  cmd.MapFlagKeyValueSeparator,
		})
	}
}

// Set sets a context flag to a value.
func (cmd *Command) Set(name, value string) error {
	if f := cmd.lookupFlag(name); f != nil {
		cmd.setMultiValueParsingConfig(f)
		return f.Set(name, value)
	}

	return fmt.Errorf("no such flag -%s", name)
}

// IsSet determines if the flag was actually set
func (cmd *Command) IsSet(name string) bool {
	fl := cmd.lookupFlag(name)
	if fl == nil {
		tracef("flag with name %[1]q NOT found; assuming not set (cmd=%[2]q)", name, cmd.Name)
		return false
	}

	isSet := fl.IsSet()
	if isSet {
		tracef("flag with name %[1]q is set (cmd=%[2]q)", name, cmd.Name)
	} else {
		tracef("flag with name %[1]q is no set (cmd=%[2]q)", name, cmd.Name)
	}

	return isSet

View on GitHub (pinned to 1a4deb4f5a)

Solutions

  1. Fix the flag name passed to Set, matching the declared Name or an alias exactly
  2. Ensure the flag is declared in the Flags field of the command (or root/persistent flags) before Set is called
  3. Use cmd.lookupFlag-equivalent checks like cmd.IsSet/cmd.Flag names first, or iterate cmd.Flags to verify existence

Example fix

// before
cmd.Set("verbose", "true") // flag declared as 'verbosity'
// after
cmd.Set("verbosity", "true")
Defensive patterns

Strategy: validation

Validate before calling

if cmd.LookupFlagName == nil { for _, f := range cmd.Flags { _ = f.Names() } }
// preferred: probe before Set
if err := cmd.Set(name, val); err != nil && strings.HasPrefix(err.Error(), "no such flag") { /* handle */ }

Type guard

func hasFlag(cmd *cli.Command, name string) bool {
    for _, f := range cmd.Flags {
        for _, n := range f.Names() {
            if n == name { return true }
        }
    }
    return false
}

Prevention

When it happens

Trigger: Calling cmd.Set(name, value) with a name that was never registered via Flags (or aliases), a typo in the flag name, or referencing a flag defined only on a different (non-root/non-parent) command.

Common situations: Setting flags dynamically from config files or environment where key names are free-form; tests invoking Set on a subcommand while the flag is declared on the root; renaming a flag and forgetting to update Set call sites.

Related errors


AI-assisted analysis of urfave/cli@1a4deb4f5a (2026-08-31). Data as JSON: /api/errors/9c2b5e9d87c4a7b6. Report an issue: GitHub.