I would like to pass input parameters into my .mp script. For example, instead of running:

mpost script.mp

I want to run something like:

mpost script.mp 1 1 2 diamand

Where "1 1 2 diamond" are 4 input parameters telling the script what to do.

At the moment I have things like:

numeric showindices
showindices := 0;
%showindices := 1;

string drawingmode
drawingmode := "diamond";
%drawingmode := "square";

I am then running "mpost script.mp" with showindices=0 un-commented, and then separately with showindices=1 un-commented. Likewise I'm running it with drawingmode=diamond un-commented, and then separately with drawingmode=square un-commented.

There's 10 drawing modes and various "switches" similar to showindices, so I don't want to just keep un-commenting and re-commenting different lines. I also don't want to copy and paste the whole script so many times. Perhaps you'll say I can run through all these combinations in a loop, but is there a way to just pass in arguments from the command line, if I wanted for example, "square" drawingmode and "1" for showindices ?

Note: This quesiton is not a duplicate of: Passing variable as argument
Note: This quesiton is also not a duplicate of: Input Command and Declaring Variables

  • 1
    Computers usually don't guess at the intentions of the user. You need to make a shell script that does the assignments behind the scenes and knows what parameters the value should be assigned to. Then it will build a command line such as mpost "numeric showindices;showindices:=$2;string drawingmode; drawingmode=$5; input $1" and run it. – egreg Dec 5 '18 at 8:48
  • @egreg: Sounds interesting, but I don't really understand what you are saying. – user1271772 Dec 5 '18 at 19:08
  • The questions you are saying are not duplicates don't have anything to do with MetaPost, so there is no way they could be duplicates at all. – Henri Menke Dec 10 '18 at 0:47
  • 1
    @egreg Please write an answer. – Henri Menke Dec 26 '18 at 0:49
  • @HenriMenke Done – egreg Dec 26 '18 at 10:47

You can call mpost with the command line

mpost '\numeric showindices;showindices:=1;string drawingmode;drawingmode:="diamond";input script'

The initial backslash is required in order that mpost doesn't interpret numeric as a file name.

This can become complicated with more than two parameters, so you could write down a shell script such as

#!/usr/bin/env sh
mpost "\\ \
numeric showindices; showindices:=$2; \
string drawingmode; drawingmode:="$3"; \
input $1"

Save this as the file runscript and call it from the command line like

sh runscript script.mp 1 \"diamond\"

The shell will split the input after sh runscript at words, assigning them the variables 1, 2, 3 and so on. Note how quotes should be used in order to pass “real” quotes to the script.

If you make the file runscript executable, you can also call it like

./runscript script.mp 1 \"diamond\"

and you can save runscript in a place where the shell looks for executable programs or scripts, then calling it as

runscript script.mp 1 \"diamond\"
  • thank you. Please let me go through it properly and try it out. I may not be able to until January because my office is closed for the holiday season. – user1271772 Dec 27 '18 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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