Comparison

util/startup.lua @ 10596:cb107ea49b35 0.11

util.startup: Add startup step for parsing command-line options
author Matthew Wild <mwild1@gmail.com>
date Sun, 19 Jan 2020 15:26:22 +0000
parent 10390:82705ec87253
child 10597:25a3c8134b0a
comparison
equal deleted inserted replaced
10581:10d6d0d91f4e 10596:cb107ea49b35
9 local config = require "core.configmanager"; 9 local config = require "core.configmanager";
10 10
11 local dependencies = require "util.dependencies"; 11 local dependencies = require "util.dependencies";
12 12
13 local original_logging_config; 13 local original_logging_config;
14
15 local short_params = { D = "daemonize", F = "no-daemonize" };
16 local value_params = { config = true };
17
18 function startup.parse_args()
19 local parsed_opts = {};
20
21 if #arg > 0 and arg[1] ~= "--config" then
22 while true do
23 local raw_param = arg[1];
24 if not raw_param then
25 break;
26 end
27
28 local prefix = raw_param:match("^%-%-?");
29 if not prefix then
30 break;
31 elseif prefix == "--" and raw_param == "--" then
32 table.remove(arg, 1);
33 break;
34 end
35 local param = table.remove(arg, 1):sub(#prefix+1);
36 if #param == 1 then
37 param = short_params[param];
38 end
39
40 if not param then
41 print("Unknown command-line option: "..tostring(param));
42 print("Perhaps you meant to use prosodyctl instead?");
43 os.exit(1);
44 end
45
46 local param_k, param_v;
47 if value_params[param] then
48 param_k, param_v = param, table.remove(arg, 1);
49 if not param_v then
50 print("Expected a value to follow command-line option: "..raw_param);
51 os.exit(1);
52 end
53 else
54 param_k, param_v = param:match("^([^=]+)=(.+)$");
55 if not param_k then
56 if param:match("^no%-") then
57 param_k, param_v = param:sub(4), false;
58 else
59 param_k, param_v = param, true;
60 end
61 end
62 end
63 parsed_opts[param_k] = param_v;
64 end
65 end
66 prosody.opts = parsed_opts;
67 end
14 68
15 function startup.read_config() 69 function startup.read_config()
16 local filenames = {}; 70 local filenames = {};
17 71
18 local filename; 72 local filename;