Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang: How to access CLI flags (arguments) as application environment variables?

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")

like image 632
Nate Murray Avatar asked Jul 31 '09 15:07

Nate Murray


1 Answers

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).

like image 72
Alan Moore Avatar answered Oct 31 '22 15:10

Alan Moore