1+ // SPDX-FileCopyrightText : © 2025 TU Wien <vadl@tuwien.ac.at>
2+ // SPDX-License-Identifier: GPL-3.0-or-later
3+ //
4+ // This program is free software: you can redistribute it and/or modify
5+ // it under the terms of the GNU General Public License as published by
6+ // the Free Software Foundation, either version 3 of the License, or
7+ // (at your option) any later version.
8+ //
9+ // This program is distributed in the hope that it will be useful,
10+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ // GNU General Public License for more details.
13+ //
14+ // You should have received a copy of the GNU General Public License
15+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+ package vadl .cli ;
18+
19+ import java .util .Arrays ;
20+ import java .util .Comparator ;
21+ import java .util .Iterator ;
22+ import java .util .stream .Collectors ;
23+ import javax .annotation .Nonnull ;
24+ import org .apache .commons .lang3 .stream .Streams ;
25+ import picocli .CommandLine ;
26+ import vadl .configuration .IssConfiguration ;
27+
28+ class Helpers {
29+ }
30+
31+
32+ class IssOptsConverter implements CommandLine .ITypeConverter <IssConfiguration .IssOptsToSkip >,
33+ Iterable <String > {
34+
35+ @ Override
36+ public IssConfiguration .IssOptsToSkip convert (String value ) {
37+ if (value .equals ("help" )) {
38+ // Calculate the maximum width of option names
39+ int maxOptionLength = Arrays .stream (IssConfiguration .IssOptsToSkip .values ())
40+ .map (opt -> toCliName (opt ).length ())
41+ .max (Integer ::compare )
42+ .orElse (0 );
43+
44+ // Define the indentation for descriptions
45+ int descriptionIndent = 6 + maxOptionLength ; // 6 accounts for " - " and two spaces
46+
47+ // Print help message for available options
48+ System .out .println ("Available optimizations to skip:" );
49+ Arrays .stream (IssConfiguration .IssOptsToSkip .values ())
50+ .sorted (Comparator .comparing (v -> v .name ()))
51+ .forEach (opt ->
52+ printFormattedOption (toCliName (opt ), opt .desc , maxOptionLength ,
53+ descriptionIndent ));
54+
55+ System .exit (0 );
56+ }
57+
58+ try {
59+ return IssConfiguration .IssOptsToSkip .valueOf (value .toUpperCase ().replace ('-' , '_' ));
60+ } catch (IllegalArgumentException e ) {
61+ throw new CommandLine .TypeConversionException (
62+ "\n Available options are %s" .formatted (
63+ Streams .of (iterator ()).sorted ().collect (Collectors .joining (", " ))
64+ )
65+ );
66+ }
67+ }
68+
69+ @ Nonnull
70+ @ Override
71+ public Iterator <String > iterator () {
72+ return Arrays .stream (IssConfiguration .IssOptsToSkip .values ())
73+ .map (this ::toCliName )
74+ .sorted ()
75+ .iterator ();
76+ }
77+
78+ private String toCliName (IssConfiguration .IssOptsToSkip value ) {
79+ return value .name ().toLowerCase ().replace ('_' , '-' );
80+ }
81+
82+ private static void printFormattedOption (String optionName , String description , int nameWidth ,
83+ int descriptionIndent ) {
84+ // Split the description into lines
85+ String [] lines = description .split ("\n " , -1 );
86+
87+ // Print the first line with the option name
88+ System .out .printf (" - %-" + nameWidth + "s %s%n" , optionName , lines [0 ]);
89+
90+ // Print subsequent lines with indentation
91+ String format = "%" + (descriptionIndent + 2 ) + "s%s%n" ;
92+ for (int i = 1 ; i < lines .length ; i ++) {
93+ System .out .printf (format , "" , lines [i ]);
94+ }
95+ }
96+ }
0 commit comments