Java 类com.beust.jcommander.ParameterException 实例源码

项目:GitHub    文件:Arguments.java   
/**
 * Parses command line arguments and populates this command line instance.
 * <p>
 * If the command line arguments include the "help" argument, or if the
 * arguments have incorrect values or order, then usage information is
 * printed to {@link System#out} and the program terminates.
 *
 * @param args
 *            the command line arguments
 * @return an instance of the parsed arguments object
 */
public Arguments parse(String[] args) {

    JCommander jCommander = new JCommander(this);
    jCommander.setProgramName("jsonschema2pojo");

    try {
        jCommander.parse(args);

        if (this.showHelp) {
            jCommander.usage();
            exit(EXIT_OKAY);
        }
    } catch (ParameterException e) {
        System.err.println(e.getMessage());
        jCommander.usage();
        exit(EXIT_ERROR);
    }

    return this;
}
项目:hrrs    文件:WritableFileValidator.java   
@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public void validate(String name, String value) throws ParameterException {

    boolean invalid = false;
    File file = new File(value);

    if (file.exists()) {
        invalid = !file.canWrite() || !file.isFile();
    } else {
        try {
            file.createNewFile();
            file.delete();
        } catch (IOException error) {
            invalid = true;
        }
    }

    if (invalid) {
        String message = String.format("expecting a writable file: %s %s", name, value);
        throw new ParameterException(message);
    }

}
项目:ttc2017smartGrids    文件:JDK6Console.java   
public char[] readPassword(boolean echoInput) {
  try {
    writer.flush();
    Method method;
    if (echoInput) {
        method = console.getClass().getDeclaredMethod("readLine");
        return ((String) method.invoke(console)).toCharArray();
    } else {
        method = console.getClass().getDeclaredMethod("readPassword");
        return (char[]) method.invoke(console);
    }
  }
  catch (Exception e) {
    throw new ParameterException(e);
  }
}
项目:ttc2017smartGrids    文件:App.java   
public static void main(String[] args) {

        App app = new App();
        JCommander jc = new JCommander(app);

        try {
            jc.parse(args);
        } catch (ParameterException pe) {
            System.err.println(pe.getMessage());
            jc.usage();
            return;
        }

        app.build();

    }
项目:ttc2017smartGrids    文件:JDK6Console.java   
public char[] readPassword(boolean echoInput) {
  try {
    writer.flush();
    Method method;
    if (echoInput) {
        method = console.getClass().getDeclaredMethod("readLine");
        return ((String) method.invoke(console)).toCharArray();
    } else {
        method = console.getClass().getDeclaredMethod("readPassword");
        return (char[]) method.invoke(console);
    }
  }
  catch (Exception e) {
    throw new ParameterException(e);
  }
}
项目:clearwsd    文件:VerbSenseArgumentCounter.java   
private VerbSenseArgumentCounter(String... args) {
    JCommander cmd = new JCommander(this);
    cmd.setProgramName(this.getClass().getSimpleName());
    try {
        if (args.length == 0) {
            cmd.usage();
            System.exit(0);
        }
        cmd.parse(args);
    } catch (ParameterException e) {
        System.err.println(e.getMessage());
        cmd.usage();
        System.exit(1);
    }
    initializeDb();
    initializeAnnotator();
    validateDir();
}
项目:clearwsd    文件:WordSenseCLI.java   
WordSenseCLI(String[] args) {
    cmd = new JCommander(this);
    cmd.setProgramName(WordSenseCLI.class.getSimpleName());
    try {
        cmd.parse(args);
        if (help || args.length == 0) {
            System.out.println(helpMessage);
            cmd.usage();
            System.exit(0);
        }
    } catch (ParameterException e) {
        System.err.println(e.getMessage());
        cmd.usage();
        System.exit(1);
    }
}
项目:accumulo-delimited-ingest    文件:DelimitedIngestArguments.java   
@Override
public void validate(String name, String value) throws ParameterException {
  String[] elements = value.split(",");
  boolean sawRowId = false;
  for (String element : elements) {
    if (DelimitedIngest.ROW_ID.equals(element)) {
      if (sawRowId) {
        throw new ParameterException("Saw multiple instance of '" + DelimitedIngest.ROW_ID + "' in the column mapping.");
      }
      sawRowId = true;
    }
  }
  if (!sawRowId) {
    throw new ParameterException("One element in the column mapping must be '" + DelimitedIngest.ROW_ID + "', but found none");
  }
}
项目:incubator-ratis    文件:Runner.java   
public static void main(String[] args) throws Exception {
  initializeCommands();
  Runner runner = new Runner();
  Server server = new Server();

  JCommander.Builder builder = JCommander.newBuilder().addObject(runner);
  commands.forEach(command -> builder
      .addCommand(command.getClass().getSimpleName().toLowerCase(), command));
  JCommander jc = builder.build();
  try {
    jc.parse(args);
    Optional<SubCommandBase> selectedCommand = commands.stream().filter(
        command -> command.getClass().getSimpleName().toLowerCase()
            .equals(jc.getParsedCommand())).findFirst();
    if (selectedCommand.isPresent()) {
      selectedCommand.get().run();
    } else {
      jc.usage();
    }
  } catch (ParameterException exception) {
    System.err.println("Wrong parameters: " + exception.getMessage());
    jc.usage();
  }

}
项目:jresume    文件:FileLocationValidator.java   
@Override
public void validate(String name, String value) throws ParameterException {
    File file = new File(value);
    ParameterException exception = new ParameterException("File " + file.getAbsolutePath() + " does not exist and cannot be created.");
    try {

        if (!file.exists()) {
            boolean canCreate = file.createNewFile();
            if (canCreate) {
                FileDeleteStrategy.FORCE.delete(file);
            } else {
                throw exception;
            }

        }
    } catch (IOException exc) {
        throw exception;
    }

}
项目:simulacron    文件:CommandLineArguments.java   
@Override
public Level convert(String value) {
  Level convertedValue = Level.valueOf(value);

  if (convertedValue == null) {
    throw new ParameterException("Value " + value + "can not be converted to a logging level.");
  }
  return convertedValue;
}
项目:GitHub    文件:ClassConverter.java   
@Override
public Class convert(String value) {

    if (isBlank(value)) {
        throw new ParameterException(getErrorString("a blank value", "a class"));
    }

    try {
        return Class.forName(value);
    } catch (ClassNotFoundException e) {
        throw new ParameterException(getErrorString(value, "a class"));
    }
}
项目:GitHub    文件:UrlConverter.java   
public URL convert(String value) {
    if (isBlank(value)) {
        throw new ParameterException(getErrorString("a blank value", "a valid URL"));
    }

    try {
        return URLUtil.parseURL(value);
    } catch (IllegalArgumentException e) {
        throw new ParameterException(getErrorString(value, "a valid URL"));
    }

}
项目:hrrs    文件:LoggerLevelSpecsValidator.java   
@Override
public void validate(String name, String loggerLevelSpecs) throws ParameterException {
    if (!loggerLevelSpecs.matches(LOGGER_LEVEL_SPECS_REGEX)) {
        String message = String.format("invalid logger level specification: %s %s", name, loggerLevelSpecs);
        throw new ParameterException(message);
    }
}
项目:ttc2017smartGrids    文件:PropertyFileDefaultProvider.java   
private void init(String fileName) {
  try {
    properties = new Properties();
    URL url = ClassLoader.getSystemResource(fileName);
    if (url != null) {
      properties.load(url.openStream());
    } else {
      throw new ParameterException("Could not find property file: " + fileName
          + " on the class path");
    }
  }
  catch (IOException e) {
    throw new ParameterException("Could not open property file: " + fileName);
  }
}
项目:ttc2017smartGrids    文件:PositiveInteger.java   
public void validate(String name, String value)
    throws ParameterException {
  int n = Integer.parseInt(value);
  if (n < 0) {
    throw new ParameterException("Parameter " + name
        + " should be positive (found " + value +")");
  }
}
项目:ttc2017smartGrids    文件:FloatConverter.java   
public Float convert(String value) {
  try {
    return Float.parseFloat(value);
  } catch(NumberFormatException ex) {
    throw new ParameterException(getErrorString(value, "a float"));
  }
}
项目:ttc2017smartGrids    文件:URLConverter.java   
public URL convert(String value) {
    try {
        return new URL(value);
    } catch (MalformedURLException e) {
        throw new ParameterException(
                getErrorString(value, "a RFC 2396 and RFC 2732 compliant URL"));

    }
}
项目:ttc2017smartGrids    文件:URIConverter.java   
public URI convert(String value) {
  try {
    return new URI(value);
  } catch (URISyntaxException e) {
    throw new ParameterException(getErrorString(value, "a RFC 2396 and RFC 2732 compliant URI"));
  }
}
项目:ttc2017smartGrids    文件:IntegerConverter.java   
public Integer convert(String value) {
  try {
    return Integer.parseInt(value);
  } catch(NumberFormatException ex) {
    throw new ParameterException(getErrorString(value, "an integer"));
  }
}
项目:ttc2017smartGrids    文件:ISO8601DateConverter.java   
public Date convert(String value) {
  try {
    return DATE_FORMAT.parse(value);
  } catch (ParseException pe) {
    throw new ParameterException(getErrorString(value, String.format("an ISO-8601 formatted date (%s)", DATE_FORMAT.toPattern())));
  }
}
项目:ttc2017smartGrids    文件:DoubleConverter.java   
public Double convert(String value) {
  try {
    return Double.parseDouble(value);
  } catch(NumberFormatException ex) {
    throw new ParameterException(getErrorString(value, "a double"));
  }
}
项目:ttc2017smartGrids    文件:BigDecimalConverter.java   
public BigDecimal convert(String value) {
  try {
    return new BigDecimal(value);
  } catch (NumberFormatException nfe) {
    throw new ParameterException(getErrorString(value, "a BigDecimal"));
  }
}
项目:ttc2017smartGrids    文件:BooleanConverter.java   
public Boolean convert(String value) {
  if ("false".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value)) {
    return Boolean.parseBoolean(value);
  } else {
    throw new ParameterException(getErrorString(value, "a boolean"));
  }
}
项目:ttc2017smartGrids    文件:LongConverter.java   
public Long convert(String value) {
  try {
    return Long.parseLong(value);
  } catch(NumberFormatException ex) {
    throw new ParameterException(getErrorString(value, "a long"));
  }
}
项目:ttc2017smartGrids    文件:PropertyFileDefaultProvider.java   
private void init(String fileName) {
  try {
    properties = new Properties();
    URL url = ClassLoader.getSystemResource(fileName);
    if (url != null) {
      properties.load(url.openStream());
    } else {
      throw new ParameterException("Could not find property file: " + fileName
          + " on the class path");
    }
  }
  catch (IOException e) {
    throw new ParameterException("Could not open property file: " + fileName);
  }
}
项目:jcommander-ext    文件:ParameterExceptionsTest.java   
@Test
public void shouldThrow() throws Exception {
    assertThat(ParameterExceptions.invalidParameter("name", "desc"))
            .isInstanceOf(ParameterException.class)
            .hasMessageContaining("'name'")
            .hasMessageEndingWith("desc" + ".")
    ;
}
项目:ttc2017smartGrids    文件:PositiveInteger.java   
public void validate(String name, String value)
    throws ParameterException {
  int n = Integer.parseInt(value);
  if (n < 0) {
    throw new ParameterException("Parameter " + name
        + " should be positive (found " + value +")");
  }
}
项目:hrrs    文件:NonZeroPositiveIntegerValidator.java   
public void validate(String name, String value) throws ParameterException {
    int n = Integer.parseInt(value);
    if (n <= 0) {
        String message = String.format("expecting a non-zero positive integer: %s %d", name, n);
        throw new ParameterException(message);
    }
}
项目:ttc2017smartGrids    文件:FloatConverter.java   
public Float convert(String value) {
  try {
    return Float.parseFloat(value);
  } catch(NumberFormatException ex) {
    throw new ParameterException(getErrorString(value, "a float"));
  }
}
项目:ttc2017smartGrids    文件:URLConverter.java   
public URL convert(String value) {
    try {
        return new URL(value);
    } catch (MalformedURLException e) {
        throw new ParameterException(
                getErrorString(value, "a RFC 2396 and RFC 2732 compliant URL"));

    }
}
项目:ttc2017smartGrids    文件:IntegerConverter.java   
public Integer convert(String value) {
  try {
    return Integer.parseInt(value);
  } catch(NumberFormatException ex) {
    throw new ParameterException(getErrorString(value, "an integer"));
  }
}
项目:ttc2017smartGrids    文件:ISO8601DateConverter.java   
public Date convert(String value) {
  try {
    return DATE_FORMAT.parse(value);
  } catch (ParseException pe) {
    throw new ParameterException(getErrorString(value, String.format("an ISO-8601 formatted date (%s)", DATE_FORMAT.toPattern())));
  }
}
项目:ttc2017smartGrids    文件:DoubleConverter.java   
public Double convert(String value) {
  try {
    return Double.parseDouble(value);
  } catch(NumberFormatException ex) {
    throw new ParameterException(getErrorString(value, "a double"));
  }
}
项目:ttc2017smartGrids    文件:BigDecimalConverter.java   
public BigDecimal convert(String value) {
  try {
    return new BigDecimal(value);
  } catch (NumberFormatException nfe) {
    throw new ParameterException(getErrorString(value, "a BigDecimal"));
  }
}
项目:ttc2017smartGrids    文件:BooleanConverter.java   
public Boolean convert(String value) {
  if ("false".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value)) {
    return Boolean.parseBoolean(value);
  } else {
    throw new ParameterException(getErrorString(value, "a boolean"));
  }
}
项目:Java_Good    文件:ContactDataGenerator.java   
public static void main(String[] args) throws IOException {
    ContactDataGenerator generator = new ContactDataGenerator();
    JCommander jCommander = new JCommander(generator);
    try {
        jCommander.parse(args);
    } catch (ParameterException ex) {
        jCommander.usage();
        return;
    }
    generator.run();
}
项目:phoenix.webui.framework    文件:BrowserParamValidator.java   
@Override
public void validate(String name, String value) throws ParameterException
{
    if(!DriverConstants.DRIVER_CHROME.equals(value)
            && !DriverConstants.DRIVER_FIREFOX.equals(value)
            && !DriverConstants.DRIVER_IE.equals(value)
            && !DriverConstants.DRIVER_HTML_UNIT.equals(value)
            && !DriverConstants.DRIVER_OPERA.equals(value)
            && !DriverConstants.DRIVER_PHANTOM_JS.equals(value)
            && value != null
            && !value.equals(""))
    {
        throw new ParameterException("Browser is invalid.");
    }
}
项目:eadlsync    文件:LogLevelConverter.java   
@Override
public Level convert(String value) {
    if (supportedLogLevels.contains(value.toUpperCase())) {
        return Level.toLevel(value, Level.OFF);
    }
    throw new ParameterException(String.format("Your value (%s) is not a supported log level %s", value, supportedLogLevels));
}
项目:eadlsync    文件:CommitIdValidator.java   
@Override
public void validate(String name, String value) throws ParameterException {
    if (!YStatementConstants.COMMIT_ID_PATTERN.matcher(value).matches()) {
        throw new ParameterException(String.format("The Parameter %s does not match the required "
                + "pattern for a commit id of the se-repo (found %s)", name, value));
    }
}