Skip to content

Commit

Permalink
Implemented command line option groups
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-kraemer committed Oct 23, 2013
1 parent 78ba66a commit b8d0589
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 23 deletions.
20 changes: 13 additions & 7 deletions src/main/java/de/undercouch/citeproc/CSLTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
import de.undercouch.citeproc.helper.oauth.FileAuthenticationStore;
import de.undercouch.citeproc.helper.oauth.RequestException;
import de.undercouch.citeproc.helper.oauth.UnauthorizedException;
import de.undercouch.citeproc.helper.tool.Option;
import de.undercouch.citeproc.helper.tool.Option.ArgumentType;
import de.undercouch.citeproc.helper.tool.OptionBuilder;
import de.undercouch.citeproc.helper.tool.OptionGroup;
import de.undercouch.citeproc.helper.tool.OptionParser;
import de.undercouch.citeproc.helper.tool.OptionParserException;
import de.undercouch.citeproc.helper.tool.Value;
Expand Down Expand Up @@ -99,12 +99,9 @@ private static enum FileFormat {
/**
* A list of possible command line options for this tool
*/
private static List<Option<OID>> options = new OptionBuilder<OID>()
private static OptionGroup<OID> options = new OptionBuilder<OID>()
.add(OID.BIBLIOGRAPHY, "bibliography", "b", "input bibliography FILE (*.bib, *.json)",
"FILE", ArgumentType.STRING)
.add(OID.MENDELEY, "mendeley", "read input bibliography from Mendeley Web")
.add(OID.MENDELEY_SYNC, "mendeley-sync", "synchronize with Mendeley Web, "
+ "implies --mendeley")
.add(OID.STYLE, "style", "s", "citation STYLE name (default: ieee)",
"STYLE", ArgumentType.STRING)
.add(OID.LOCALE, "locale", "l", "citation LOCALE (default: en-US)",
Expand All @@ -113,8 +110,17 @@ private static enum FileFormat {
"FORMAT", ArgumentType.STRING)
.add(OID.CITATION, "citation", "c", "generate citations and not a bibliography")
.add(OID.LIST, "list", "display sorted list of available citation IDs")
.add(OID.HELP, "help", "h", "display this help and exit")
.add(OID.VERSION, "version", "V", "output version information and exit")
.add(new OptionBuilder<OID>("Mendeley:")
.add(OID.MENDELEY, "mendeley", "read input bibliography from Mendeley Web")
.add(OID.MENDELEY_SYNC, "mendeley-sync", "synchronize with Mendeley Web, "
+ "implies --mendeley")
.build()
)
.add(new OptionBuilder<OID>("Miscellaneous:")
.add(OID.HELP, "help", "h", "display this help and exit")
.add(OID.VERSION, "version", "V", "output version information and exit")
.build()
)
.build();

/**
Expand Down
43 changes: 33 additions & 10 deletions src/main/java/de/undercouch/citeproc/helper/tool/OptionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,31 @@

package de.undercouch.citeproc.helper.tool;

import java.util.ArrayList;
import java.util.List;

import de.undercouch.citeproc.helper.tool.Option.ArgumentType;

/**
* Builder for a list of command line options
* Builder for a group of command line options
* @author Michel Kraemer
* @param <T> identifier type
*/
public class OptionBuilder<T> {
private List<Option<T>> options = new ArrayList<Option<T>>();
private final OptionGroup<T> options;

/**
* Creates a new option builder
*/
public OptionBuilder() {
options = new OptionGroup<T>();
}

/**
* Creates a new option builder
* @param groupName the name of the option group the builder will create
* when calling {@link #build()}
*/
public OptionBuilder(String groupName) {
options = new OptionGroup<T>(groupName);
}

/**
* Adds a new option without arguments
Expand All @@ -35,7 +48,7 @@ public class OptionBuilder<T> {
* @return this option builder
*/
public OptionBuilder<T> add(T id, String longName, String description) {
options.add(new Option<T>(id, longName, description));
options.addOption(new Option<T>(id, longName, description));
return this;
}

Expand All @@ -49,7 +62,7 @@ public OptionBuilder<T> add(T id, String longName, String description) {
* @return this option builder
*/
public OptionBuilder<T> add(T id, String longName, String shortName, String description) {
options.add(new Option<T>(id, longName, shortName, description));
options.addOption(new Option<T>(id, longName, shortName, description));
return this;
}

Expand All @@ -68,14 +81,24 @@ public OptionBuilder<T> add(T id, String longName, String shortName, String desc
*/
public OptionBuilder<T> add(T id, String longName, String shortName, String description,
String argumentName, ArgumentType argumentType) {
options.add(new Option<T>(id, longName, shortName, description, argumentName, argumentType));
options.addOption(new Option<T>(id, longName, shortName, description, argumentName, argumentType));
return this;
}

/**
* Adds a new option group
* @param group the group to add
* @return this option builder
*/
public OptionBuilder<T> add(OptionGroup<T> group) {
options.addChild(group);
return this;
}

/**
* @return the built list of options
* @return the built group of options
*/
public List<Option<T>> build() {
public OptionGroup<T> build() {
return options;
}
}
93 changes: 93 additions & 0 deletions src/main/java/de/undercouch/citeproc/helper/tool/OptionGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2013 Michel Kraemer
//
// 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 de.undercouch.citeproc.helper.tool;

import java.util.ArrayList;
import java.util.List;

/**
* A group of command line options
* @author Michel Kraemer
* @param <T> identifier type
*/
public class OptionGroup<T> {
private final String name;
private final List<Option<T>> options = new ArrayList<Option<T>>();
private final List<OptionGroup<T>> children = new ArrayList<OptionGroup<T>>();

/**
* Creates a new option group with no name
*/
public OptionGroup() {
this.name = "";
}

/**
* Creates a new option group
* @param name the group's name
*/
public OptionGroup(String name) {
this.name = name;
}

/**
* @return the group's name
*/
public String getName() {
return name;
}

/**
* @return the options in this group
*/
public List<Option<T>> getOptions() {
return options;
}

/**
* @return a flat list of options in this group and all options from all children
*/
public List<Option<T>> getFlatOptions() {
List<Option<T>> result = new ArrayList<Option<T>>();
result.addAll(getOptions());
for (OptionGroup<T> g : getChildren()) {
result.addAll(g.getFlatOptions());
}
return result;
}

/**
* @return the group's children
*/
public List<OptionGroup<T>> getChildren() {
return children;
}

/**
* Adds an option to this group
* @param o the option to add
*/
public void addOption(Option<T> o) {
options.add(o);
}

/**
* Adds a child to this group
* @param c the child to add
*/
public void addChild(OptionGroup<T> c) {
children.add(c);
}
}
28 changes: 22 additions & 6 deletions src/main/java/de/undercouch/citeproc/helper/tool/OptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public class OptionParser {
* @param out destination stream
*/
public static <T> void usage(String command, String description,
List<Option<T>> options, PrintStream out) {
OptionGroup<T> options, PrintStream out) {
out.println("Usage: " + command);
out.println(description);
out.println();

//calculate column widths
int firstColumnWidth = 0;
int secondColumnWidth = 0;
for (Option<T> o : options) {
for (Option<T> o : options.getFlatOptions()) {
//calculate width of first column (short name)
if (o.getShortName() != null) {
int fcw = o.getShortName().length() + 1;
Expand All @@ -63,7 +63,18 @@ public static <T> void usage(String command, String description,
}

//output options
for (Option<T> o : options) {
printOptions(options, out, firstColumnWidth, secondColumnWidth);
}

private static <T> void printOptions(OptionGroup<T> options, PrintStream out,
int firstColumnWidth, int secondColumnWidth) {
//print group name (if any)
if (options.getName() != null && !options.getName().isEmpty()) {
System.out.println();
System.out.println(options.getName());
}

for (Option<T> o : options.getOptions()) {
out.print(" ");

//output short name
Expand Down Expand Up @@ -115,6 +126,11 @@ public static <T> void usage(String command, String description,
}
out.println(desc);
}

//print children
for (OptionGroup<T> c : options.getChildren()) {
printOptions(c, out, firstColumnWidth, secondColumnWidth);
}
}

/**
Expand All @@ -128,7 +144,7 @@ public static <T> void usage(String command, String description,
* @throws MissingArgumentException if an option misses a required argument
* @throws InvalidOptionException if one of the arguments is unknown
*/
public static <T> List<Value<T>> parse(String[] args, List<Option<T>> options,
public static <T> List<Value<T>> parse(String[] args, OptionGroup<T> options,
T def) throws MissingArgumentException, InvalidOptionException {
List<Value<T>> result = new ArrayList<Value<T>>();
for (int i = 0; i < args.length; ++i) {
Expand All @@ -139,7 +155,7 @@ public static <T> List<Value<T>> parse(String[] args, List<Option<T>> options,
if (a.startsWith("--")) {
//handle long name options
String an = a.substring(2);
for (Option<T> o : options) {
for (Option<T> o : options.getFlatOptions()) {
if (o.getLongName().equals(an)) {
i += parseValue(o, args, i, result);
found = true;
Expand All @@ -149,7 +165,7 @@ public static <T> List<Value<T>> parse(String[] args, List<Option<T>> options,
} else if (a.startsWith("-")) {
//handle short name options
String an = a.substring(1);
for (Option<T> o : options) {
for (Option<T> o : options.getFlatOptions()) {
if (o.getShortName() != null && o.getShortName().equals(an)) {
i += parseValue(o, args, i, result);
found = true;
Expand Down

0 comments on commit b8d0589

Please sign in to comment.