escript, flags to script

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

escript, flags to script

Bengt Kleberg-2
Greetings,

How do I get escript to recognise Unix style command line flags?

Given my-script (far) below, started like this:
; my-script -a hello

I would like an output of:
cmd-line-args -a hello

but I get:
cmd-line-args []


my-script:

#!/usr/bin/env escript

main(X) ->
                io:format("cmd-line-args ~p~n",[X]),
                erlang:halt().


% Bengt Kleberg




Reply | Threaded
Open this post in threaded view
|

escript, flags to script

Luke Gorrie-3
Bengt Kleberg <qtxkleb> writes:

> Greetings,
>
> How do I get escript to recognise Unix style command line flags?

The problem is that 'erl' is interpreting the args. A possibly
solution would be to put a leading "escape" character on them so that
erl never sees anything starting with "-".

My quick hack is this. First a new script that escapes the args:

  #!/bin/sh
  function escape() {
    for arg in $*; do echo -n "\\${arg} "; done
    echo ""
  }

  erl -pa /home/luke/src/escript-1.0 -noshell -s escript go $0 $(escape $*)

Then the erlang module needs to strip the escape characters:

  go(X0) ->
      X = map(fun(I) ->
                      [_EscapeChar | Arg] = atom_to_list(I),
                      Arg
              end, X0),
      [_,F|Args] = X,
      ... etc

This works for me:

  $ cmd-line-args -foo bar -baz
  cmd-line-args ["-foo","bar","-baz"]



Reply | Threaded
Open this post in threaded view
|

escript, flags to script

Thierry Mallard-2
In reply to this post by Bengt Kleberg-2
On Fri, Mar 23, 2001 at 03:59:47PM +0100, Bengt Kleberg wrote:
> Greetings,
>
> How do I get escript to recognise Unix style command line flags?
>
> Given my-script (far) below, started like this:
> ; my-script -a hello

I think there is a bug in the parsing of the arguments. Not looked at the
sources yet, but it seems that if there is a "-" in an argument, [] is passed
instead :

tsm|~ > ./testing hello world
cmd-line-args ["hello","world"]

tsm|~ > ./testing hello -a  
cmd-line-args ["hello"]

tsm|~ > ./testing -a world  
cmd-line-args []

Still have to find out why...

--
Thierry Mallard              | http://vawis.net
GnuPG key on wwwkeys.pgp.net | http://erlang-fr.org (new)
key 0xA3D021CB               | http://worldforge.org



Reply | Threaded
Open this post in threaded view
|

escript, flags to script

Thierry Mallard
On Fri, Mar 23, 2001 at 04:43:54PM +0100, Thierry Mallard wrote:
> I think there is a bug in the parsing of the arguments. Not looked at the
> sources yet, but it seems that if there is a "-" in an argument, [] is passed
> instead [...]
> Still have to find out why...

Ok, I did some quick work with the help of Mickael Remond.

Here is the patch : we now parses the arguments, that are grouped in the shell
using " ... "

Hope this helps.

        Best regards,

--
Thierry Mallard              | http://vawis.net
GnuPG key on wwwkeys.pgp.net | http://erlang-fr.org (new)
key 0xA3D021CB               | http://worldforge.org
-------------- next part --------------
diff -urN escript-1.0/escript.erl escript-1.0.1/escript.erl
--- escript-1.0/escript.erl Fri Mar 23 14:58:01 2001
+++ escript-1.0.1/escript.erl Fri Mar 23 17:19:10 2001
@@ -12,8 +12,10 @@
 -import(lists, [map/2, member/2]).
 
 go(X0) ->
-    X = map(fun(I) -> atom_to_list(I) end, X0),
-    [_,F|Args] = X,
+    [X] = map(fun(I) -> atom_to_list(I) end, X0),
+    T = string:tokens(X, " "),   %% We separated into strings
+    [_|FArgs] = T,  %% We drop the program path
+    [F|Args] = FArgs,   %% Separata into script file path and args
     %% io:format("Escript:~p~n",[X]),
     {Nerrs, Dict} = parse_file(F),
     if
diff -urN escript-1.0/mk_escript.sh escript-1.0.1/mk_escript.sh
--- escript-1.0/mk_escript.sh Fri Mar 23 14:58:01 2001
+++ escript-1.0.1/mk_escript.sh Fri Mar 23 17:25:26 2001
@@ -2,6 +2,6 @@
 CWD=`pwd`
 echo $CWD
 echo "#!/bin/sh" > escript
-echo "erl -pa $CWD -noshell -s escript go \$0 \$*" >> escript
+echo "erl -pa $CWD -noshell -s escript go \"\$0 \$*\"" >> escript
 chmod u+x escript