views:

620

answers:

1

How does one access command line flag (arguments) as environment variables in Erlang. (As flags, not ARGV) For example:

RabbitMQ cli looks something like:

erl \
...
-sasl errlog_type error \
-sasl sasl_error_logger '{file,"'${RABBITMQ_SASL_LOGS}'"}' \
... # more stuff here

If one looks at sasl.erl you see the line:

get_sasl_error_logger() ->
   case application:get_env(sasl, sasl_error_logger) of
% ... etc

By some unknown magic the sasl_error_logger variable becomes an erlang tuple! I've tried replicating this in my own erlang application, but I seem to be only able to access these values via init:get_argument, which returns the value as a string.

How does one pass in values via the commandline and be able to access them easily as erlang terms?

UPDATE Also for anyone looking, to use environment variables in the 'regular' way use os:getenv("THE_VAR")

+5  A: 

Make sure you set up an application configuration file

{application, fred,
 [{description, "Your application"},
  {vsn, "1.0"},
  {modules, []},
  {registered,[]},
  {applications, [kernel,stdlib]},
  {env, [
    {param, 'fred'}
        ]
...

and then you can set your command line up like this:

-fred param 'billy'

I think you need to have the parameter in your application configuration to do this - I've never done it any other way...

Some more info (easier than putting it in a comment)

Given this

{emxconfig, {ets, [{keypos, 2}]}},

I can certainly do this:

   {ok, {StorageType, Config}} = application:get_env(emxconfig),

but (and this may be important) my application is started at this time (may actually just need to be loaded and not actually started from looking at the application_controller code).

Alan Moore
Alan, thanks for the tip. I do have the param in my `.app` file. Good news: I get the default value specified in the `.app` file. Bad news: I can't seem to override it on the command line.
Nate Murray
Nate, could you show us your app file and the command you start erlang with?
Zed
Yes - it should work so there's probably something fishy in your command line. Have you started your app by the time you access the environment?
Alan Moore