Martin Kristiansen
Martin Kristiansen

Reputation: 10222

file:consult without the file part

I'm writing an escript that needs as its input a normal proplist :

script "[{error_string, \"This is broken\"}]"

all I wanna do now is parse arbitrary strings into actual proplists, normally I would write this to a file, and then go on to use file:consult on the file to get the values - that does however seem a little over the top - so my question is, how do I do the same, that is parse the input string into a proplist without sending the the data to a file?

Upvotes: 3

Views: 517

Answers (2)

mpm
mpm

Reputation: 3584

Easiest way to do it is to use erl_eval, just like in Odobenus Rosmarus answer, even if it seems little complicated. Just have to remember to put . at the end of your expression. You can read a little more on this topic on Erlang Central Wiki.

But if you are building escript to populate your application with options i would look into full-blown argument parser. One I've used before is https://github.com/jcomellas/getopt . Project is stable, with good enough documentation, and does exactly what is should. It requires little bit configuration, but created code stays readable.

Upvotes: 1

Odobenus Rosmarus
Odobenus Rosmarus

Reputation: 5998

M1 = "[{error_string, \"This is broken\"}].",
{ok, S1, _} = erl_scan:string(M1),
erl_parse:parse_term(S1).

returns

{ok,[{error_string,"This is broken"}]}

Upvotes: 4

Related Questions