();
private final FormatterFactory formatterFactory;
private URL dotCucumber;
private boolean dryRun;
private boolean strict = false;
private boolean monochrome = false;
private SnippetType snippetType = SnippetType.UNDERSCORE;
private boolean formattersCreated = false;
/**
* Create a new instance from a string of options, for example:
*
*
*
* @param argv the arguments
*/
public RuntimeOptions(String argv) {
this(new FormatterFactory(), Shellwords.parse(argv));
}
/**
* Create a new instance from a list of options, for example:
*
*
*
* @param argv the arguments
*/
public RuntimeOptions(List argv) {
this(new FormatterFactory(), argv);
}
public RuntimeOptions(Env env, List argv) {
this(env, new FormatterFactory(), argv);
}
public RuntimeOptions(FormatterFactory formatterFactory, List argv) {
this(new Env("cucumber-jvm"), formatterFactory, argv);
}
public RuntimeOptions(Env env, FormatterFactory formatterFactory, List argv) {
this.formatterFactory = formatterFactory;
argv = new ArrayList(argv); // in case the one passed in is unmodifiable.
parse(argv);
String cucumberOptionsFromEnv = env.get("cucumber.options");
if (cucumberOptionsFromEnv != null) {
parse(Shellwords.parse(cucumberOptionsFromEnv));
}
filters.addAll(lineFilters);
if (formatterNames.isEmpty()) {
formatterNames.add("progress");
}
}
private void parse(List args) {
List parsedFilters = new ArrayList();
List parsedLineFilters = new ArrayList();
List parsedFeaturePaths = new ArrayList();
List parsedGlue = new ArrayList();
while (!args.isEmpty()) {
String arg = args.remove(0).trim();
if (arg.equals("--help") || arg.equals("-h")) {
printUsage();
System.exit(0);
} else if (arg.equals("--version") || arg.equals("-v")) {
System.out.println(VERSION);
System.exit(0);
} else if (arg.equals("--glue") || arg.equals("-g")) {
String gluePath = args.remove(0);
parsedGlue.add(gluePath);
} else if (arg.equals("--tags") || arg.equals("-t")) {
parsedFilters.add(args.remove(0));
} else if (arg.equals("--format") || arg.equals("-f")) {
formatterNames.add(args.remove(0));
} else if (arg.equals("--dotcucumber")) {
String urlOrPath = args.remove(0);
dotCucumber = Utils.toURL(urlOrPath);
} else if (arg.equals("--no-dry-run") || arg.equals("--dry-run") || arg.equals("-d")) {
dryRun = !arg.startsWith("--no-");
} else if (arg.equals("--no-strict") || arg.equals("--strict") || arg.equals("-s")) {
strict = !arg.startsWith("--no-");
} else if (arg.equals("--no-monochrome") || arg.equals("--monochrome") || arg.equals("-m")) {
monochrome = !arg.startsWith("--no-");
} else if (arg.equals("--snippets")) {
String nextArg = args.remove(0);
snippetType = SnippetType.fromString(nextArg);
} else if (arg.equals("--name") || arg.equals("-n")) {
String nextArg = args.remove(0);
Pattern patternFilter = Pattern.compile(nextArg);
parsedFilters.add(patternFilter);
} else if (arg.startsWith("-")) {
printUsage();
throw new CucumberException("Unknown option: " + arg);
} else {
PathWithLines pathWithLines = new PathWithLines(arg);
parsedFeaturePaths.add(pathWithLines.path);
parsedLineFilters.addAll(pathWithLines.lines);
}
}
if (!parsedFilters.isEmpty()) {
filters.clear();
filters.addAll(parsedFilters);
}
if (!parsedFeaturePaths.isEmpty()) {
featurePaths.clear();
lineFilters.clear();
featurePaths.addAll(parsedFeaturePaths);
lineFilters.addAll(parsedLineFilters);
}
if (!parsedGlue.isEmpty()) {
glue.clear();
glue.addAll(parsedGlue);
}
}
private void printUsage() {
System.out.println(USAGE);
}
public List cucumberFeatures(ResourceLoader resourceLoader) {
return load(resourceLoader, featurePaths, filters, System.out);
}
List getFormatters() {
if(!formattersCreated) {
for (String formatterName : formatterNames){
Formatter formatter = formatterFactory.create(formatterName);
formatters.add(formatter);
setMonochromeOnColorAwareFormatters(formatter);
setStrictOnStrictAwareFormatters(formatter);
}
formattersCreated = true;
}
return formatters;
}
public Formatter formatter(ClassLoader classLoader) {
return (Formatter) Proxy.newProxyInstance(classLoader, new Class>[]{Formatter.class}, new InvocationHandler() {
@Override
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
for (Formatter formatter : getFormatters()) {
Utils.invoke(formatter, method, 0, args);
}
return null;
}
});
}
public Reporter reporter(ClassLoader classLoader) {
return (Reporter) Proxy.newProxyInstance(classLoader, new Class>[]{Reporter.class}, new InvocationHandler() {
@Override
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
for (Formatter formatter : formatters) {
if (formatter instanceof Reporter) {
Utils.invoke(formatter, method, 0, args);
}
}
return null;
}
});
}
private void setMonochromeOnColorAwareFormatters(Formatter formatter) {
if (formatter instanceof ColorAware) {
ColorAware colorAware = (ColorAware) formatter;
colorAware.setMonochrome(monochrome);
}
}
private void setStrictOnStrictAwareFormatters(Formatter formatter) {
if (formatter instanceof StrictAware) {
StrictAware strictAware = (StrictAware) formatter;
strictAware.setStrict(strict);
}
}
public List getGlue() {
return glue;
}
public boolean isStrict() {
return strict;
}
public boolean isDryRun() {
return dryRun;
}
public List getFeaturePaths() {
return featurePaths;
}
public URL getDotCucumber() {
return dotCucumber;
}
public void addFormatter(Formatter formatter) {
formatters.add(formatter);
}
public List getFilters() {
return filters;
}
public boolean isMonochrome() {
return monochrome;
}
public SnippetType getSnippetType() {
return snippetType;
}
}