Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap CLI Options API to avoid direct dependency on JOpt lib in comma…
…nds #17
- Loading branch information
Showing
19 changed files
with
263 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.nhl.bootique.cli; | ||
|
||
/** | ||
* @since 0.12 | ||
*/ | ||
public interface OptionBuilder { | ||
|
||
OptionBuilder requiresArgument(String description); | ||
|
||
OptionBuilder mayTakeArgument(String description); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.nhl.bootique.cli; | ||
|
||
import java.io.Writer; | ||
import java.util.Collection; | ||
|
||
/** | ||
* An object that represents a set of command line options passed to Booqtie | ||
* app. | ||
* | ||
* @since 0.12 | ||
*/ | ||
public interface Options { | ||
|
||
void printHelp(Writer out); | ||
|
||
boolean hasOption(String name); | ||
|
||
/** | ||
* Returns a collection of String values for the specified option name. | ||
* | ||
* @param name | ||
* option name | ||
* @return a potentially empty collection of CLI values for a given option. | ||
*/ | ||
Collection<String> stringsFor(String name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.nhl.bootique.cli; | ||
|
||
/** | ||
* @since 0.12 | ||
*/ | ||
public interface OptionsBuilder { | ||
|
||
OptionBuilder add(String option, String description); | ||
|
||
OptionBuilder addHelp(String option, String description); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/nhl/bootique/command/FailoverHelpCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/nhl/bootique/jopt/JoptOptionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.nhl.bootique.jopt; | ||
|
||
import com.nhl.bootique.cli.OptionBuilder; | ||
|
||
import joptsimple.OptionSpecBuilder; | ||
|
||
/** | ||
* @since 0.12 | ||
*/ | ||
class JoptOptionBuilder implements OptionBuilder { | ||
|
||
private OptionSpecBuilder optionSpecBuilder; | ||
|
||
JoptOptionBuilder(OptionSpecBuilder optionSpecBuilder) { | ||
this.optionSpecBuilder = optionSpecBuilder; | ||
} | ||
|
||
@Override | ||
public OptionBuilder mayTakeArgument(String description) { | ||
optionSpecBuilder.withOptionalArg().describedAs(description); | ||
return this; | ||
} | ||
|
||
@Override | ||
public OptionBuilder requiresArgument(String description) { | ||
optionSpecBuilder.withRequiredArg().describedAs(description); | ||
return this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.nhl.bootique.jopt; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.util.Collection; | ||
|
||
import com.nhl.bootique.cli.Options; | ||
import com.nhl.bootique.log.BootLogger; | ||
|
||
import joptsimple.OptionParser; | ||
import joptsimple.OptionSet; | ||
|
||
/** | ||
* A facade to the command-line option parsing framework. | ||
*/ | ||
public class JoptOptions implements Options { | ||
|
||
private OptionParser parser; | ||
private OptionSet optionSet; | ||
private BootLogger bootLogger; | ||
|
||
public JoptOptions(BootLogger bootLogger, OptionParser parser, OptionSet parsed) { | ||
this.parser = parser; | ||
this.optionSet = parsed; | ||
this.bootLogger = bootLogger; | ||
} | ||
|
||
@Override | ||
public void printHelp(Writer out) { | ||
try { | ||
parser.printHelpOn(out); | ||
} catch (IOException e) { | ||
bootLogger.stderr("Error printing help", e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasOption(String optionName) { | ||
return optionSet.has(optionName); | ||
} | ||
|
||
@Override | ||
public Collection<String> stringsFor(String optionName) { | ||
return optionSet.valuesOf(optionName).stream().map(o -> String.valueOf(o)).collect(toList()); | ||
} | ||
} |
Oops, something went wrong.