/** * Retrieves the argument as an Integer object, if any, of an option. * * @param option the option that we want argument value to be returned for * @param defaultValue is the default value to be returned if the option is not specified * * @return Value of the argument if option is set, and has an argument, otherwise defaultValue. * @throws NumberFormatException if there are problems parsing the option value into the Integer type */ public Integer getIntegerValue(Option option, Integer defaultValue) throws NumberFormatException { Integer answer = getIntegerValue(option); return (answer != null) ? answer : defaultValue; }
/** * Retrieves the argument as a String object, if any, of an option. * * @param option the option that we want argument value to be returned for * @param defaultValue is the default value to be returned if the option is not specified * * @return Value of the argument if option is set, and has an argument, otherwise defaultValue. */ public String getStringValue(Option option, String defaultValue) { String answer = getStringValue(option); return (answer != null) ? answer : defaultValue; }
/** * Constructs a new UploaderApp instance. */ public UploaderApp() { argParser = new ArgumentParser("herd-uploader-app"); // Create command line options specific to the uploader. Other common options will be handled by the base class. createNewVersionOpt = argParser .addArgument("V", "createNewVersion", false, "If not set, only initial version of the business object data is allowed to be created.", false); rrsOpt = argParser.addArgument("r", "rrs", false, "If set, the data will be saved in Reduced Redundancy Storage.", false); maxRetryAttemptsOpt = argParser.addArgument("R", "maxRetryAttempts", true, "The maximum number of the business object data registration retry attempts that uploader would perform before rolling back the upload.", false); retryDelaySecsOpt = argParser.addArgument("D", "retryDelaySecs", true, "The delay in seconds between the business object data registration retry attempts.", false); forceOpt = argParser.addArgument("f", "force", false, "If set, allows upload to proceed when the latest version of the business object data has UPLOADING status by invalidating that version.", false); }
namespaceOpt = argParser.addArgument("n", "namespace", true, "Namespace.", true); businessObjectDefinitionNameOpt = argParser.addArgument("b", "businessObjectDefinitionName", true, "Business object definition.", true); localOutputFileOpt = argParser.addArgument("o", "localOutputFile", true, "The path to files on your local file system.", true); regServerHostOpt = argParser.addArgument("H", "regServerHost", true, "Registration Service hostname.", true); Option regServerPortOpt = argParser.addArgument("P", "regServerPort", true, "Registration Service port.", true); udcServerHostOpt = argParser.addArgument("c", "udcServerHost", true, "UDC application server hostname.", true); Option sslOpt = argParser.addArgument("s", "ssl", true, "Enable or disable SSL (HTTPS).", false); usernameOpt = argParser.addArgument("u", "username", true, "The username for HTTPS client authentication.", false); passwordOpt = argParser.addArgument("w", "password", true, "The password used for HTTPS client authentication.", false); trustSelfSignedCertificateOpt = argParser.addArgument("C", "trustSelfSignedCertificate", true, "If set to true, makes HTTPS client trust self-signed certificate.", false); disableHostnameVerificationOpt = argParser.addArgument("d", "disableHostnameVerification", true, "If set to true, turns off hostname verification.", false); Option helpOpt = argParser.addArgument("h", "help", false, "Display usage information and exit.", false); Option versionOpt = argParser.addArgument("v", "version", false, "Display version information and exit.", false); argParser.parseArguments(args, false); if (argParser.getBooleanValue(helpOpt)) System.out.println(argParser.getUsageInformation()); return ToolsCommonConstants.ReturnValue.SUCCESS; if (argParser.getBooleanValue(versionOpt)) argParser.parseArguments(args, true); useSsl = argParser.getStringValueAsBoolean(sslOpt, false); trustSelfSignedCertificate = argParser.getStringValueAsBoolean(trustSelfSignedCertificateOpt, false);
@Test(expected = UnrecognizedOptionException.class) public void testParseArgumentsUnrecognizedOptionException() throws ParseException { ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument("a", "configured_option", false, "Some flag parameter", false); argParser.parseArguments(new String[] {"-b", "unrecognized_option"}); }
@Test public void testGetIntegerNullValue() throws ParseException { // Create an argument parser with an optional "a" option. ArgumentParser argParser = new ArgumentParser(""); Option option = argParser.addArgument("a", "some_required_parameter", false, "Some required parameter with an argument", false); // Parse the arguments and get an integer value which wasn't specified which should return null. argParser.parseArguments(new String[] {}); Integer value = argParser.getIntegerValue(option, null, 0, 100); assertNull(value); }
S3FileTransferRequestParamsDto.builder().withLocalPath(argParser.getStringValue(localPathOpt)).withUseRrs(argParser.getBooleanValue(rrsOpt)) .withAwsAccessKeyId(argParser.getStringValue(s3AccessKeyOpt)).withAwsSecretKey(argParser.getStringValue(s3SecretKeyOpt)) .withS3Endpoint(argParser.getStringValue(s3EndpointOpt)).withMaxThreads(maxThreads) .withHttpProxyHost(argParser.getStringValue(httpProxyHostOpt)).withHttpProxyPort(httpProxyPort) .withSocketTimeout(argParser.getIntegerValue(socketTimeoutOpt)).build(); RegServerAccessParamsDto regServerAccessParamsDto = RegServerAccessParamsDto.builder().withRegServerHost(regServerHost).withRegServerPort(regServerPort).withUseSsl(useSsl) .withUsername(argParser.getStringValue(usernameOpt)).withPassword(argParser.getStringValue(passwordOpt)) .withTrustSelfSignedCertificate(trustSelfSignedCertificate).withDisableHostnameVerification(disableHostnameVerification).build(); controller.performUpload(regServerAccessParamsDto, argParser.getFileValue(manifestPathOpt), params, argParser.getBooleanValue(createNewVersionOpt), argParser.getBooleanValue(forceOpt), maxRetryAttempts, retryDelaySecs);
@Test public void testGetStringValue() throws ParseException { final String testDefaultValue = "default_str_value"; ArgumentParser argParser = new ArgumentParser(""); Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter", false); String inputValue; String resultValue; final String shortStrOpt = String.format("-%s", strOpt.getOpt()); final String longStrOpt = String.format("--%s", strOpt.getLongOpt()); argParser.parseArguments(new String[] {}); assertNull(argParser.getStringValue(strOpt)); assertEquals(testDefaultValue, argParser.getStringValue(strOpt, testDefaultValue)); inputValue = "my_string_value_1"; argParser.parseArguments(new String[] {shortStrOpt, inputValue}); resultValue = argParser.getStringValue(strOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = "my_string_value_2"; argParser.parseArguments(new String[] {shortStrOpt, inputValue}); resultValue = argParser.getStringValue(strOpt, testDefaultValue); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = "my_string_value_3"; argParser.parseArguments(new String[] {longStrOpt, inputValue}); resultValue = argParser.getStringValue(strOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); }
@Test public void testGetFileValue() throws ParseException { File testDefaultValue = new File("default_file_name"); ArgumentParser argParser = new ArgumentParser(""); Option fileOpt = argParser.addArgument("f", "file", true, "Source file name", false); File inputValue; File resultValue; final String shortFileOpt = String.format("-%s", fileOpt.getOpt()); final String longFileOpt = String.format("--%s", fileOpt.getLongOpt()); argParser.parseArguments(new String[] {""}); assertNull(argParser.getFileValue(fileOpt)); assertEquals(testDefaultValue, argParser.getFileValue(fileOpt, testDefaultValue)); inputValue = new File("folder/file_name_1"); argParser.parseArguments(new String[] {shortFileOpt, inputValue.toString()}); resultValue = argParser.getFileValue(fileOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = new File("folder/file_name_2"); argParser.parseArguments(new String[] {shortFileOpt, inputValue.toString()}); resultValue = argParser.getFileValue(fileOpt, testDefaultValue); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = new File("file_name_3"); argParser.parseArguments(new String[] {longFileOpt, inputValue.toString()}); resultValue = argParser.getFileValue(fileOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); }
@Test public void testGetBooleanValue() throws ParseException { ArgumentParser argParser = new ArgumentParser(""); Option boolOpt = argParser.addArgument("b", "bool", false, "Some optional configuration flag", false); Boolean resultValue; final String shortBoolOpt = String.format("-%s", boolOpt.getOpt()); final String longBoolOpt = String.format("--%s", boolOpt.getLongOpt()); argParser.parseArguments(new String[] {}); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertFalse(resultValue); argParser.parseArguments(new String[] {shortBoolOpt}); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertTrue(resultValue); argParser.parseArguments(new String[] {longBoolOpt}); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertTrue(resultValue); }
@Test public void testGetStringValueAsBoolean() throws ParseException ArgumentParser argParser = new ArgumentParser(""); Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter to have a boolean value", false); argParser.parseArguments(new String[] {}); assertFalse(argParser.getStringValueAsBoolean(strOpt, false)); assertTrue(argParser.getStringValueAsBoolean(strOpt, true)); .asList(CustomBooleanEditor.VALUE_TRUE, CustomBooleanEditor.VALUE_YES, CustomBooleanEditor.VALUE_ON, CustomBooleanEditor.VALUE_1)) argParser.parseArguments(new String[] {shortStrOpt, inputValue}); assertTrue(argParser.getStringValueAsBoolean(strOpt, false)); argParser.parseArguments(new String[] {longStrOpt, inputValue}); assertTrue(argParser.getStringValueAsBoolean(strOpt, false)); .asList(CustomBooleanEditor.VALUE_FALSE, CustomBooleanEditor.VALUE_NO, CustomBooleanEditor.VALUE_OFF, CustomBooleanEditor.VALUE_0)) argParser.parseArguments(new String[] {shortStrOpt, inputValue}); assertFalse(argParser.getStringValueAsBoolean(strOpt, true)); argParser.parseArguments(new String[] {shortStrOpt, INVALID_BOOLEAN_VALUE}); try argParser.getStringValueAsBoolean(strOpt, false); fail("Suppose to throw a ParseException when option has an invalid boolean value.");
S3FileTransferRequestParamsDto params = S3FileTransferRequestParamsDto.builder().withLocalPath(argParser.getStringValue(localPathOpt)) .withAwsAccessKeyId(argParser.getStringValue(s3AccessKeyOpt)).withAwsSecretKey(argParser.getStringValue(s3SecretKeyOpt)) .withS3Endpoint(argParser.getStringValue(s3EndpointOpt)).withMaxThreads(maxThreads).withHttpProxyHost(argParser.getStringValue(httpProxyHostOpt)) .withHttpProxyPort(httpProxyPort).withSocketTimeout(argParser.getIntegerValue(socketTimeoutOpt)).build(); RegServerAccessParamsDto regServerAccessParamsDto = RegServerAccessParamsDto.builder().withRegServerHost(regServerHost).withRegServerPort(regServerPort).withUseSsl(useSsl) .withUsername(argParser.getStringValue(usernameOpt)).withPassword(argParser.getStringValue(passwordOpt)) .withTrustSelfSignedCertificate(trustSelfSignedCertificate).withDisableHostnameVerification(disableHostnameVerification).build(); controller.performDownload(regServerAccessParamsDto, argParser.getFileValue(manifestPathOpt), params);
@Test public void testGetUsageInformation() { List<Option> optionsIn = new ArrayList<>(); optionsIn.add(new Option("a", "some_flag", false, "Some flag parameter")); optionsIn.add(new Option("b", "some_parameter", true, "Some parameter with an argument")); ArgumentParser argParser = new ArgumentParser("TestApp"); argParser.addArgument(optionsIn.get(0), false); argParser.addArgument(optionsIn.get(1), true); String usage = argParser.getUsageInformation(); assertNotNull(usage); assertTrue(usage.contains(String.format("usage: %s", argParser.getApplicationName()))); for (Option option : optionsIn) { assertTrue(usage.contains(String.format("-%s,", option.getOpt()))); assertTrue(usage.contains(String.format("--%s", option.getLongOpt()))); assertTrue(usage.contains(option.getDescription())); assertTrue(!option.hasArg() || usage.contains("<arg>")); } }
RegServerAccessParamsDto.builder().withRegServerHost(argParser.getStringValue(regServerHostOpt)).withRegServerPort(regServerPort).withUseSsl(useSsl) .withUsername(argParser.getStringValue(usernameOpt)).withPassword(argParser.getStringValue(passwordOpt)) .withTrustSelfSignedCertificate(trustSelfSignedCertificate).withDisableHostnameVerification(disableHostnameVerification).build(); controller.performRetentionExpirationExport(argParser.getStringValue(namespaceOpt), argParser.getStringValue(businessObjectDefinitionNameOpt), argParser.getFileValue(localOutputFileOpt), regServerAccessParamsDto, argParser.getStringValue(udcServerHostOpt));
maxRetryAttempts = argParser.getIntegerValue(maxRetryAttemptsOpt, MAX_RETRY_ATTEMPTS_DEFAULT, MAX_RETRY_ATTEMPTS_MIN, MAX_RETRY_ATTEMPTS_MAX); retryDelaySecs = argParser.getIntegerValue(retryDelaySecsOpt, RETRY_DELAY_SECS_DEFAULT, RETRY_DELAY_SECS_MIN, RETRY_DELAY_SECS_MAX); LOGGER.error("Error parsing command line arguments: " + ex.getMessage() + "\n" + argParser.getUsageInformation()); return ReturnValue.FAILURE;
@Test public void testAddArgument() { Option opt1 = new Option("a", "some_flag", false, "Some flag parameter"); Option opt2 = new Option("b", "some_parameter", true, "Some parameter with an argument"); List<String> optionsIn = Arrays.asList(optionToString(opt1), optionToString(opt2)); ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument(opt1, true); argParser.addArgument(opt2.getOpt(), opt2.getLongOpt(), opt2.hasArg(), opt2.getDescription(), false); Collection resultOptions = argParser.getConfiguredOptions(); List<String> optionsOut = new ArrayList<>(); for (Object obj : resultOptions) { assertTrue(obj instanceof Option); optionsOut.add(optionToString((Option) obj)); } optionsOut.containsAll(optionsIn); optionsIn.containsAll(optionsOut); }
/** * Constructs a new UploaderApp instance. */ public DownloaderApp() { argParser = new ArgumentParser("herd-downloader-app"); }
ensureCommandLineNotNull(); String stringValue = getStringValue(option);
/** * Retrieves the argument as a File object, if any, of an option. * * @param option the option that we want argument value to be returned for * @param defaultValue is the default value to be returned if the option is not specified * * @return Value of the argument if option is set, and has an argument, otherwise defaultValue. */ public File getFileValue(Option option, File defaultValue) { File answer = getFileValue(option); return (answer != null) ? answer : defaultValue; } }
/** * Creates and adds an Option using the specified parameters. The parameters contain a short-name and a long-name. It may be specified as requiring an * argument and/or being mandatory. * * @param opt Short single-character name of the option. * @param longOpt Long multi-character name of the option. * @param hasArg flag signally if an argument is required after this option * @param description Self-documenting description * @param required specifies whether the option being added is mandatory * * @return the resulting option that was added */ public Option addArgument(String opt, String longOpt, boolean hasArg, String description, boolean required) { Option option = new Option(opt, longOpt, hasArg, description); return addArgument(option, required); }