Helper to optionally wrap a program?

I have a program foo. I want to create a wrapper to add an argument foo -u X, BUT only if the -u option is not provided.

foo-wrapper calls foo -u X, but foo-wrapper -u Y calls foo-wrapper -u Y.

Is there already a helper in nixpkgs to do that?

I don’t recall seeing anything like this myself, and I’d be a little surprised if it is common enough of a need for someone to have built in an argument parser and some kind of declaration language for it.

What does foo do if you pass the -u argument twice? If it only respects the first or last option, I guess you could add the default flag in the appropriate location:

I guess it’d be prone to break if they changed the behavior later.