You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by cm...@apache.org on 2013/09/09 20:53:01 UTC
svn commit: r1521240 -
/hadoop/common/branches/HDFS-4949/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java
Author: cmccabe
Date: Mon Sep 9 18:53:01 2013
New Revision: 1521240
URL: http://svn.apache.org/r1521240
Log:
HDFS-5120. Add command-line support for manipulating cache pools.
Modified:
hadoop/common/branches/HDFS-4949/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java
Modified: hadoop/common/branches/HDFS-4949/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java?rev=1521240&r1=1521239&r2=1521240&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java Mon Sep 9 18:53:01 2013
@@ -905,4 +905,81 @@ public class StringUtils {
}
return str.toString();
}
+
+ /**
+ * From a list of command-line arguments, remove both an option and the
+ * next argument.
+ *
+ * @param name Name of the option to remove. Example: -foo.
+ * @param args List of arguments.
+ * @return null if the option was not found; the value of the
+ * option otherwise.
+ */
+ public static String popOptionWithArgument(String name, List<String> args) {
+ String val = null;
+ for (Iterator<String> iter = args.iterator(); iter.hasNext(); ) {
+ String cur = iter.next();
+ if (cur.equals("--")) {
+ // stop parsing arguments when you see --
+ break;
+ } else if (cur.equals(name)) {
+ iter.remove();
+ if (!iter.hasNext()) {
+ throw new RuntimeException("option " + name + " requires 1 " +
+ "argument.");
+ }
+ val = iter.next();
+ iter.remove();
+ break;
+ }
+ }
+ return val;
+ }
+
+ /**
+ * From a list of command-line arguments, remove an option.
+ *
+ * @param name Name of the option to remove. Example: -foo.
+ * @param args List of arguments.
+ * @return true if the option was found and removed; false otherwise.
+ */
+ public static boolean popOption(String name, List<String> args) {
+ for (Iterator<String> iter = args.iterator(); iter.hasNext(); ) {
+ String cur = iter.next();
+ if (cur.equals("--")) {
+ // stop parsing arguments when you see --
+ break;
+ } else if (cur.equals(name)) {
+ iter.remove();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * From a list of command-line arguments, return the first non-option
+ * argument. Non-option arguments are those which either come after
+ * a double dash (--) or do not start with a dash.
+ *
+ * @param args List of arguments.
+ * @return The first non-option argument, or null if there were none.
+ */
+ public static String popFirstNonOption(List<String> args) {
+ for (Iterator<String> iter = args.iterator(); iter.hasNext(); ) {
+ String cur = iter.next();
+ if (cur.equals("--")) {
+ if (!iter.hasNext()) {
+ return null;
+ }
+ cur = iter.next();
+ iter.remove();
+ return cur;
+ } else if (!cur.startsWith("-")) {
+ iter.remove();
+ return cur;
+ }
+ }
+ return null;
+ }
}