Comparison

util/startup.lua @ 10651:1196f1e8d178

util.startup: Break out command line argument parsing into util.argparse This will allow using it from other places such as prosodyctl sub-commands and plugins
author Kim Alvefur <zash@zash.se>
date Wed, 19 Feb 2020 21:38:00 +0100
parent 10636:a9c975a0f113
child 10934:b4daa697a7ea
comparison
equal deleted inserted replaced
10650:324a0c7d1c6a 10651:1196f1e8d178
3 local startup = {}; 3 local startup = {};
4 4
5 local prosody = { events = require "util.events".new() }; 5 local prosody = { events = require "util.events".new() };
6 local logger = require "util.logger"; 6 local logger = require "util.logger";
7 local log = logger.init("startup"); 7 local log = logger.init("startup");
8 local parse_args = require "util.argparse".parse;
8 9
9 local config = require "core.configmanager"; 10 local config = require "core.configmanager";
10 local config_warnings; 11 local config_warnings;
11 12
12 local dependencies = require "util.dependencies"; 13 local dependencies = require "util.dependencies";
15 16
16 local short_params = { D = "daemonize", F = "no-daemonize" }; 17 local short_params = { D = "daemonize", F = "no-daemonize" };
17 local value_params = { config = true }; 18 local value_params = { config = true };
18 19
19 function startup.parse_args() 20 function startup.parse_args()
20 local parsed_opts = {}; 21 prosody.opts = parse_args(arg, {
21 prosody.opts = parsed_opts; 22 short_params = short_params,
22 23 value_params = value_params,
23 if #arg == 0 then 24 });
24 return;
25 end
26 while true do
27 local raw_param = arg[1];
28 if not raw_param then
29 break;
30 end
31
32 local prefix = raw_param:match("^%-%-?");
33 if not prefix then
34 break;
35 elseif prefix == "--" and raw_param == "--" then
36 table.remove(arg, 1);
37 break;
38 end
39 local param = table.remove(arg, 1):sub(#prefix+1);
40 if #param == 1 then
41 param = short_params[param];
42 end
43
44 if not param then
45 print("Unknown command-line option: "..tostring(param));
46 print("Perhaps you meant to use prosodyctl instead?");
47 os.exit(1);
48 end
49
50 local param_k, param_v;
51 if value_params[param] then
52 param_k, param_v = param, table.remove(arg, 1);
53 if not param_v then
54 print("Expected a value to follow command-line option: "..raw_param);
55 os.exit(1);
56 end
57 else
58 param_k, param_v = param:match("^([^=]+)=(.+)$");
59 if not param_k then
60 if param:match("^no%-") then
61 param_k, param_v = param:sub(4), false;
62 else
63 param_k, param_v = param, true;
64 end
65 end
66 end
67 parsed_opts[param_k] = param_v;
68 end
69 end 25 end
70 26
71 function startup.read_config() 27 function startup.read_config()
72 local filenames = {}; 28 local filenames = {};
73 29