3

Is it possible to create flags to pass arguments into a script with wolframscript? I am trying to pass multiple variables that correlate to specific keys.

For example,

wolframscript -script Notebook.wls -flag2 "True" -flag1 "False" -flag3 "True"

is the idea.

BOUNCE
  • 589
  • 2
  • 9
  • 3
    https://reference.wolfram.com/language/ref/$ScriptCommandLine.html – ilian May 31 '19 at 18:54
  • @ilian so in reference to that I can't truly make flags in the sense of a normal commandline, like -h? only parse through it with loops? – BOUNCE May 31 '19 at 19:05

1 Answers1

6

I suspect you have to write a script (in Unix/Linux/OSX style) that uses wolframscript as the shell in order to be able to process arbitrary flags from the command line.

Consider following script file which peels flag name-value pairs from arguments:

#!/usr/bin/env wolframscript -fun
(flag1 = flag2 = Missing;
  FixedPoint[Replace[
    {{"-flag1", val_, rest___} :> (flag1 = ToExpression@val; {rest}),
     {"-flag2", val_, rest___} :> (flag2 = ToExpression@val; {rest}),
     {flag_, _, rest___} /; StringMatchQ[flag, "-" ~~ __] :>
      (Print["Ignored: ", flag]; {rest})}], {##}];
  {flag1, flag2}) &

Effectively the whole program is expressed as a function which takes command line arguments as function arguments.

Now if you set execution permissions for this script, you can run it:

./scriptname -flag3 5 -flag2 10

Ignored: -flag3

{Missing, 10}

kirma
  • 19,056
  • 1
  • 51
  • 93