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?

  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented May 1, 2013 at 15:45
  • $\begingroup$ I wanted to take care of the simple case for now. $\endgroup$
    – Ashley
    Commented May 1, 2013 at 16:40
  • $\begingroup$ You may find this and this answers useful (the latter only the last part), in both I construct various argument parsers. $\endgroup$ Commented May 1, 2013 at 21:34

1 Answer 1


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}]]


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.