embulk 0.8.27-java → 0.8.28-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +11 -1
- data/appveyor.yml +4 -4
- data/bin/embulk +33 -9
- data/build.gradle +17 -4
- data/embulk-cli/build.gradle +1 -0
- data/embulk-cli/src/main/bat/{selfrun.bat → selfrun.bat.template} +1 -2
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkArguments.java +54 -0
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkBundle.java +44 -0
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkCommandLine.java +256 -0
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkCommandLineException.java +25 -0
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkGlobalJRubyScriptingContainer.java +195 -0
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkMigrate.java +9 -5
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkRun.java +769 -0
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkSelfUpdate.java +1 -14
- data/embulk-cli/src/main/java/org/embulk/cli/EmbulkSubcommand.java +47 -0
- data/embulk-cli/src/main/java/org/embulk/cli/Main.java +12 -21
- data/embulk-cli/src/main/java/org/embulk/cli/parse/AbstractHelpLineDefinition.java +15 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/CliHelpFormatterWithHelpMessages.java +141 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/CliOptionsWithHelpMessages.java +45 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/EmbulkCommandLineHelpRequired.java +10 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/EmbulkCommandLineParseException.java +25 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/EmbulkCommandLineParser.java +183 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/HelpMessageAsCliOption.java +36 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/HelpMessageLineDefinition.java +20 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/OptionBehavior.java +39 -0
- data/embulk-cli/src/main/java/org/embulk/cli/parse/OptionDefinition.java +120 -0
- data/embulk-cli/src/main/sh/{selfrun.sh → selfrun.sh.template} +1 -1
- data/embulk-cli/src/test/java/org/embulk/cli/SelfrunTest.java +7 -3
- data/embulk-core/src/main/java/org/embulk/EmbulkRunner.java +526 -0
- data/embulk-core/src/main/java/org/embulk/EmbulkSetup.java +70 -0
- data/embulk-core/src/main/java/org/embulk/plugin/PluginClassLoader.java +3 -3
- data/embulk-docs/src/release.rst +1 -0
- data/embulk-docs/src/release/release-0.8.28.rst +14 -0
- data/lib/embulk.rb +21 -17
- data/lib/embulk/runner.rb +35 -166
- data/lib/embulk/version.rb +1 -1
- metadata +29 -11
- data/lib/embulk/command/embulk_bundle.rb +0 -47
- data/lib/embulk/command/embulk_main.rb +0 -2
- data/lib/embulk/command/embulk_run.rb +0 -418
@@ -26,12 +26,11 @@ public class EmbulkSelfUpdate
|
|
26
26
|
// TODO(dmikurube): Stop catching Exceptions here when embulk_run.rb is replaced to Java.
|
27
27
|
public void updateSelf(final String runningVersionString,
|
28
28
|
final String specifiedVersionString,
|
29
|
-
final String embulkRunRubyPathString,
|
30
29
|
final boolean isForced)
|
31
30
|
throws IOException, URISyntaxException
|
32
31
|
{
|
33
32
|
try {
|
34
|
-
updateSelfWithExceptions(runningVersionString, specifiedVersionString,
|
33
|
+
updateSelfWithExceptions(runningVersionString, specifiedVersionString, isForced);
|
35
34
|
}
|
36
35
|
catch (Throwable ex) {
|
37
36
|
ex.printStackTrace();
|
@@ -41,7 +40,6 @@ public class EmbulkSelfUpdate
|
|
41
40
|
|
42
41
|
private void updateSelfWithExceptions(final String runningVersionString,
|
43
42
|
final String specifiedVersionString,
|
44
|
-
final String embulkRunRubyPathString,
|
45
43
|
final boolean isForced)
|
46
44
|
throws IOException, URISyntaxException
|
47
45
|
{
|
@@ -52,17 +50,6 @@ public class EmbulkSelfUpdate
|
|
52
50
|
throw exceptionNoSingleJar();
|
53
51
|
}
|
54
52
|
|
55
|
-
if (embulkRunRubyPathString != null) {
|
56
|
-
final String[] splitRubyFile = embulkRunRubyPathString.split("!", 2);
|
57
|
-
if (splitRubyFile.length < 2) {
|
58
|
-
throw exceptionNoSingleJar();
|
59
|
-
}
|
60
|
-
final Path jarPathRuby = Paths.get(splitRubyFile[0]);
|
61
|
-
if (!jarPathJava.equals(jarPathRuby)) {
|
62
|
-
throw exceptionNoSingleJar();
|
63
|
-
}
|
64
|
-
}
|
65
|
-
|
66
53
|
final String targetVersionString;
|
67
54
|
if (specifiedVersionString != null) {
|
68
55
|
System.out.printf("Checking version %s...\n", specifiedVersionString);
|
@@ -0,0 +1,47 @@
|
|
1
|
+
package org.embulk.cli;
|
2
|
+
|
3
|
+
public enum EmbulkSubcommand
|
4
|
+
{
|
5
|
+
BUNDLE("bundle"),
|
6
|
+
CLEANUP("cleanup"),
|
7
|
+
EXAMPLE("example"),
|
8
|
+
EXEC("exec"),
|
9
|
+
GEM("gem"),
|
10
|
+
GUESS("guess"),
|
11
|
+
IRB("irb"),
|
12
|
+
MIGRATE("migrate"),
|
13
|
+
MKBUNDLE("mkbundle"),
|
14
|
+
NEW("new"),
|
15
|
+
PREVIEW("preview"),
|
16
|
+
RUN("run"),
|
17
|
+
SELFUPDATE("selfupdate"),
|
18
|
+
VERSION_OUT("--version"),
|
19
|
+
VERSION_ERR("-version"),
|
20
|
+
;
|
21
|
+
|
22
|
+
private EmbulkSubcommand(final String subcommand)
|
23
|
+
{
|
24
|
+
this.subcommand = subcommand;
|
25
|
+
}
|
26
|
+
|
27
|
+
public static EmbulkSubcommand of(final String subcommand)
|
28
|
+
throws EmbulkCommandLineException
|
29
|
+
{
|
30
|
+
if (!(subcommand.equals("--version")) && !(subcommand.equals("-version"))) {
|
31
|
+
for (final EmbulkSubcommand constant : values()) {
|
32
|
+
if (constant.toString().equals(subcommand)) {
|
33
|
+
return constant;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
}
|
37
|
+
throw new EmbulkCommandLineException("Unknown subcommand " + subcommand + ".");
|
38
|
+
}
|
39
|
+
|
40
|
+
@Override
|
41
|
+
public final String toString()
|
42
|
+
{
|
43
|
+
return this.subcommand;
|
44
|
+
}
|
45
|
+
|
46
|
+
private final String subcommand;
|
47
|
+
}
|
@@ -1,37 +1,28 @@
|
|
1
1
|
package org.embulk.cli;
|
2
2
|
|
3
|
-
import java.
|
3
|
+
import java.util.ArrayList;
|
4
|
+
import java.util.Collections;
|
4
5
|
|
5
6
|
public class Main
|
6
7
|
{
|
7
|
-
public static void main(String[] args)
|
8
|
+
public static void main(final String[] args)
|
8
9
|
{
|
9
|
-
|
10
|
-
|
10
|
+
final ArrayList<String> jrubyOptions = new ArrayList<String>();
|
11
|
+
|
11
12
|
int i;
|
12
|
-
for (i = 0; i < args.length; i
|
13
|
+
for (i = 0; i < args.length; ++i) {
|
13
14
|
if (args[i].startsWith("-R")) {
|
14
|
-
|
15
|
+
jrubyOptions.add(args[i].substring(2));
|
15
16
|
} else {
|
16
17
|
break;
|
17
18
|
}
|
18
19
|
}
|
19
|
-
jrubyArgs[i] = getScriptPath();
|
20
|
-
for (; i < args.length; i++) {
|
21
|
-
jrubyArgs[i+1] = args[i];
|
22
|
-
}
|
23
|
-
org.jruby.Main.main(jrubyArgs);
|
24
|
-
}
|
25
20
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
try {
|
30
|
-
resourcePath = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString() + "!/";
|
31
|
-
}
|
32
|
-
catch (URISyntaxException ex) {
|
33
|
-
resourcePath = "uri:classloader:/";
|
21
|
+
final String[] embulkArgs = new String[args.length - i];
|
22
|
+
for (int j = 0; i < args.length; ++i, ++j) {
|
23
|
+
embulkArgs[j] = args[i];
|
34
24
|
}
|
35
|
-
|
25
|
+
|
26
|
+
EmbulkBundle.checkBundle(embulkArgs, Collections.unmodifiableList(jrubyOptions));
|
36
27
|
}
|
37
28
|
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
package org.embulk.cli.parse;
|
2
|
+
|
3
|
+
import org.apache.commons.cli.Option;
|
4
|
+
|
5
|
+
/**
|
6
|
+
* AbstractHelpLineDefinition represents any one line in help messages in the command line parser.
|
7
|
+
*
|
8
|
+
* It is visible only in {@code org.embulk.cli.parse} because its interface communicates commons-cli objects.
|
9
|
+
* Dependencies on third-party libraries are to be encapsulated.
|
10
|
+
*/
|
11
|
+
abstract class AbstractHelpLineDefinition
|
12
|
+
{
|
13
|
+
// Visible only in org.embulk.cli.parse to keep commons-cli segregated from other components.
|
14
|
+
abstract Option getCliOption();
|
15
|
+
}
|
@@ -0,0 +1,141 @@
|
|
1
|
+
package org.embulk.cli.parse;
|
2
|
+
|
3
|
+
import java.io.PrintWriter;
|
4
|
+
import java.util.HashMap;
|
5
|
+
import java.util.List;
|
6
|
+
import org.apache.commons.cli.HelpFormatter;
|
7
|
+
import org.apache.commons.cli.Option;
|
8
|
+
import org.apache.commons.cli.Options;
|
9
|
+
|
10
|
+
/**
|
11
|
+
* CliHelpFormatterWithHelpMessages is an extension of commons-cli's org.apache.commons.cli.HelpFormatter.
|
12
|
+
*
|
13
|
+
* It recognizes {@code HelpMessageAsCliOption} from {@code CliOptionsWithHelpMessages} in addition to that
|
14
|
+
* {@code org.apache.commons.cli.HelpFormatter} processes ordinary {@code org.apache.commons.cli.Option}.
|
15
|
+
*
|
16
|
+
* It combines both {@code HelpMessageAsCliOption} and ordinary {@code org.apache.commons.cli.Option}, and
|
17
|
+
* builds the entire help message.
|
18
|
+
*
|
19
|
+
* It is visible only in {@code org.embulk.cli.parse} because it is an extension of a commons-cli class.
|
20
|
+
* Dependencies on third-party libraries are to be encapsulated.
|
21
|
+
*/
|
22
|
+
final class CliHelpFormatterWithHelpMessages
|
23
|
+
extends HelpFormatter
|
24
|
+
{
|
25
|
+
CliHelpFormatterWithHelpMessages(final String syntaxPrefix, final int prefixWidth)
|
26
|
+
{
|
27
|
+
super();
|
28
|
+
this.prefixWidth = prefixWidth;
|
29
|
+
this.setSyntaxPrefix(syntaxPrefix);
|
30
|
+
}
|
31
|
+
|
32
|
+
@Override
|
33
|
+
public final void printHelp(
|
34
|
+
final PrintWriter printWriter,
|
35
|
+
final int width,
|
36
|
+
final String commandLineSyntax,
|
37
|
+
final String header,
|
38
|
+
final Options options,
|
39
|
+
final int leftPaddingWidth,
|
40
|
+
final int descriptionPaddingWidth,
|
41
|
+
final String footer,
|
42
|
+
final boolean autoUsage)
|
43
|
+
{
|
44
|
+
super.printHelp(printWriter,
|
45
|
+
width,
|
46
|
+
commandLineSyntax,
|
47
|
+
header,
|
48
|
+
options,
|
49
|
+
leftPaddingWidth,
|
50
|
+
descriptionPaddingWidth,
|
51
|
+
footer,
|
52
|
+
autoUsage);
|
53
|
+
printWriter.flush();
|
54
|
+
}
|
55
|
+
|
56
|
+
@Override
|
57
|
+
protected final StringBuffer renderOptions(
|
58
|
+
final StringBuffer buffer,
|
59
|
+
final int width,
|
60
|
+
final Options options,
|
61
|
+
final int leftPaddingWidth,
|
62
|
+
final int descriptionPaddingWidth)
|
63
|
+
{
|
64
|
+
// If |options| is not the extended version, it is just delegated to the original HelpFormatter.
|
65
|
+
if (!(options instanceof CliOptionsWithHelpMessages)) {
|
66
|
+
return super.renderOptions(buffer, width, options, leftPaddingWidth, descriptionPaddingWidth);
|
67
|
+
}
|
68
|
+
|
69
|
+
final List<Option> allOptions = ((CliOptionsWithHelpMessages) options).getAllOptions();
|
70
|
+
final String leftPadding = super.createPadding(leftPaddingWidth);
|
71
|
+
final String descriptionPadding = super.createPadding(descriptionPaddingWidth);
|
72
|
+
|
73
|
+
int maxOptionLinePrefixLength = 0;
|
74
|
+
final HashMap<Option, String> optionLinePrefixes = new HashMap<Option, String>();
|
75
|
+
|
76
|
+
// The first pass: builds all "prefixes" of option lines, and measures their length.
|
77
|
+
for (final Option option : allOptions) {
|
78
|
+
final StringBuilder optionLinePrefixBuilder = new StringBuilder();
|
79
|
+
|
80
|
+
if (!(option instanceof HelpMessageAsCliOption)) {
|
81
|
+
optionLinePrefixBuilder.append(leftPadding);
|
82
|
+
if (option.getOpt() == null) {
|
83
|
+
optionLinePrefixBuilder.append(" ").append(getLongOptPrefix()).append(option.getLongOpt());
|
84
|
+
}
|
85
|
+
else {
|
86
|
+
optionLinePrefixBuilder.append(getOptPrefix()).append(option.getOpt());
|
87
|
+
if (option.hasLongOpt()) {
|
88
|
+
optionLinePrefixBuilder.append(", ").append(getLongOptPrefix()).append(option.getLongOpt());
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
if (option.hasArg()) {
|
93
|
+
final String argName = option.getArgName();
|
94
|
+
if (argName != null && argName.length() == 0) {
|
95
|
+
optionLinePrefixBuilder.append(' ');
|
96
|
+
}
|
97
|
+
else {
|
98
|
+
optionLinePrefixBuilder.append(option.hasLongOpt() ? getLongOptSeparator() : " ");
|
99
|
+
optionLinePrefixBuilder.append(argName != null ? option.getArgName() : getArgName());
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
optionLinePrefixes.put(option, optionLinePrefixBuilder.toString());
|
104
|
+
if (maxOptionLinePrefixLength < optionLinePrefixBuilder.length()) {
|
105
|
+
maxOptionLinePrefixLength = optionLinePrefixBuilder.length();
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
if (maxOptionLinePrefixLength < this.prefixWidth) {
|
111
|
+
maxOptionLinePrefixLength = this.prefixWidth;
|
112
|
+
}
|
113
|
+
|
114
|
+
// The second pass: builds all lines both from options and help messages.
|
115
|
+
for (final Option option : allOptions) {
|
116
|
+
if (option instanceof HelpMessageAsCliOption) {
|
117
|
+
buffer.append(option.toString());
|
118
|
+
}
|
119
|
+
else {
|
120
|
+
final StringBuilder lineBuilder = new StringBuilder();
|
121
|
+
lineBuilder.append(optionLinePrefixes.get(option));
|
122
|
+
if (lineBuilder.length() < maxOptionLinePrefixLength) {
|
123
|
+
lineBuilder.append(super.createPadding(maxOptionLinePrefixLength - lineBuilder.length()));
|
124
|
+
}
|
125
|
+
lineBuilder.append(descriptionPadding);
|
126
|
+
|
127
|
+
if (option.getDescription() != null) {
|
128
|
+
lineBuilder.append(option.getDescription());
|
129
|
+
}
|
130
|
+
super.renderWrappedText(buffer,
|
131
|
+
width,
|
132
|
+
maxOptionLinePrefixLength + descriptionPaddingWidth,
|
133
|
+
lineBuilder.toString());
|
134
|
+
}
|
135
|
+
buffer.append(super.getNewLine());
|
136
|
+
}
|
137
|
+
return buffer;
|
138
|
+
}
|
139
|
+
|
140
|
+
private final int prefixWidth;
|
141
|
+
}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
package org.embulk.cli.parse;
|
2
|
+
|
3
|
+
import java.util.ArrayList;
|
4
|
+
import java.util.Collections;
|
5
|
+
import java.util.List;
|
6
|
+
import org.apache.commons.cli.Option;
|
7
|
+
import org.apache.commons.cli.Options;
|
8
|
+
|
9
|
+
/**
|
10
|
+
* CliOptionsWithHelpMessages is an extension of commons-cli's org.apache.commons.cli.Options.
|
11
|
+
*
|
12
|
+
* It just recognizes {@code HelpMessageAsCliOption} while others are same with {@code org.apache.commons.cli.Options}.
|
13
|
+
* {@code HelpMessageAsCliOption} is added just as a help message line, but ignored as a command line option.
|
14
|
+
*
|
15
|
+
* It is visible only in |org.embulk.cli.parse| because it is an extension of a commons-cli class.
|
16
|
+
* Dependencies on third-party libraries are to be encapsulated.
|
17
|
+
*/
|
18
|
+
final class CliOptionsWithHelpMessages
|
19
|
+
extends Options
|
20
|
+
{
|
21
|
+
CliOptionsWithHelpMessages()
|
22
|
+
{
|
23
|
+
this.allOptions = new ArrayList<Option>();
|
24
|
+
}
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Adds commons-cli's {@code org.apache.commons.cli.Option}.
|
28
|
+
*/
|
29
|
+
@Override
|
30
|
+
public final Options addOption(final Option option)
|
31
|
+
{
|
32
|
+
this.allOptions.add(option);
|
33
|
+
if (option instanceof HelpMessageAsCliOption) {
|
34
|
+
return this;
|
35
|
+
}
|
36
|
+
return super.addOption(option);
|
37
|
+
}
|
38
|
+
|
39
|
+
final List<Option> getAllOptions()
|
40
|
+
{
|
41
|
+
return Collections.unmodifiableList(this.allOptions);
|
42
|
+
}
|
43
|
+
|
44
|
+
private final ArrayList<Option> allOptions;
|
45
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
package org.embulk.cli.parse;
|
2
|
+
|
3
|
+
public final class EmbulkCommandLineParseException
|
4
|
+
extends Exception
|
5
|
+
{
|
6
|
+
protected EmbulkCommandLineParseException()
|
7
|
+
{
|
8
|
+
super();
|
9
|
+
}
|
10
|
+
|
11
|
+
public EmbulkCommandLineParseException(final String message)
|
12
|
+
{
|
13
|
+
super(message);
|
14
|
+
}
|
15
|
+
|
16
|
+
public EmbulkCommandLineParseException(final Throwable cause)
|
17
|
+
{
|
18
|
+
super(cause);
|
19
|
+
}
|
20
|
+
|
21
|
+
public EmbulkCommandLineParseException(final String message, final Throwable cause)
|
22
|
+
{
|
23
|
+
super(message, cause);
|
24
|
+
}
|
25
|
+
}
|
@@ -0,0 +1,183 @@
|
|
1
|
+
package org.embulk.cli.parse;
|
2
|
+
|
3
|
+
import java.io.PrintStream;
|
4
|
+
import java.io.PrintWriter;
|
5
|
+
import java.util.ArrayList;
|
6
|
+
import java.util.Collections;
|
7
|
+
import java.util.HashMap;
|
8
|
+
import java.util.List;
|
9
|
+
import org.apache.commons.cli.CommandLine;
|
10
|
+
import org.apache.commons.cli.DefaultParser;
|
11
|
+
import org.apache.commons.cli.Option;
|
12
|
+
import org.apache.commons.cli.ParseException;
|
13
|
+
import org.embulk.cli.EmbulkCommandLine;
|
14
|
+
|
15
|
+
public class EmbulkCommandLineParser
|
16
|
+
{
|
17
|
+
private EmbulkCommandLineParser(
|
18
|
+
final String usage,
|
19
|
+
final List<AbstractHelpLineDefinition> helpLineDefinitions,
|
20
|
+
final int minArgs,
|
21
|
+
final int maxArgs,
|
22
|
+
final int width)
|
23
|
+
{
|
24
|
+
this.usage = usage;
|
25
|
+
this.helpLineDefinitions = Collections.unmodifiableList(helpLineDefinitions);
|
26
|
+
this.minArgs = minArgs;
|
27
|
+
this.maxArgs = maxArgs;
|
28
|
+
this.width = width;
|
29
|
+
|
30
|
+
this.cliOptions = new CliOptionsWithHelpMessages();
|
31
|
+
|
32
|
+
final HashMap<Option, OptionDefinition> optionDefinitionFromCliOption = new HashMap<Option, OptionDefinition>();
|
33
|
+
for (final AbstractHelpLineDefinition definition : this.helpLineDefinitions) {
|
34
|
+
cliOptions.addOption(definition.getCliOption());
|
35
|
+
if (definition instanceof OptionDefinition) {
|
36
|
+
optionDefinitionFromCliOption.put(definition.getCliOption(), ((OptionDefinition) definition));
|
37
|
+
}
|
38
|
+
}
|
39
|
+
this.optionDefinitionFromCliOption = optionDefinitionFromCliOption;
|
40
|
+
}
|
41
|
+
|
42
|
+
public static class Builder
|
43
|
+
{
|
44
|
+
private Builder()
|
45
|
+
{
|
46
|
+
this.mainUsage = null;
|
47
|
+
this.additionalUsage = new StringBuilder();
|
48
|
+
this.helpLineDefinitions = new ArrayList<AbstractHelpLineDefinition>();
|
49
|
+
this.minArgs = 0;
|
50
|
+
this.maxArgs = Integer.MAX_VALUE;
|
51
|
+
this.width = 74;
|
52
|
+
}
|
53
|
+
|
54
|
+
public EmbulkCommandLineParser build()
|
55
|
+
{
|
56
|
+
return new EmbulkCommandLineParser(
|
57
|
+
this.mainUsage + this.additionalUsage.toString(),
|
58
|
+
this.helpLineDefinitions,
|
59
|
+
this.minArgs,
|
60
|
+
this.maxArgs,
|
61
|
+
this.width);
|
62
|
+
}
|
63
|
+
|
64
|
+
public Builder setMainUsage(final String mainUsage)
|
65
|
+
{
|
66
|
+
this.mainUsage = mainUsage;
|
67
|
+
return this;
|
68
|
+
}
|
69
|
+
|
70
|
+
public Builder addUsage(final String line)
|
71
|
+
{
|
72
|
+
this.additionalUsage.append(System.getProperty("line.separator"));
|
73
|
+
this.additionalUsage.append(line);
|
74
|
+
return this;
|
75
|
+
}
|
76
|
+
|
77
|
+
public Builder addOptionDefinition(final OptionDefinition optionDefinition)
|
78
|
+
{
|
79
|
+
this.helpLineDefinitions.add(optionDefinition);
|
80
|
+
return this;
|
81
|
+
}
|
82
|
+
|
83
|
+
public Builder addHelpMessageLine(final String message)
|
84
|
+
{
|
85
|
+
this.helpLineDefinitions.add(new HelpMessageLineDefinition(message));
|
86
|
+
return this;
|
87
|
+
}
|
88
|
+
|
89
|
+
public Builder setArgumentsRange(final int minArgs, final int maxArgs)
|
90
|
+
{
|
91
|
+
this.minArgs = minArgs;
|
92
|
+
this.maxArgs = maxArgs;
|
93
|
+
return this;
|
94
|
+
}
|
95
|
+
|
96
|
+
public Builder setWidth(final int width)
|
97
|
+
{
|
98
|
+
this.width = width;
|
99
|
+
return this;
|
100
|
+
}
|
101
|
+
|
102
|
+
private String mainUsage;
|
103
|
+
private final StringBuilder additionalUsage;
|
104
|
+
private final ArrayList<AbstractHelpLineDefinition> helpLineDefinitions;
|
105
|
+
private int minArgs;
|
106
|
+
private int maxArgs;
|
107
|
+
private int width;
|
108
|
+
}
|
109
|
+
|
110
|
+
public static Builder builder()
|
111
|
+
{
|
112
|
+
return new Builder();
|
113
|
+
}
|
114
|
+
|
115
|
+
public EmbulkCommandLine parse(final List<String> argsEmbulk,
|
116
|
+
final PrintWriter helpPrintWriter,
|
117
|
+
final PrintWriter errorPrintWriter)
|
118
|
+
throws EmbulkCommandLineParseException, EmbulkCommandLineHelpRequired
|
119
|
+
{
|
120
|
+
final CommandLine cliCommandLine;
|
121
|
+
try {
|
122
|
+
cliCommandLine = new DefaultParser().parse(
|
123
|
+
this.cliOptions, argsEmbulk.toArray(new String[argsEmbulk.size()]));
|
124
|
+
}
|
125
|
+
catch (ParseException ex) {
|
126
|
+
throw new EmbulkCommandLineParseException(ex.getMessage(), ex);
|
127
|
+
}
|
128
|
+
|
129
|
+
for (final Option cliOptionSpecified : cliCommandLine.getOptions()) {
|
130
|
+
final OptionDefinition optionDefinitionSpecified =
|
131
|
+
this.optionDefinitionFromCliOption.get(cliOptionSpecified);
|
132
|
+
|
133
|
+
if (optionDefinitionSpecified.printsHelp()) {
|
134
|
+
throw new EmbulkCommandLineHelpRequired();
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
final List<String> arguments = cliCommandLine.getArgList();
|
139
|
+
if (arguments.size() < this.minArgs) {
|
140
|
+
throw new EmbulkCommandLineParseException("Too few arguments.");
|
141
|
+
}
|
142
|
+
if (arguments.size() > this.maxArgs) {
|
143
|
+
throw new EmbulkCommandLineParseException("Too many arguments;");
|
144
|
+
}
|
145
|
+
|
146
|
+
final EmbulkCommandLine.Builder commandLineBuilder = EmbulkCommandLine.builder();
|
147
|
+
commandLineBuilder.addArguments(arguments);
|
148
|
+
for (final Option cliOptionSpecified : cliCommandLine.getOptions()) {
|
149
|
+
final OptionDefinition optionDefinitionSpecified =
|
150
|
+
this.optionDefinitionFromCliOption.get(cliOptionSpecified);
|
151
|
+
optionDefinitionSpecified.behave(commandLineBuilder, cliOptionSpecified.getValue());
|
152
|
+
}
|
153
|
+
|
154
|
+
return commandLineBuilder.build();
|
155
|
+
}
|
156
|
+
|
157
|
+
public final void printHelp(final PrintStream printStream)
|
158
|
+
{
|
159
|
+
this.printHelp(new PrintWriter(printStream));
|
160
|
+
}
|
161
|
+
|
162
|
+
public void printHelp(final PrintWriter printWriter)
|
163
|
+
{
|
164
|
+
final CliHelpFormatterWithHelpMessages helpFormatter = new CliHelpFormatterWithHelpMessages("Usage: ", 32);
|
165
|
+
helpFormatter.printHelp(printWriter, // PrintWriter pw
|
166
|
+
this.width, // int width
|
167
|
+
this.usage, // String cmdLineSyntax
|
168
|
+
"", // String header
|
169
|
+
this.cliOptions, // Options options
|
170
|
+
4, // int leftPad
|
171
|
+
5, // int descPad
|
172
|
+
"" // String footer
|
173
|
+
);
|
174
|
+
}
|
175
|
+
|
176
|
+
private final String usage;
|
177
|
+
private final List<AbstractHelpLineDefinition> helpLineDefinitions;
|
178
|
+
private final int minArgs;
|
179
|
+
private final int maxArgs;
|
180
|
+
private final int width;
|
181
|
+
private final CliOptionsWithHelpMessages cliOptions;
|
182
|
+
private final HashMap<Option, OptionDefinition> optionDefinitionFromCliOption;
|
183
|
+
}
|