2

I write a lot of scripts in Mathematica and I'd like to eliminate the boilerplate that parses command-line arguments and assigns them to variables of the correct type before doing the real work. I was thinking of accomplishing the following way.

  1. Find a function called main.

  2. Look at the list of its parameters [a_Integer, b_Real, c_String].

  3. Attempt to convert the elements of $ScriptCommandLine into those parameters.

  4. Call main with those parameters.

This way, each script can simply contain a function main that will be called with the appropriate parameters from a bootstrap script.

I'm trying to figure out how to accomplish #2. I've been trying to use Definition, but I can't figure out how to parse the output. Is there a better way?

Ashley
  • 269
  • 1
  • 6
  • How do you plan to handle functions with multiple or complex patterns? E.g. f[x_,y_]:=x+y; f[x_]:=x; or g[x_,a_:1]:= a x or h[n_?OddQ]:=n+1; h[n_?EvenQ]:=n? It might be better and clearer to just specify the type list separately from a function definition. – Szabolcs May 01 '13 at 15:45
  • I wanted to take care of the simple case for now. – Ashley May 01 '13 at 16:40
  • You may find this and this answers useful (the latter only the last part), in both I construct various argument parsers. – Leonid Shifrin May 01 '13 at 21:34

1 Answers1

0

Found one way to do this. If f is the function you're interested in, the following will give you a list of lists of argument types, so long as the definitions are of the form f[firstParam_Integer, secondParam_Real, ...].

Cases[DownValues[f], RuleDelayed[func_, _] :> Level[func, {4}]]

Ashley
  • 269
  • 1
  • 6