diff --git a/esv.d b/esv.d index ee67571..c1b8bc1 100644 --- a/esv.d +++ b/esv.d @@ -20,7 +20,6 @@ module esv; -import std.algorithm : startsWith; import std.conv : to, ConvException; import std.file : exists, mkdirRecurse, write, FileException; import std.format : format; @@ -76,14 +75,7 @@ main(string[] args) "V", &VFlag, ); } catch (GetOptException e) { - string opt = extractFlag(e); - - enforceDie(!e.msg.startsWith("Unrecognized option"), - "unknown option " ~ opt); - enforceDie(!e.msg.startsWith("Missing value for argument"), - "missing argument for option " ~ opt); - - die(e.msg); /* catch-all */ + handleOptError(e.msg); } if (VFlag) { diff --git a/util.d b/util.d index 826fff7..044e2c8 100644 --- a/util.d +++ b/util.d @@ -1,7 +1,6 @@ module util; -import std.getopt : GetOptException; -import std.stdio : File; +import std.stdio : File; public @safe: @@ -42,12 +41,20 @@ enforceDie(A...)(bool cond, string fmt, A a) die(format(fmt, a)); } -string -extractFlag(in GetOptException e) +void +handleOptError(in string msg) { + import std.algorithm : startsWith; import std.regex : matchFirst; - return e.msg.matchFirst("-.")[0]; + string opt = msg.matchFirst("-.")[0]; + + enforceDie(!msg.startsWith("Unrecognized option"), + "unknown option " ~ opt); + enforceDie(!msg.startsWith("Missing value for argument"), + "missing argument for option " ~ opt); + + die(msg); /* catch-all */ } string