From 337d9fa4c0edc6472a582ee60eb3d704140a9ae8 Mon Sep 17 00:00:00 2001
From: "Ilya.Kazakevich"
Date: Wed, 11 Feb 2015 23:32:36 +0300
Subject: [PATCH] PY-11855 Run manage.py task improvements
Options and arguments are parsed on CommandLikeParser now
---
.../commandInterface/command/Option.java | 2 +-
.../CommandLineParseResult.java | 2 +-
.../CommandLinePartType.java | 5 +
.../MalformedCommandLineException.java | 2 +-
.../OptParseCommandLineParser.java | 71 --------
.../optParse/LongOptionParser.java | 53 ++++++
.../optParse/OptParseCommandLineParser.java | 161 ++++++++++++++++++
.../optParse/OptionParser.java | 40 +++++
.../optParse/ShortOptionParser.java | 59 +++++++
.../optParse/package-info.java | 22 +++
10 files changed, 343 insertions(+), 74 deletions(-)
delete mode 100644 python/src/com/jetbrains/python/commandLineParser/OptParseCommandLineParser.java
create mode 100644 python/src/com/jetbrains/python/commandLineParser/optParse/LongOptionParser.java
create mode 100644 python/src/com/jetbrains/python/commandLineParser/optParse/OptParseCommandLineParser.java
create mode 100644 python/src/com/jetbrains/python/commandLineParser/optParse/OptionParser.java
create mode 100644 python/src/com/jetbrains/python/commandLineParser/optParse/ShortOptionParser.java
create mode 100644 python/src/com/jetbrains/python/commandLineParser/optParse/package-info.java
diff --git a/python/src/com/jetbrains/python/commandInterface/command/Option.java b/python/src/com/jetbrains/python/commandInterface/command/Option.java
index bb7fce2d973b..0099e8c7b229 100644
--- a/python/src/com/jetbrains/python/commandInterface/command/Option.java
+++ b/python/src/com/jetbrains/python/commandInterface/command/Option.java
@@ -78,7 +78,7 @@ public final class Option {
* @return if option accepts argument -- pair of [argument_quantity, its_type_info]. Null otherwise.
*/
@Nullable
- Pair getArgumentAndQuantity() {
+ public Pair getArgumentAndQuantity() {
return myArgumentAndQuantity;
}
diff --git a/python/src/com/jetbrains/python/commandLineParser/CommandLineParseResult.java b/python/src/com/jetbrains/python/commandLineParser/CommandLineParseResult.java
index a2a9756b0c26..a49394b37ef0 100644
--- a/python/src/com/jetbrains/python/commandLineParser/CommandLineParseResult.java
+++ b/python/src/com/jetbrains/python/commandLineParser/CommandLineParseResult.java
@@ -38,7 +38,7 @@ public final class CommandLineParseResult {
@NotNull
private final WordWithPosition myCommand;
- CommandLineParseResult(
+ public CommandLineParseResult(
@NotNull final WordWithPosition command,
@NotNull final Collection> parts) {
myCommand = command;
diff --git a/python/src/com/jetbrains/python/commandLineParser/CommandLinePartType.java b/python/src/com/jetbrains/python/commandLineParser/CommandLinePartType.java
index 0f2359e15087..1caf82eaad88 100644
--- a/python/src/com/jetbrains/python/commandLineParser/CommandLinePartType.java
+++ b/python/src/com/jetbrains/python/commandLineParser/CommandLinePartType.java
@@ -29,6 +29,11 @@ public enum CommandLinePartType {
* Option is named but optional parameter. Like "-l" in "ls -l".
*/
OPTION,
+ /**
+ * Option argument like --folder-to-delete=/
+ * Here root is option argument
+ */
+ OPTION_ARGUMENT,
/**
* Some part of command line that {@link com.jetbrains.python.commandLineParser.CommandLineParser} does not understand
*/
diff --git a/python/src/com/jetbrains/python/commandLineParser/MalformedCommandLineException.java b/python/src/com/jetbrains/python/commandLineParser/MalformedCommandLineException.java
index 8f69e96bf96e..5ef572961036 100644
--- a/python/src/com/jetbrains/python/commandLineParser/MalformedCommandLineException.java
+++ b/python/src/com/jetbrains/python/commandLineParser/MalformedCommandLineException.java
@@ -23,7 +23,7 @@ import org.jetbrains.annotations.NotNull;
* @author Ilya.Kazakevich
*/
public class MalformedCommandLineException extends Exception {
- MalformedCommandLineException(@NotNull final String message) {
+ public MalformedCommandLineException(@NotNull final String message) {
super(message);
}
}
diff --git a/python/src/com/jetbrains/python/commandLineParser/OptParseCommandLineParser.java b/python/src/com/jetbrains/python/commandLineParser/OptParseCommandLineParser.java
deleted file mode 100644
index ddcd0822e53c..000000000000
--- a/python/src/com/jetbrains/python/commandLineParser/OptParseCommandLineParser.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright 2000-2015 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.jetbrains.python.commandLineParser;
-
-import com.intellij.openapi.util.Pair;
-import com.jetbrains.python.WordWithPosition;
-import org.jetbrains.annotations.NotNull;
-
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.Deque;
-import java.util.List;
-
-// TODO: Support options and their arguments
-
-/**
- *
- * Optparse-based commandline parses.
- * According to optparse manual, commandline should look like:
- *
.
- * You should understand difference between argument, long option, short option, and option argument before using this class.
- * It is documented in optparse manual.
- *
- *
- * This class provides not only args and options (like many other parsers do), but also position in command line
- * which may be useful when you want to mark argument somehow.
- *
- *
- * @author Ilya.Kazakevich
- */
-public final class OptParseCommandLineParser implements CommandLineParser {
- @NotNull
- @Override
- public CommandLineParseResult parse(@NotNull final List commandLineParts) throws MalformedCommandLineException {
- final Deque parts = new ArrayDeque(commandLineParts);
- if (parts.isEmpty()) {
- throw new MalformedCommandLineException("No command provided");
- }
- final WordWithPosition command = parts.pop();
- final List> resultParts = new ArrayList>();
- if (command.getText().startsWith("-")) {
- throw new MalformedCommandLineException("Command can't start with option prefix");
- }
-
- // TODO: Support option arguments!!! Not only bool options exist! Check nargs!
- for (final WordWithPosition part : parts) {
- if (part.getText().startsWith("-")) {
- // This is option!
- resultParts.add(Pair.create(CommandLinePartType.OPTION, part));
- }
- else {
- // TODO: Check optopn argument!
- resultParts.add(Pair.create(CommandLinePartType.ARGUMENT, part));
- }
- }
- return new CommandLineParseResult(command, resultParts);
- }
-}
diff --git a/python/src/com/jetbrains/python/commandLineParser/optParse/LongOptionParser.java b/python/src/com/jetbrains/python/commandLineParser/optParse/LongOptionParser.java
new file mode 100644
index 000000000000..63a173e393e3
--- /dev/null
+++ b/python/src/com/jetbrains/python/commandLineParser/optParse/LongOptionParser.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python.commandLineParser.optParse;
+
+import com.intellij.openapi.util.Pair;
+import com.jetbrains.python.commandInterface.command.Option;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.List;
+
+/**
+ * Supports --long-option-style-with=value
+ * @author Ilya.Kazakevich
+ */
+final class LongOptionParser implements OptionParser {
+ @Nullable
+ @Override
+ public Pair