3232import java .io .PrintWriter ;
3333import java .util .HashMap ;
3434import java .util .Map ;
35+ import java .util .Properties ;
3536import java .util .function .Consumer ;
3637
3738import org .apache .commons .cli .CommandLine ;
@@ -72,6 +73,7 @@ public class CommandLineParser {
7273
7374 // "special", i.e. non-configuration options
7475 private static final Option OPT_HELP = Option .builder ("h" ).longOpt ("help" ).hasArg (false ).desc ("shows this help" ).build ();
76+ private static final Option OPT_VERSION = Option .builder ().longOpt ("version" ).hasArg (false ).desc ("prints the version and exits" ).build ();
7577
7678 // input-specific options
7779 private static final Option OPT_INPUT = Option .builder ("i" ).longOpt ("input" ).hasArg (true ).argName ("file" ).type (FileInputStream .class )
@@ -158,6 +160,7 @@ public class CommandLineParser {
158160 * Below code adds all options defined above to CLI_OPTS - needed for parsing
159161 */
160162 CommandLineParser .CLI_OPTS .addOption (CommandLineParser .OPT_HELP );
163+ CommandLineParser .CLI_OPTS .addOption (CommandLineParser .OPT_VERSION );
161164
162165 CommandLineParser .CLI_OPTS .addOption (CommandLineParser .OPT_NUM_ANSWER_SETS );
163166 CommandLineParser .CLI_OPTS .addOption (CommandLineParser .OPT_FILTER );
@@ -223,8 +226,10 @@ private void initializeGlobalOptionHandlers() {
223226 /*
224227 * below put invocations are used to "register" the handler methods for each commandline option
225228 */
226- // help is handled separately, therefore dummy handler
229+ // help and version are handled separately, therefore dummy handlers
227230 this .globalOptionHandlers .put (CommandLineParser .OPT_HELP .getOpt (), (o , c ) -> { });
231+ this .globalOptionHandlers .put (CommandLineParser .OPT_VERSION .getOpt (), (o , c ) -> { });
232+
228233 this .globalOptionHandlers .put (CommandLineParser .OPT_GROUNDER .getOpt (), this ::handleGrounder );
229234 this .globalOptionHandlers .put (CommandLineParser .OPT_SOLVER .getOpt (), this ::handleSolver );
230235 this .globalOptionHandlers .put (CommandLineParser .OPT_NOGOOD_STORE .getOpt (), this ::handleNogoodStore );
@@ -269,6 +274,9 @@ public AlphaConfig parseCommandLine(String[] args) throws ParseException {
269274 if (commandLine .hasOption (CommandLineParser .OPT_HELP .getOpt ())) {
270275 LOGGER .debug ("Found help option!" );
271276 this .handleHelp ();
277+ } else if (commandLine .hasOption (CommandLineParser .OPT_VERSION .getLongOpt ())) {
278+ LOGGER .debug ("Found version option!" );
279+ this .handleVersion ();
272280 } else {
273281 this .validate (commandLine );
274282 }
@@ -308,6 +316,16 @@ public String getUsageMessage() {
308316 return helpBuffer .toString ();
309317 }
310318
319+ public String getVersion () {
320+ try {
321+ Properties properties = new Properties ();
322+ properties .load (CommandLineParser .class .getResourceAsStream ("/META-INF/build.properties" ));
323+ return properties .getProperty ("project.version" );
324+ } catch (Exception e ) {
325+ return "unknown (" + e .getMessage () + ")" ;
326+ }
327+ }
328+
311329 private void validate (CommandLine commandLine ) throws ParseException {
312330 if (!commandLine .hasOption (CommandLineParser .OPT_INPUT .getOpt ()) && !commandLine .hasOption (CommandLineParser .OPT_ASPSTRING .getOpt ())) {
313331 throw new ParseException ("Missing input source - need to specifiy either a file (" + CommandLineParser .OPT_INPUT .getOpt () + ") or a string ("
@@ -330,6 +348,10 @@ private void handleHelp() {
330348 this .abortAction .accept (this .getUsageMessage ());
331349 }
332350
351+ private void handleVersion () {
352+ this .abortAction .accept (this .getVersion ());
353+ }
354+
333355 private void handleInput (Option opt , InputConfig cfg ) {
334356 String optVal = opt .getValue ().trim ();
335357 cfg .getFiles ().add (optVal );
0 commit comments