All Downloads are FREE. Search and download functionalities are using the official Maven repository.

su.nlq.prometheus.jmx.arguments.Arguments Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package su.nlq.prometheus.jmx.arguments;

import org.jetbrains.annotations.NotNull;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionHandlerFilter;
import su.nlq.prometheus.jmx.logging.Logger;

import java.io.StringWriter;
import java.util.Optional;

public enum Arguments {
  ;

  @SuppressWarnings("StaticMethodNamingConvention")
  public static @NotNull  Optional of(@NotNull String[] args, @NotNull T bean) {
    final CmdLineParser parser = new CmdLineParser(bean);
    try {
      parser.parseArgument(args);
      return Optional.of(bean);
    } catch (CmdLineException e) {
      Logger.instance.error(e.getMessage());
      final StringWriter writer = new StringWriter();
      parser.printUsage(writer, null);
      Logger.instance.info("Usage:\n" + writer.toString());
      final String example = parser.printExample(OptionHandlerFilter.ALL);
      if (!example.isEmpty()) {
        Logger.instance.info("Example: java -jar  " + example);
      }
    }
    return Optional.empty();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy