java.lang.Object
eu.svjatoslav.commons.cli_helper.parameter_parser.Parser

public class Parser extends Object
This class parses command-line arguments against a set of registered Option objects.

Usage:


   Parser parser = new Parser();
   FileOption fileOpt = parser.add(new FileOption("Input file").mustExist())
                              .addAliases("-f", "--file")
                              .setMandatory();
   parser.parse(args);
   if (!parser.checkSuccess()) {
       parser.showHelp();
       System.exit(1);
   }
   File inputFile = fileOpt.getValue();
 
  • Constructor Details

    • Parser

      public Parser()
  • Method Details

    • add

      public <E extends Option<?, E>> E add(E option)
      Registers an option with this parser.
      Type Parameters:
      E - the concrete type of the option
      Parameters:
      option - the option to be added
      Returns:
      the same option object, for chaining
    • findParameterByAlias

      public Option<?,?> findParameterByAlias(String alias)
      Looks up an option by alias.
      Parameters:
      alias - the alias to search for (e.g., "-f" or "--file")
      Returns:
      the matching option, or null if not found
    • parse

      public boolean parse(String[] args)
      Parses the provided command-line arguments, matching them to registered options and their parameters.
      Parameters:
      args - command-line arguments (usually from main(String[]))
      Returns:
      true if parsing succeeded without errors; otherwise false.
    • checkSuccess

      public boolean checkSuccess()
      A convenience method to see if the last parse succeeded. If you stored the result of parse(String[]) in a boolean, you can also just check that.
      Returns:
      true if all mandatory options are present (and no parse errors occurred).
    • showHelp

      public void showHelp()
      Prints all available options, their formats, and their descriptions. Usually called upon parse failure.