MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Help with Check Function Arguments & Options

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87289] Re: Help with Check Function Arguments & Options
  • From: "David Park" <djmpark at comcast.net>
  • Date: Sun, 6 Apr 2008 06:46:03 -0400 (EDT)
  • References: <ft7gkq$bve$1@smc.vnet.net>

Andrew,

Assuming you are using Mathematica Version 6, you could write your function 
something like the following:

f::usage =
  "f[x,y,option] will peform an operation on x and y depending on the 
option.";
fOperation::usage =
  "fOperation \[Rule] Plus is an option for f that gives the operation for 
combining the two arguments.";
Options[f] = {fOperation -> Plus};
SyntaxInformation[
   f] = {"ArgumentsPattern" -> {_, _, OptionsPattern[]}};
f[x_, y_, OptionsPattern[]] :=
 Module[{op},
  op = OptionValue[fOperation];
  op[x, y]
  ]

Then if the user types f[a] the input appears with a little red caret that 
indicates more arguments are needed. If evaluated with the wrong number of 
arguments it just returns unevaluated. If the user uses an incorrect option 
such as f[a,b,PlotRange->5] then PlotRange will show in red, evaluate with 
the default option value, and give a warning message.

f[a, b]
a + b

f[a, b, fOperation -> Times]
a b

f[a, b, fOperation -> (#1^2 + #2 &)]
a^2 + b

-- 
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/


"Andrew Beveridge" <acbev at lanl.gov> wrote in message 
news:ft7gkq$bve$1 at smc.vnet.net...
>I would like to thank everyone who has previously responded to my
> posts. Your advice has helped greatly.
>
> I have a couple of final questions.
>
> I am writing a package file and would like to do some argument and
> option checking if possible; that is check the number of arguments
> and see if there are unknown options.
>
> If my function has no options I think I can do this.
>
> f[x_,y_] := x + y
> f[args___] := If[Length[{args}] != 2, Message[f::"argrx", f,
> Length[{args}], 2]; Return[$Failed]]
>
> This type of check has been posted to the message board before.
>
> My question is assume that f[x,y] has multiple options, that is
> Options[f] = {opt1 -> 1, opt2 -> True}, etc
>
> How do I check if the user entered the correct options?
>
> Thanks again for all of your help.
>
> Regards,
> Andrew
>
> Andrew C Beveridge
> MS M888
> Los Alamos National Laboratory
> Los Alamos, NM  87545
>
> 505-665-2092
> e-mail: acbev at lanl.gov
>
> 



  • Prev by Date: Re: Re: Label vertices in Graph to display with GraphPlot
  • Next by Date: Re: same input ... different results???
  • Previous by thread: Re: Help with Check Function Arguments & Options
  • Next by thread: What determines #1 and #2 values (with &) for ContourLabels?