use dirs; use fmt; use getopt; use os; use strings; fn abort_usage( title: str, help: []getopt::help, error: fmt::formattable... ) never = { if (len(error) > 0) { fmt::errorf("{}: ", title)!; fmt::errorln(error...)!; }; getopt::printusage(os::stdout, title, help)!; os::exit(1); }; export fn main() void = { const help: []getopt::help = [ "manage your Factorio mods", ('c', "file", "use the given configuration file"), ('d', "directory", "use the given mods directory"), "", "args..." ]; const cmd = getopt::parse(os::args, help...); defer getopt::finish(&cmd); if (len(cmd.args) == 0) { abort_usage(os::args[0], help); }; // Default config path let config_path = fmt::asprintf( "{}/{}", dirs::config("fmm"), "fmm.ini" )!; // User-specified config path for (let opt &.. cmd.opts) { if (opt.0 == 'c') { free(config_path); config_path = strings::dup(opt.1)!; break; }; }; defer free(config_path); // Base configuration from config file let config = config_parse(config_path); defer config_free(config); // Parse global CLI options for (let opt &.. cmd.opts) { switch (opt.0) { case 'd' => free(config.mods_dir); config.mods_dir = strings::dup(opt.1)!; case => void; }; }; let subcmd = cmd.args[0]; const task = switch (cmd.args[0]) { case "clean", "c" => yield &clean; case "disable", "d" => yield &disable; case "enable", "e" => yield &enable; case "install", "i" => yield &install; case "query", "q" => yield &query; case "sync", "s" => yield &sync; case "publish", "p" => yield &publish; case => abort_usage("fmm", help, "unrecognized subcommand:", subcmd); }; task(config, cmd.args); };