# How do I add options to a built in function, which only apply to a certain class of argument?

I'd like to add an option to a built-in function that only applies when it is given an argument of a certain form.

For example, something like this approach to extending DateString to support an extra option when it is given a date argument that matches the form zoned[...] (forget that zoned does nothing here):

DateString[zoned[x_], spec_, opts : OptionsPattern[{TimeZone -> 0,ShowZone->True}]] ^:=
DateString[x, If[OptionValue[ShowZone],
Append[spec, If[OptionValue[TimeZone] != 0, "UTC" <> ToString[OptionValue[TimeZone]], "Z"]], spec]]


Since ShowZone is not an option for DateString, I get an error.

"DateString::optx: Unknown option ShowZone in DateString..."

But I can't just associate TimeZone as an up value for zoned with

zoned/:Options[DateString]={ShowZone->True}


because that also results in an error:

Options::tag: Rule for Options of Options[DateString] can only be attached to DateString.

It seems I have to (unprotect DateString and) then add a new option globally, which is generally just ignored:

Unprotect[DateString];
Options[DateString] = Union[Options[DateString], {ShowZone -> False}];
Protect[DateString];


Is this the only approach? Even this approach leaves ShowZone blue in the function definition and red in function invocations.

-
What error do you get? Can you add some sample use which leads to an error? –  Leonid Shifrin Dec 6 '12 at 19:21
@LeonidShifrin: "Options::tag: Rule for Options of Options[DateString] can only be attached to DateString." –  raxacoricofallapatorius Dec 6 '12 at 19:31
I dont see why do you need that definition at all (the one with Options). The following input, for example, works without a problem: DateString[zoned[{1959, 8, 29, 0, 0, 0}], {"Year"}, ShowZone -> True]. –  Leonid Shifrin Dec 6 '12 at 19:46
@LeonidShifrin: (Sorry, the error above is for the assignment as an upvalue.) The error I get from using ShowZone in DateString is: "DateString::optx: Unknown option ShowZone in ..." –  raxacoricofallapatorius Dec 6 '12 at 19:50
Then we are back to my original request: please add an example illustrating the error. Generally, it is a good practice to provide fully self-contained description, not to make us guess the missing parts. –  Leonid Shifrin Dec 6 '12 at 19:53