« first day (2174 days earlier)      last day (2300 days later) » 

2:39 AM
feeling stupid here, since I have already posted a question in this area, mathematica.stackexchange.com/q/43088/9490, but still I don't understand
In[5]:= ClearAll@func
Options[func] = {a -> 1, b -> 2};
func[x_, OptionsPattern[]] := 3
func[1, c -> 3]

Out[8]= 3
What do I need to do to get func to issue an error message on an unrecognized option?
Plot[x + 2, {x, -2, 2}, "notAnOption" -> 3] gives a Plot::optx error
 
 
3 hours later…
5:22 AM
@JasonB. Good morning. Actually, it is not that hard and if you refer to Plot then it doesn't use the style that Leonid suggested.
Try this here:
ClearAll[func, checkOpts];
func::optx = "Unknown option `` in ``.";
Options[func] = {a -> 1, b -> 2};
PatternTest[func[x_, opts : OptionsPattern[]],
   Function[arg, checkOpts[arg], HoldFirst]] := x;

SetAttributes[checkOpts, HoldFirst];
checkOpts[call : func[arg_, opts : OptionsPattern[]]] :=
 With[{bad = FilterRules[{opts}, Except[Options[func]]]},
  If[Length[bad] > 0,
   Message[func::optx, First[bad], HoldForm[call]];
   False,
   True,
   True]
  ]
You simply write a function checkOpts that takes the whole function call as argument and uses FilterRules to find options that don't belong to func. Since you have the complete call pattern, you can print the original call in the message.
If you like to generalize this, you can replace func with (func_) and args_ with args__ and it will work on all of your functions that have the optx message defined.
 
 
3 hours later…
8:29 AM
 
@halirutan how does your site theme estimate the read-time? It's a cute feature.
 
@b3m2a1 (wordcount/500 + codblockcount*3+formulacount*20)*130/(IQofTheReader+1)
If the IQ of the reader is unknow, it's simply 100 words per minute :)
Actually, I don't like this feature very much because it is useless. You can surely estimate how long it will take to read an article. Understanding is a different matter especially, when people write about math or physics.
 
 
11 hours later…
7:37 PM
@halirutan Thanks - for some reason I thought this was handled automatically. But it looks like all the built-in functions use an options-checking function like this as well.
 
 
2 hours later…
9:26 PM
@JasonB. I think you might be looking for System`Private`TestOptions
If you did this:
Options[func] = {a -> 1, b -> 2};
func[x_, OptionsPattern[]] := (OptionValue[b]; 3)
func[1, c -> 3]

OptionValue::nodef :  Unknown option c for func.

3
You'd get an error telling you you used a bad Option. I believe this routes through System`Private`TestOptions as all the OptionsPattern code seems to route through System`Private`
How that function works, I do not know.
But it's got some DownValue:
System`Private`TestOptions // System`Private`HasDownCodeQ

True
And it's HoldFirst
 

« first day (2174 days earlier)      last day (2300 days later) »