Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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 1 '13 at 15:45
    
I wanted to take care of the simple case for now. –  Ashley May 1 '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 1 '13 at 21:34

1 Answer 1

up vote 0 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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