vibe.core.args 24/69(34%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
5010
510
529
5327
549
559
5645
579
580
599
609
619
620
639
640
650
660
670
680
690
700
719
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2101
2110
2120
2130
2140
2151
2161
2170
2180
2190
2200
2210
2220
2230
2240
2253
2260
2271
2280
2290
2301
23112
2323
2333
2340
2350
2360
2370
2380
2390
2400
2410
2421
2431
2440
2451
2460
2470
2480
2490
2500
2510
2520
/** Parses and allows querying the command line arguments and configuration file. The optional configuration file (vibe.conf) is a JSON file, containing an object with the keys corresponding to option names, and values corresponding to their values. It is searched for in the local directory, user's home directory, or /etc/vibe/ (POSIX only), whichever is found first. Copyright: © 2012 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig, Vladimir Panteleev */ module vibe.core.args; import vibe.core.log; import vibe.data.json; import std.algorithm : any, filter, map, sort, startsWith; import std.array : array, join, replicate, split; import std.exception; import std.file; import std.getopt; import std.path : buildPath; import std.string : format, stripRight, wrap; import core.runtime; /** Finds and reads an option from the configuration file or command line. Command line options take precedence over configuration file entries. Params: names = Option names. Separate multiple name variants with "|", as for $(D std.getopt). pvalue = Pointer to store the value. Unchanged if value was not found. help_text = Text to be displayed when the application is run with --help. Returns: $(D true) if the value was found, $(D false) otherwise. See_Also: readRequiredOption */ bool readOption(T)(string names, T* pvalue, string help_text) { // May happen due to http://d.puremagic.com/issues/show_bug.cgi?id=9881 if (g_args is null) init(); OptionInfo info; info.names = names.split("|").sort!((a, b) => a.length < b.length)().array(); info.hasValue = !is(T == bool); info.helpText = help_text; assert(!g_options.any!(o => o.names == info.names)(), "readOption() may only be called once per option name."); g_options ~= info; immutable olen = g_args.length; getopt(g_args, getoptConfig, names, pvalue); if (g_args.length < olen) return true; if (g_haveConfig) { foreach (name; info.names) if (auto pv = name in g_config) { *pvalue = deserializeJson!T(*pv); return true; } } return false; } /** The same as readOption, but throws an exception if the given option is missing. See_Also: readOption */ T readRequiredOption(T)(string names, string help_text) { string formattedNames() { return names.split("|").map!(s => s.length == 1 ? "-" ~ s : "--" ~ s).join("/"); } T ret; enforce(readOption(names, &ret, help_text) || g_help, format("Missing mandatory option %s.", formattedNames())); return ret; } /** Prints a help screen consisting of all options encountered in getOption calls. */ void printCommandLineHelp() { enum dcolumn = 20; enum ncolumns = 80; logInfo("Usage: %s <options>\n", g_args[0]); foreach (opt; g_options) { string shortopt; string[] longopts; if (opt.names[0].length == 1 && !opt.hasValue) { shortopt = "-"~opt.names[0]; longopts = opt.names[1 .. $]; } else { shortopt = " "; longopts = opt.names; } string optionString(string name) { if (name.length == 1) return "-"~name~(opt.hasValue ? " <value>" : ""); else return "--"~name~(opt.hasValue ? "=<value>" : ""); } string[] lopts; foreach(lo; longopts) lopts ~= optionString(lo); auto optstr = format(" %s %s", shortopt, lopts.join(", ")); if (optstr.length < dcolumn) optstr ~= replicate(" ", dcolumn - optstr.length); auto indent = replicate(" ", dcolumn+1); auto desc = wrap(opt.helpText, ncolumns - dcolumn - 2, optstr.length > dcolumn ? indent : "", indent).stripRight(); if (optstr.length > dcolumn) logInfo("%s\n%s", optstr, desc); else logInfo("%s %s", optstr, desc); } } /** Checks for unrecognized command line options and display a help screen. This function is called automatically from `vibe.appmain` and from `vibe.core.core.runApplication` to check for correct command line usage. It will print a help screen in case of unrecognized options. Params: args_out = Optional parameter for storing any arguments not handled by any readOption call. If this is left to null, an error will be triggered whenever unhandled arguments exist. Returns: If "--help" was passed, the function returns false. In all other cases either true is returned or an exception is thrown. */ bool finalizeCommandLineOptions(string[]* args_out = null) { scope(exit) g_args = null; if (args_out) { *args_out = g_args; } else if (g_args.length > 1) { logError("Unrecognized command line option: %s\n", g_args[1]); printCommandLineHelp(); throw new Exception("Unrecognized command line option."); } if (g_help) { printCommandLineHelp(); return false; } return true; } /** This functions allows the usage of a custom command line argument parser with vibe.d. $(OL $(LI build executable with version(VibeDisableCommandLineParsing)) $(LI parse main function arguments with a custom command line parser) $(LI pass vibe.d arguments to `setCommandLineArgs`) $(LI use vibe.d command line parsing utilities) ) Params: args = The arguments that should be handled by vibe.d */ void setCommandLineArgs(string[] args) { g_args = args; } /// unittest { import std.format : format; string[] args = ["--foo", "10"]; setCommandLineArgs(args); } private struct OptionInfo { string[] names; bool hasValue; string helpText; } private { __gshared string[] g_args; __gshared bool g_haveConfig; __gshared Json g_config; __gshared OptionInfo[] g_options; __gshared bool g_help; } private string[] getConfigPaths() { string[] result = [""]; import std.process : environment; version (Windows) result ~= environment.get("USERPROFILE"); else result ~= [environment.get("HOME"), "/etc/vibe/"]; return result; } // this is invoked by the first readOption call (at least vibe.core will perform one) private void init() { import vibe.utils.string : stripUTF8Bom; version (VibeDisableCommandLineParsing) {} else g_args = Runtime.args.filter!(arg => !arg.startsWith("--DRT-")).array; if (!g_args.length) g_args = ["dummy"]; // TODO: let different config files override individual fields auto searchpaths = getConfigPaths(); foreach (spath; searchpaths) { auto cpath = buildPath(spath, configName); if (cpath.exists) { scope(failure) logError("Failed to parse config file %s.", cpath); auto text = stripUTF8Bom(cpath.readText()); g_config = text.parseJson(); g_haveConfig = true; break; } } if (!g_haveConfig) logDiagnostic("No config file found in %s", searchpaths); readOption("h|help", &g_help, "Prints this help screen."); } private enum configName = "vibe.conf"; private template ValueTuple(T...) { alias ValueTuple = T; } private alias getoptConfig = ValueTuple!(std.getopt.config.passThrough, std.getopt.config.bundling);