Skip to content

Commit

Permalink
Fully adopt ParserErrorHandler across parsers (#53)
Browse files Browse the repository at this point in the history
- Adopt in CliParser
- Move command instantiation logic to ParseResult<T>
  • Loading branch information
rvesse committed Nov 25, 2016
1 parent 6ad6ebf commit ec515f7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@

package com.github.rvesse.airline.parser;

import static com.github.rvesse.airline.parser.ParserUtil.createInstance;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.github.rvesse.airline.model.CommandGroupMetadata;
import com.github.rvesse.airline.model.CommandMetadata;
import com.github.rvesse.airline.model.GlobalMetadata;
import com.github.rvesse.airline.model.ParserMetadata;
import com.github.rvesse.airline.parser.errors.ParseException;
import com.github.rvesse.airline.utils.AirlineUtils;

/**
* Represents parsing results
Expand Down Expand Up @@ -68,4 +77,42 @@ public ParseState<T> getState() {
public Collection<ParseException> getErrors() {
return this.errors;
}

/**
* Gets the command if one was successfully parsed
*
* @return Command, or {@code null} if no command was parsed
*/
public T getCommand() {
CommandMetadata command = this.state.getCommand();
if (command == null)
return null;

// Prepare bindings
Map<Class<?>, Object> bindings = new HashMap<Class<?>, Object>();
bindings.put(GlobalMetadata.class, state.getGlobal());

if (state.getGroup() != null) {
bindings.put(CommandGroupMetadata.class, state.getGroup());
}

if (state.getCommand() != null) {
bindings.put(CommandMetadata.class, state.getCommand());
}

bindings.put(ParserMetadata.class, state.getParserConfiguration());
bindings = AirlineUtils.unmodifiableMapCopy(bindings);

if (state.getGlobal() != null) {
// Create instance
return createInstance(command.getType(), command.getAllOptions(), state.getParsedOptions(),
command.getArguments(), state.getParsedArguments(), command.getMetadataInjections(), bindings,
state.getParserConfiguration().getCommandFactory());
} else {
return createInstance(command.getType(), command.getAllOptions(), state.getParsedOptions(),
command.getArguments(), state.getParsedArguments(), command.getMetadataInjections(), bindings,
state.getParserConfiguration().getCommandFactory());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,21 @@
*/
package com.github.rvesse.airline.parser.command;

import static com.github.rvesse.airline.parser.ParserUtil.createInstance;

import java.util.HashMap;
import java.util.Map;

import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.CommandGroupMetadata;
import com.github.rvesse.airline.model.CommandMetadata;
import com.github.rvesse.airline.model.GlobalMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.ParserMetadata;
import com.github.rvesse.airline.parser.AbstractCommandParser;
import com.github.rvesse.airline.parser.ParseResult;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseException;
import com.github.rvesse.airline.restrictions.ArgumentsRestriction;
import com.github.rvesse.airline.restrictions.GlobalRestriction;
import com.github.rvesse.airline.restrictions.OptionRestriction;
import com.github.rvesse.airline.utils.AirlineUtils;

public class CliParser<T> extends AbstractCommandParser<T> {

public T parse(GlobalMetadata<T> metadata, Iterable<String> args) {
public ParseResult<T> parseWithResult(GlobalMetadata<T> metadata, Iterable<String> args) {
if (args == null)
throw new NullPointerException("args cannot be null");

Expand All @@ -52,35 +46,12 @@ public T parse(GlobalMetadata<T> metadata, Iterable<String> args) {
}

validate(state);
return metadata.getParserConfiguration().getErrorHandler().finished(state);
}

CommandMetadata command = state.getCommand();

Map<Class<?>, Object> bindings = new HashMap<Class<?>, Object>();
bindings.put(GlobalMetadata.class, metadata);

if (state.getGroup() != null) {
bindings.put(CommandGroupMetadata.class, state.getGroup());
}

if (state.getCommand() != null) {
bindings.put(CommandMetadata.class, state.getCommand());
}

bindings.put(ParserMetadata.class, state.getParserConfiguration());

if (command == null)
return null;

//@formatter:off
return createInstance(command.getType(),
command.getAllOptions(),
state.getParsedOptions(),
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
AirlineUtils.unmodifiableMapCopy(bindings),
state.getParserConfiguration().getCommandFactory());
//@formatter:on
public T parse(GlobalMetadata<T> metadata, Iterable<String> args) {
ParseResult<T> result = parseWithResult(metadata, args);
return result.getCommand();
}

/**
Expand All @@ -98,7 +69,11 @@ protected void validate(ParseState<T> state) {
for (GlobalRestriction restriction : state.getGlobal().getRestrictions()) {
if (restriction == null)
continue;
restriction.validate(state);
try {
restriction.validate(state);
} catch (ParseException e) {
state.getParserConfiguration().getErrorHandler().handleError(e);
}
}
CommandMetadata command = state.getCommand();
if (command != null) {
Expand All @@ -109,7 +84,11 @@ protected void validate(ParseState<T> state) {
for (ArgumentsRestriction restriction : arguments.getRestrictions()) {
if (restriction == null)
continue;
restriction.finalValidate(state, arguments);
try {
restriction.finalValidate(state, arguments);
} catch (ParseException e) {
state.getParserConfiguration().getErrorHandler().handleError(e);
}
}
}

Expand All @@ -120,7 +99,11 @@ protected void validate(ParseState<T> state) {
for (OptionRestriction restriction : option.getRestrictions()) {
if (restriction == null)
continue;
restriction.finalValidate(state, option);
try {
restriction.finalValidate(state, option);
} catch (ParseException e) {
state.getParserConfiguration().getErrorHandler().handleError(e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package com.github.rvesse.airline.parser.command;

import static com.github.rvesse.airline.parser.ParserUtil.createInstance;

import java.util.Collections;
import java.util.List;

import org.apache.commons.collections4.IteratorUtils;
Expand All @@ -33,7 +30,6 @@
import com.github.rvesse.airline.restrictions.ArgumentsRestriction;
import com.github.rvesse.airline.restrictions.GlobalRestriction;
import com.github.rvesse.airline.restrictions.OptionRestriction;
import com.github.rvesse.airline.utils.AirlineUtils;

public class SingleCommandParser<T> extends AbstractCommandParser<T> {

Expand All @@ -52,20 +48,7 @@ public ParseResult<T> parseWithResult(ParserMetadata<T> parserConfig, CommandMet
public T parse(ParserMetadata<T> parserConfig, CommandMetadata commandMetadata,
Iterable<GlobalRestriction> restrictions, Iterable<String> args) {
ParseResult<T> result = parseWithResult(parserConfig, commandMetadata, restrictions, args);

ParseState<T> state = result.getState();
CommandMetadata command = state.getCommand();

//@formatter:off
return createInstance(command.getType(),
command.getAllOptions(),
state.getParsedOptions(),
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
Collections.<Class<?>, Object>unmodifiableMap(AirlineUtils.singletonMap(CommandMetadata.class, commandMetadata)),
state.getParserConfiguration().getCommandFactory());
//@formatter:on
return result.getCommand();
}

/**
Expand Down

0 comments on commit ec515f7

Please sign in to comment.