Skip to content

feat: add Command.ArgValidator for tree-wide argument validation#2377

Open
dearchap wants to merge 1 commit into
urfave:mainfrom
dearchap:feat/2327-arg-validator
Open

feat: add Command.ArgValidator for tree-wide argument validation#2377
dearchap wants to merge 1 commit into
urfave:mainfrom
dearchap:feat/2327-arg-validator

Conversation

@dearchap

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • feature

What this PR does / why we need it:

Adds ArgValidator to *cli.Command — an optional hook that runs before Before and Action. It is inherited by subcommands unless they set their own. Returning a non-nil error short-circuits the command and propagates as the exit error.

This is useful for security-boundary CLIs that need to reject shell metacharacters or other patterns before user-supplied bytes reach an exec call, as well as for tracing, metrics, or authz middleware that applies tree-wide.

root := &cli.Command{
    ArgValidator: func(ctx context.Context, cmd *cli.Command) error {
        for _, arg := range cmd.Args().Slice() {
            if containsMetachar(arg) {
                return fmt.Errorf("rejected: %q", arg)
            }
        }
        return nil
    },
}

Which issue(s) this PR fixes:

Closes #2327

Testing

  • go test ./... passes
  • go vet ./... passes
  • 5 new tests cover: runs before action, error short-circuits, inherits from parent, subcommand override, runs before Before hook
  • make v3diff shows only the new ArgValidatorFunc type and ArgValidator field
  • make v3approve run to update API surface docs

Release Notes

Add Command.ArgValidator for tree-wide argument validation with subcommand inheritance.

@dearchap dearchap requested a review from a team as a code owner June 29, 2026 21:04
ArgValidator is an optional hook that runs before Before and Action.
It is inherited by subcommands unless they set their own. Returning
a non-nil error short-circuits the command.

Closes urfave#2327
@dearchap dearchap force-pushed the feat/2327-arg-validator branch from 0454780 to 1efb810 Compare June 29, 2026 21:07
@dearchap dearchap requested a review from coilysiren June 29, 2026 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add tree-wide argv-validation hook / middleware

1 participant