1 /*
2  *Copyright (C) 2018 Laurent Tréguier
3  *
4  *This file is part of DLS.
5  *
6  *DLS is free software: you can redistribute it and/or modify
7  *it under the terms of the GNU General Public License as published by
8  *the Free Software Foundation, either version 3 of the License, or
9  *(at your option) any later version.
10  *
11  *DLS is distributed in the hope that it will be useful,
12  *but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *GNU General Public License for more details.
15  *
16  *You should have received a copy of the GNU General Public License
17  *along with DLS.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 module dls.util.getopt;
22 
23 import std.getopt : Option;
24 
25 void printHelp(const char[] title, const Option[] options, void delegate(const char[]) sink)
26 {
27     import std.ascii : newline;
28     import std.algorithm : map, maxElement;
29 
30     sink(title);
31     sink(newline);
32 
33     immutable longest = maxElement(options.map!(o => o.optLong.length + (o.optShort.length > 0
34             ? o.optShort.length + 1 : 0)));
35 
36     foreach (option; options)
37     {
38         auto lineSize = option.optLong.length;
39         sink(option.optLong);
40 
41         if (option.optShort.length > 0)
42         {
43             lineSize += option.optShort.length + 1;
44             sink("|");
45             sink(option.optShort);
46         }
47 
48         if (option.help.length > 0)
49         {
50             auto spaces = new char[longest + 1 - lineSize];
51             spaces[] = ' ';
52             sink(spaces);
53             sink(option.help);
54         }
55 
56         sink(newline);
57     }
58 }