JanDeDobbeleer/oh-my-posh · error

no such flag -%v

Error message

no such flag -%v

What it means

MarkPersistentFlagRequired can only mark flags registered on the command's own PersistentFlags set. It looks the name up in PersistentFlags() and returns this error when the flag is not found there - typically because it was registered as a local flag (Flags()), on a different command, or never registered at all. No state is changed when this happens.

Source

Thrown at src/cmdtree/cmdtree.go:132

func (c *Command) PersistentFlags() *cmdflag.FlagSet {
	if c.pflags == nil {
		c.pflags = cmdflag.NewFlagSet(c.Name(), cmdflag.ContinueOnError)
	}

	return c.pflags
}

func (c *Command) SetArgs(args []string) {
	c.setArgs = args
}

// MarkPersistentFlagRequired only applies to a flag registered on this
// command's own persistent set and errors otherwise.
func (c *Command) MarkPersistentFlagRequired(name string) error {
	flag := c.PersistentFlags().Lookup(name)
	if flag == nil {
		return fmt.Errorf("no such flag -%v", name)
	}

	c.requiredFlags = append(c.requiredFlags, name)
	return nil
}

func (c *Command) hasSubCommands() bool {
	for _, cmd := range c.commands {
		if !cmd.Hidden && cmd.Name() != "help" {
			return true
		}
	}

	return false
}

func (c *Command) findChild(name string) *Command {
	for _, cmd := range c.commands {

View on GitHub (pinned to 0976794618)

Solutions

  1. Register the flag on the command's persistent set first: cmd.PersistentFlags().StringVar(...) before MarkPersistentFlagRequired.
  2. Verify the exact flag name matches the registered name (watch for typos).
  3. If the flag is meant to be local-only, add a check in Run or use the requiredFlags mechanism appropriate to local flags instead.
  4. Check you are calling the method on the command that owns the flag, not a parent or child.

Example fix

// before
cmd.Flags().Bool("upgrade", false, "upgrade")
err := cmd.MarkPersistentFlagRequired("upgrade") // no such flag -upgrade
// after
cmd.PersistentFlags().BoolVar(&upgrade, "upgrade", false, "upgrade")
err := cmd.MarkPersistentFlagRequired("upgrade") // nil
Defensive patterns

Strategy: try-catch

Validate before calling

func requirePersistent(cmd *cmdtree.Command, names ...string) error {
    for _, n := range names {
        if cmd.PersistentFlags().Lookup(n) == nil {
            return fmt.Errorf("flag %q must be registered via PersistentFlags before MarkPersistentFlagRequired", n)
        }
    }
    return nil
}

Type guard

func hasPersistentFlag(cmd *cmdtree.Command, name string) bool {
    return cmd.PersistentFlags().Lookup(name) != nil
}

Try / catch

if err := cmd.MarkPersistentFlagRequired("config"); err != nil {
    // "no such flag -config": registration order or ownership problem
    panic(fmt.Sprintf("CLI setup bug: %v", err)) // fail fast at startup, not at runtime
}

Prevention

When it happens

Trigger: Calling cmd.MarkPersistentFlagRequired("name") where cmd.PersistentFlags().Lookup("name") returns nil - e.g. the flag was declared with cmd.Flags().StringVar(...) instead of cmd.PersistentFlags().StringVar(...), or a typo in the name.

Common situations: Copy-pasted cobra-style setup code where the flag was added as a local flag; marking a child's flag required from the parent command; renaming a flag in one place but not in the Mark... call; forgetting registration entirely before marking required.

Related errors


AI-assisted analysis of JanDeDobbeleer/oh-my-posh@0976794618 (2026-08-31). Data as JSON: /api/errors/a7020f5660faa7c5. Report an issue: GitHub.