Beautify kotlinc -help output
This commit is contained in:
+9
-1
@@ -18,8 +18,10 @@ package org.jetbrains.jet.cli.common.arguments;
|
||||
|
||||
import com.intellij.util.SmartList;
|
||||
import com.sampullara.cli.Argument;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.cli.common.arguments.CommonArgumentConstants.SUPPRESS_WARNINGS;
|
||||
|
||||
public abstract class CommonCompilerArguments {
|
||||
@@ -35,7 +37,8 @@ public abstract class CommonCompilerArguments {
|
||||
@Argument(value = "help", alias = "h", description = "Print a synopsis of standard options")
|
||||
public boolean help;
|
||||
|
||||
@Argument(value = "suppress", description = "Suppress compiler messages by severity (" + SUPPRESS_WARNINGS + ")")
|
||||
@Argument(value = "suppress", description = "Suppress all compiler warnings")
|
||||
@ValueDescription(SUPPRESS_WARNINGS)
|
||||
public String suppress;
|
||||
|
||||
@Argument(value = "printArgs", description = "Print command line arguments")
|
||||
@@ -47,6 +50,11 @@ public abstract class CommonCompilerArguments {
|
||||
return SUPPRESS_WARNINGS.equalsIgnoreCase(suppress);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String executableScriptFileName() {
|
||||
return "kotlinc";
|
||||
}
|
||||
|
||||
// Used only for serialize and deserialize settings. Don't use in other places!
|
||||
public static final class DummyImpl extends CommonCompilerArguments {}
|
||||
}
|
||||
|
||||
+17
-4
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.jet.cli.common.arguments;
|
||||
|
||||
import com.sampullara.cli.Argument;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import static org.jetbrains.jet.cli.common.arguments.K2JsArgumentConstants.CALL;
|
||||
@@ -27,28 +28,40 @@ import static org.jetbrains.jet.cli.common.arguments.K2JsArgumentConstants.NO_CA
|
||||
*/
|
||||
public class K2JSCompilerArguments extends CommonCompilerArguments {
|
||||
@Argument(value = "output", description = "Output file path")
|
||||
@ValueDescription("<path>")
|
||||
public String outputFile;
|
||||
|
||||
@Argument(value = "libraryFiles", description = "Path to zipped lib sources or kotlin files")
|
||||
@Argument(value = "libraryFiles", description = "Path to zipped library sources or kotlin files separated by commas")
|
||||
@ValueDescription("<path[,]>")
|
||||
public String[] libraryFiles;
|
||||
|
||||
@Argument(value = "sourceFiles", description = "Source files (dir or file)")
|
||||
@Argument(value = "sourceFiles", description = "Source files or directories separated by commas")
|
||||
@ValueDescription("<path[,]>")
|
||||
public String[] sourceFiles;
|
||||
|
||||
@Argument(value = "sourcemap", description = "Generate SourceMap")
|
||||
public boolean sourcemap;
|
||||
|
||||
@Argument(value = "target", description = "Generate JS files for specific ECMA version (only ECMA 5 is supported)")
|
||||
@ValueDescription("<version>")
|
||||
public String target;
|
||||
|
||||
@Nullable
|
||||
@Argument(value = "main", description = "Whether a main function should be called; either '" + CALL +
|
||||
"' or '" + NO_CALL + "', default '" + CALL + "' (main function will be auto detected)")
|
||||
@Argument(value = "main", description = "Whether a main function should be called; default '" + CALL + "' (main function will be auto detected)")
|
||||
@ValueDescription("{" + CALL + "," + NO_CALL + "}")
|
||||
public String main;
|
||||
|
||||
@Argument(value = "outputPrefix", description = "Path to file which will be added to the beginning of output file")
|
||||
@ValueDescription("<path>")
|
||||
public String outputPrefix;
|
||||
|
||||
@Argument(value = "outputPostfix", description = "Path to file which will be added to the end of output file")
|
||||
@ValueDescription("<path>")
|
||||
public String outputPostfix;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String executableScriptFileName() {
|
||||
return "kotlinc-js";
|
||||
}
|
||||
}
|
||||
|
||||
+16
-1
@@ -19,6 +19,7 @@
|
||||
package org.jetbrains.jet.cli.common.arguments;
|
||||
|
||||
import com.sampullara.cli.Argument;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Command line arguments for K2JVMCompiler
|
||||
@@ -26,18 +27,23 @@ import com.sampullara.cli.Argument;
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public class K2JVMCompilerArguments extends CommonCompilerArguments {
|
||||
@Argument(value = "src", description = "Source file or directory (allows many paths separated by the system path separator)")
|
||||
@ValueDescription("<path>")
|
||||
public String src;
|
||||
|
||||
@Argument(value = "jar", description = "Resulting .jar file path")
|
||||
@ValueDescription("<path>")
|
||||
public String jar;
|
||||
|
||||
@Argument(value = "output", description = "Output directory path for .class files")
|
||||
@ValueDescription("<path>")
|
||||
public String outputDir;
|
||||
|
||||
@Argument(value = "classpath", description = "Paths where to find user class files")
|
||||
@ValueDescription("<path>")
|
||||
public String classpath;
|
||||
|
||||
@Argument(value = "annotations", description = "Paths to external annotations")
|
||||
@ValueDescription("<path>")
|
||||
public String annotations;
|
||||
|
||||
@Argument(value = "includeRuntime", description = "Include Kotlin runtime in to resulting .jar")
|
||||
@@ -59,14 +65,23 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
|
||||
public boolean notNullParamAssertions;
|
||||
|
||||
@Argument(value = "module", description = "Path to the module file to compile")
|
||||
@ValueDescription("<path>")
|
||||
public String module;
|
||||
|
||||
@Argument(value = "script", description = "Evaluate the script file")
|
||||
public boolean script;
|
||||
|
||||
@Argument(value = "kotlinHome", description = "Path to Kotlin compiler home directory, used for annotations and runtime libraries discovery")
|
||||
@ValueDescription("<path>")
|
||||
public String kotlinHome;
|
||||
|
||||
@Argument(value = "inline", description = "Inlining mode: on/off (default is on)")
|
||||
@Argument(value = "inline", description = "Inlining mode (default is on)")
|
||||
@ValueDescription("{on,off}")
|
||||
public String inline;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String executableScriptFileName() {
|
||||
return "kotlinc-jvm";
|
||||
}
|
||||
}
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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 org.jetbrains.jet.cli.common.arguments;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ValueDescription {
|
||||
@NotNull
|
||||
String value();
|
||||
}
|
||||
@@ -20,94 +20,58 @@ import com.sampullara.cli.Argument;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.cli.common.arguments.CommonCompilerArguments;
|
||||
import org.jetbrains.jet.cli.common.arguments.ValueDescription;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Usage {
|
||||
private final PrintStream out;
|
||||
|
||||
private Usage(@NotNull PrintStream out) {
|
||||
this.out = out;
|
||||
}
|
||||
// The magic number 29 corresponds to the similar padding width in javac and scalac command line compilers
|
||||
private static final int OPTION_NAME_PADDING_WIDTH = 29;
|
||||
|
||||
public static void print(@NotNull PrintStream target, @NotNull CommonCompilerArguments arguments) {
|
||||
new Usage(target).print(arguments);
|
||||
}
|
||||
|
||||
private void print(@NotNull CommonCompilerArguments arguments) {
|
||||
Class<?> clazz = arguments.getClass();
|
||||
out.println("Usage: " + clazz.getName());
|
||||
for (Class<?> currentClass = clazz; currentClass != null; currentClass = currentClass.getSuperclass()) {
|
||||
for (Field field : currentClass.getDeclaredFields()) {
|
||||
Argument argument = field.getAnnotation(Argument.class);
|
||||
if (argument == null) continue;
|
||||
|
||||
try {
|
||||
printFieldUsage(field, field.get(arguments), argument);
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException("Could not use the field " + field + " as an argument field", e);
|
||||
target.println("Usage: " + arguments.executableScriptFileName() + " <options> <source files>");
|
||||
target.println("where possible options include:");
|
||||
for (Class<?> clazz = arguments.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
String usage = fieldUsage(field);
|
||||
if (usage != null) {
|
||||
target.println(usage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void printFieldUsage(@NotNull Field field, @Nullable Object defaultValue, @NotNull Argument argument) {
|
||||
@Nullable
|
||||
private static String fieldUsage(@NotNull Field field) {
|
||||
Argument argument = field.getAnnotation(Argument.class);
|
||||
if (argument == null) return null;
|
||||
ValueDescription description = field.getAnnotation(ValueDescription.class);
|
||||
|
||||
String prefix = argument.prefix();
|
||||
Class<?> type = field.getType();
|
||||
String description = argument.description();
|
||||
|
||||
StringBuilder sb = new StringBuilder(" ");
|
||||
sb.append(prefix);
|
||||
if (argument.value().equals("")) {
|
||||
if (argument.value().isEmpty()) {
|
||||
sb.append(field.getName());
|
||||
}
|
||||
else {
|
||||
sb.append(argument.value());
|
||||
}
|
||||
if (!argument.alias().equals("")) {
|
||||
if (!argument.alias().isEmpty()) {
|
||||
sb.append(" (");
|
||||
sb.append(prefix);
|
||||
sb.append(argument.alias());
|
||||
sb.append(")");
|
||||
}
|
||||
if (type == Boolean.TYPE || type == Boolean.class) {
|
||||
sb.append(" [flag] ");
|
||||
sb.append(description);
|
||||
if (description != null) {
|
||||
sb.append(" ");
|
||||
sb.append(description.value());
|
||||
}
|
||||
else {
|
||||
sb.append(" [");
|
||||
if (type.isArray()) {
|
||||
sb.append(type.getComponentType().getSimpleName());
|
||||
sb.append("[");
|
||||
sb.append(argument.delimiter());
|
||||
sb.append("]");
|
||||
}
|
||||
else {
|
||||
sb.append(type.getSimpleName());
|
||||
}
|
||||
sb.append("] ");
|
||||
sb.append(description);
|
||||
if (defaultValue != null) {
|
||||
sb.append(" (");
|
||||
if (type.isArray()) {
|
||||
int len = Array.getLength(defaultValue);
|
||||
List<Object> list = new ArrayList<Object>(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
list.add(Array.get(defaultValue, i));
|
||||
}
|
||||
sb.append(list);
|
||||
}
|
||||
else {
|
||||
sb.append(defaultValue);
|
||||
}
|
||||
sb.append(")");
|
||||
}
|
||||
while (sb.length() < OPTION_NAME_PADDING_WIDTH) {
|
||||
sb.append(" ");
|
||||
}
|
||||
out.println(sb);
|
||||
sb.append(argument.description());
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user