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.algorithm : map, maxElement;
28 
29     sink(title);
30     sink("\n");
31 
32     immutable longest = maxElement(options.map!(o => o.optLong.length + (o.optShort.length > 0
33             ? o.optShort.length + 1 : 0)));
34 
35     foreach (option; options)
36     {
37         auto lineSize = option.optLong.length;
38         sink(option.optLong);
39 
40         if (option.optShort.length > 0)
41         {
42             lineSize += option.optShort.length + 1;
43             sink("|");
44             sink(option.optShort);
45         }
46 
47         if (option.help.length > 0)
48         {
49             auto spaces = new char[longest + 1 - lineSize];
50             spaces[] = ' ';
51             sink(spaces);
52             sink(option.help);
53         }
54 
55         sink("\n");
56     }
57 }