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
- Pass the required flag explicitly: command --flag value on every invocation.
- Wrap the flag with a default at registration if it should not be required - then remove the MarkPersistentFlagRequired call.
- In scripts, source a default (e.g. ${CFG:-default.toml}) so the flag is always supplied.
- 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
- After upgrading the CLI, re-run scripts once with --help to catch newly required flags.
- Default the value in the wrapper script: ${CFG:+--config $CFG} plus an error if unset.
- Remember a registered default does not satisfy a required flag - Changed is set only by an explicit value.
- Keep required-flag documentation in the command's Short/Long so users see it before failing.
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
- --data-only and --data-derive contradict each other: one for
- font path must be a valid URL
- no such flag -%v
- unknown command %q for %q
- invalid argument %q for %q
AI-assisted analysis of JanDeDobbeleer/oh-my-posh@0976794618 (2026-08-31).
Data as JSON: /api/errors/e6ba903814c34a94.
Report an issue: GitHub.