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

How to access the command line flag (arguments) as environment variables in Erlang. (Like flags, not ARGV) For example:

RabbitMQ cli looks something like this:

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

If you look at sasl.erl , you will see the line:

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

By unknown magic, the sasl_error_logger variable becomes the root of erlang! I tried to replicate this in my own erlang application , but it seems to me that I can access these values โ€‹โ€‹through init:get_argument , which returns the value as a string.

How to pass values โ€‹โ€‹through the command line and access them easily as erlang terms?

UPDATE Also for those who are looking to use environment variables in "normal" mode, use os:getenv("THE_VAR")

+7
command-line erlang arguments environment-variables
source share
1 answer

Make sure you configure the 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 as follows:

 -fred param 'billy' 

I think you need to have a parameter in your application configuration in order to do this - I never did it differently ...

Additional information (easier than tagging it in a comment)

Considering this

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

I can do it:

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

but (and this may be important) my application starts at this time (in fact, it may just be necessary to download and not actually start by looking at the application_controller code).

+7
source share

All Articles