private static int getRequiredPositiveInt(CommandLine line, Option option) { String iterVal = line.getOptionValue(option.getOpt()); try { return Integer.parseInt(iterVal); } catch (NumberFormatException e) { String msg = "'" + option.getLongOpt() + "' value must be a positive integer."; throw new IllegalArgumentException(msg, e); } }
public static Properties commandLine2Properties(final CommandLine commandLine) { Properties properties = new Properties(); Option[] opts = commandLine.getOptions(); if (opts != null) { for (Option opt : opts) { String name = opt.getLongOpt(); String value = commandLine.getOptionValue(name); if (value != null) { properties.setProperty(name, value); } } } return properties; }
@Override protected void runPlainPath(AlluxioURI plainPath, CommandLine cl) throws AlluxioException, IOException { load(plainPath, cl.hasOption(LOCAL_OPTION.getLongOpt())); }
private static String createMutexMessage(Option... options) { StringBuilder sb = new StringBuilder(); sb.append("The "); for (int i = 0; i < options.length; i++) { if (i > 0) { sb.append(", "); } Option o = options[0]; sb.append("-").append(o.getOpt()).append("/--").append(o.getLongOpt()); } sb.append(" and generated salt options are mutually exclusive. Only one of them may be used at a time"); return sb.toString(); }
private static List<URL> checkUrls(CommandLine line, Option option) { if (line.hasOption(option.getOpt())) { final String[] urls = line.getOptionValues(option.getOpt()); return Arrays.stream(urls) .distinct() .map((url) -> { try { return Path.fromLocalFile(new File(url).getAbsoluteFile()).toUri().toURL(); } catch (Exception e) { throw new SqlClientException("Invalid path for option '" + option.getLongOpt() + "': " + url, e); } }) .collect(Collectors.toList()); } return null; }
/** * Retrieves the array of values, if any, of an option. * * @param opt string name of the option * @return Values of the argument if option is set, and has an argument, * otherwise null. */ public String[] getOptionValues(String opt) { List<String> values = new ArrayList<String>(); for (Option option : options) { if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt())) { values.addAll(option.getValuesList()); } } return values.isEmpty() ? null : values.toArray(new String[values.size()]); }
/** * Retrieves the option object given the long or short option as a String * * @param opt short or long name of the option * @return Canonicalized option */ private Option resolveOption(String opt) { opt = Util.stripLeadingHyphens(opt); for (Option option : options) { if (opt.equals(option.getOpt())) { return option; } if (opt.equals(option.getLongOpt())) { return option; } } return null; }
if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt()))
public static GalenActionConfigArguments parse(String[] args) { args = ArgumentsUtils.processSystemProperties(args); Options options = new Options(); options.addOption("g", "global", false, "Flag to create global config in user home directory"); CommandLineParser parser = new PosixParser(); CommandLine cmd; try { cmd = parser.parse(options, args); } catch (MissingArgumentException e) { throw new IllegalArgumentException("Missing value for " + e.getOption().getLongOpt(), e); } catch (Exception ex) { throw new RuntimeException(ex); } GalenActionConfigArguments configArguments = new GalenActionConfigArguments(); configArguments.isGlobal = cmd.hasOption("g"); return configArguments; }
@Override public int run(CommandLine cl) throws IOException { String[] args = cl.getArgs(); String dir; if (args.length < 1) { dir = null; } else { dir = args[0]; } boolean local = cl.hasOption(LOCAL_OPTION.getLongOpt()); BackupResponse resp = mMetaClient.backup(dir, local); if (local) { mPrintStream.printf("Successfully backed up journal to %s on master %s%n", resp.getBackupUri(), resp.getHostname()); } else { mPrintStream.printf("Successfully backed up journal to %s%n", resp.getBackupUri()); } return 0; }
/** * Adds an option instance * * @param opt the option that is to be added * @return the resulting Options instance */ public Options addOption(Option opt) { String key = opt.getKey(); // add it to the long option list if (opt.hasLongOpt()) { longOpts.put(opt.getLongOpt(), opt); } // if the option is required add it to the required list if (opt.isRequired()) { if (requiredOpts.contains(key)) { requiredOpts.remove(requiredOpts.indexOf(key)); } requiredOpts.add(key); } shortOpts.put(key, opt); return this; }
final String argument = "--" + o.getLongOpt();
public static GalenActionGenerateArguments parse(String[] args) { args = ArgumentsUtils.processSystemProperties(args); Options options = new Options(); options.addOption("e", "export", true, "Path to generated spec file"); options.addOption("G", "no-galen-extras", false, "Disable galen-extras expressions"); CommandLineParser parser = new PosixParser(); CommandLine cmd; try { cmd = parser.parse(options, args); } catch (MissingArgumentException e) { throw new IllegalArgumentException("Missing value for " + e.getOption().getLongOpt(), e); } catch (Exception ex) { throw new RuntimeException(ex); } GalenActionGenerateArguments arguments = new GalenActionGenerateArguments(); arguments.setExport(cmd.getOptionValue("e")); arguments.setUseGalenExtras(!cmd.hasOption("G")); if (cmd.getArgs() == null || cmd.getArgs().length < 1) { throw new IllegalArgumentException("Missing page dump file"); } arguments.setPath(cmd.getArgs()[0]); return arguments; }
return -1; if (cl.hasOption(MODE_OPTION.getLongOpt())) { UfsPMode mode; switch (cl.getOptionValue(MODE_OPTION.getLongOpt())) { case "noAccess": mode = UfsPMode.NO_ACCESS;
private void logOptions(String sectionName, String prefix, Options options) { if ( sectionName != null ) { log.info("== " + sectionName + " =="); } //noinspection unchecked for ( Option option : (Iterable<? extends Option>)options.getOptions() ) { if ( option.hasLongOpt() ) { if ( option.hasArg() ) { log.info(prefix + option.getLongOpt() + " <arg> - " + option.getDescription()); } else { log.info(prefix + option.getLongOpt() + " - " + option.getDescription()); } } } }
@Override public int run(CommandLine cl) throws AlluxioException, IOException { String[] args = cl.getArgs(); if (args.length == 0) { Map<String, MountPointInfo> mountTable = mFileSystem.getMountTable(); UfsCommand.printMountInfo(mountTable); return 0; } AlluxioURI alluxioPath = new AlluxioURI(args[0]); AlluxioURI ufsPath = new AlluxioURI(args[1]); MountPOptions.Builder optionsBuilder = MountPOptions.newBuilder(); if (cl.hasOption(READONLY_OPTION.getLongOpt())) { optionsBuilder.setReadOnly(true); } if (cl.hasOption(SHARED_OPTION.getLongOpt())) { optionsBuilder.setShared(true); } if (cl.hasOption(OPTION_OPTION.getLongOpt())) { Properties properties = cl.getOptionProperties(OPTION_OPTION.getLongOpt()); optionsBuilder.putAllProperties(Maps.fromProperties(properties)); } mFileSystem.mount(alluxioPath, ufsPath, optionsBuilder.build()); System.out.println("Mounted " + ufsPath + " at " + alluxioPath); return 0; }
@Override protected void runPlainPath(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException { // TODO(calvin): Remove explicit state checking. boolean recursive = cl.hasOption("R"); if (!mFileSystem.exists(path)) { throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path)); } if (!recursive && mFileSystem.getStatus(path).isFolder()) { throw new IOException( path.getPath() + " is a directory, to remove it, please use \"rm -R <path>\""); } boolean isAlluxioOnly = cl.hasOption(REMOVE_ALLUXIO_ONLY.getLongOpt()); DeletePOptions options = DeletePOptions.newBuilder().setRecursive(recursive).setAlluxioOnly(isAlluxioOnly) .setUnchecked(cl.hasOption(REMOVE_UNCHECKED_OPTION_CHAR)).build(); mFileSystem.delete(path, options); if (!isAlluxioOnly) { System.out.println(path + " has been removed"); } else { System.out.println(path + " has been removed from Alluxio space"); } }
@Test public void testCompleteOption( ) { Option simple = OptionBuilder.withLongOpt( "simple option") .hasArg( ) .isRequired( ) .hasArgs( ) .withType( Float.class ) .withDescription( "this is a simple option" ) .create( 's' ); assertEquals( "s", simple.getOpt() ); assertEquals( "simple option", simple.getLongOpt() ); assertEquals( "this is a simple option", simple.getDescription() ); assertEquals( simple.getType(), Float.class ); assertTrue( simple.hasArg() ); assertTrue( simple.isRequired() ); assertTrue( simple.hasArgs() ); }
@Test public void testTwoCompleteOptions( ) { Option simple = OptionBuilder.withLongOpt( "simple option") .hasArg( ) .isRequired( ) .hasArgs( ) .withType( Float.class ) .withDescription( "this is a simple option" ) .create( 's' ); assertEquals( "s", simple.getOpt() ); assertEquals( "simple option", simple.getLongOpt() ); assertEquals( "this is a simple option", simple.getDescription() ); assertEquals( simple.getType(), Float.class ); assertTrue( simple.hasArg() ); assertTrue( simple.isRequired() ); assertTrue( simple.hasArgs() ); simple = OptionBuilder.withLongOpt( "dimple option") .hasArg( ) .withDescription( "this is a dimple option" ) .create( 'd' ); assertEquals( "d", simple.getOpt() ); assertEquals( "dimple option", simple.getLongOpt() ); assertEquals( "this is a dimple option", simple.getDescription() ); assertEquals( String.class, simple.getType() ); assertTrue( simple.hasArg() ); assertTrue( !simple.isRequired() ); assertTrue( !simple.hasArgs() ); }
private static void checkOption(Option option, String opt, String description, String longOpt, int numArgs, String argName, boolean required, boolean optionalArg, char valueSeparator, Class<?> cls) { assertEquals(opt, option.getOpt()); assertEquals(description, option.getDescription()); assertEquals(longOpt, option.getLongOpt()); assertEquals(numArgs, option.getArgs()); assertEquals(argName, option.getArgName()); assertEquals(required, option.isRequired()); assertEquals(optionalArg, option.hasOptionalArg()); assertEquals(valueSeparator, option.getValueSeparator()); assertEquals(cls, option.getType()); }