I have an arbitrary function with multiple arguments, and I want to call it from command-line like this:
program myfun 7 24 15
I tried to do it this way:
myfun Func: [A B C][...]
ARGS: Probe System/Options/Args
Print Do [ARGS/1 Remove ARGS]
My way of thinking is that I retrieve command name with ARGS/1, then remove it from argument list, enclose in a block with the rest of arguments and evaluate with Do. However, it just returns arguments without the function name: 7 24 15. I also tried to copy both values to different variables, but no luck, and I cannot understand what is wrong.
The main issue is that the last line does not do what you think it should:
will return:
instead of calling
myfunfunction with those arguments. That happens becausedo [args/1]is not equivalent todo args/1:As you can see, in the first case,
dois evaluating a block and returning the last value, soArgs/1is evaluated to"myfun"and returned bydo. In the second case,Args/1is evaluated to"myfun", then"myfun"is passed as argument todo, resulting indoloading that value and evaluating it (resulting in an error in my case, as I did not define that word).Using
printimplies an extra level of evaluation (asprintreduces its block of arguments), so you can easily get fooled by the reduced output. If you useprobeinstead (recommended for debugging), you would get["7" "24" "15"].Now to achieve what you want, you can either do it yourself, step by step or use an existing library.
Directly coding the solution
Here is an example code:
This approach relies on
applywhich will call a function (passed as word or function value) with a list of arguments (passed as a block of values). So:to-word args/1fetches and converts the function's name to the right type.next argscreates the list of arguments by skipping the function's name at first position in the block (no needs to remove it, just skipping is simpler).Using a library
The CLI library is doing what you need (and a lot more). Feel free to join us in our Chat room if you're not there already and ask @hiiamboris if you need any info about his CLI library.
Hope this helps.