JanDeDobbeleer/oh-my-posh · error

required flag(s) "%s" not set

Error message

required flag(s) "%s" not set

What it means

After parsing flags, Command.execute iterates the names collected by MarkPersistentFlagRequired and verifies each is Changed on the merged flag set. If a required persistent flag was not supplied on the command line, execution stops with this error, routed through flagError so usage is printed. Defaults do not count - only an explicit Set marks Changed.

Source

Thrown at src/cmdtree/cmdtree.go:323

	return result
}

func (c *Command) execute(args []string) error {
	c.registerHelpFlag()
	flags := c.mergedFlags()

	if err := flags.Parse(args); err != nil {
		return c.flagError(err)
	}

	if c.helpRequested {
		return c.Help()
	}

	for _, name := range c.requiredFlags {
		if !flags.Changed(name) {
			return c.flagError(fmt.Errorf("required flag(s) \"%s\" not set", name))
		}
	}

	positionals := flags.Args()

	if c.Args != nil {
		if err := c.Args(c, positionals); err != nil {
			return c.flagError(err)
		}
	}

	if c.Run == nil {
		return c.Help()
	}

	if hook := c.findHook(func(cmd *Command) func(*Command, []string) { return cmd.PersistentPreRun }); hook != nil {
		hook(c, positionals)
	}

View on GitHub (pinned to 0976794618)

Solutions

  1. Pass the required flag explicitly: command --flag value on every invocation.
  2. Wrap the flag with a default at registration if it should not be required - then remove the MarkPersistentFlagRequired call.
  3. In scripts, source a default (e.g. ${CFG:-default.toml}) so the flag is always supplied.
  4. Check release notes after upgrading the CLI: required flags may have changed.

Example fix

// before
args := []string{"shell"} // required flag(s) "config" not set
// after
args := []string{"--config", "my.toml", "shell"}
Defensive patterns

Strategy: validation

Validate before calling

func ensureRequired(flags *cmdflag.FlagSet, required []string, args []string) error {
    _ = flags.Parse(args) // pre-parse or inspect raw args
    for _, n := range required {
        if !flags.Changed(n) {
            return fmt.Errorf("pass --%s explicitly; it is required by this command", n)
        }
    }
    return nil
}

Try / catch

if err := cmd.Execute(); err != nil {
    if m := regexp.MustCompile(`required flag\(s\) "(.+)" not set`).FindStringSubmatch(err.Error()); m != nil {
        fmt.Fprintf(os.Stderr, "missing required flag: --%s\n", m[1])
        os.Exit(2)
    }
    return err
}

Prevention

When it happens

Trigger: Executing a command that had MarkPersistentFlagRequired(name) called, without passing --name (or -short) on the command line, even if the flag has a non-empty default value.

Common situations: Scripts omitting a flag that a newer version made mandatory; users unaware a flag became required after an upgrade; env-var-driven invocations that relied on the default; help output missed because the error prints after parse succeeds.

Understand the failure class

Background: "--flag is required" and "must specify" CLI errors: how missing-required-flag validation works and how to fix it — this error's family across 20 libraries.

Related errors


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