diff --git a/airline-core/src/main/java/com/github/rvesse/airline/Cli.java b/airline-core/src/main/java/com/github/rvesse/airline/Cli.java index a10a78267..659991d88 100644 --- a/airline-core/src/main/java/com/github/rvesse/airline/Cli.java +++ b/airline-core/src/main/java/com/github/rvesse/airline/Cli.java @@ -20,9 +20,18 @@ import com.github.rvesse.airline.builder.CliBuilder; import com.github.rvesse.airline.model.GlobalMetadata; import com.github.rvesse.airline.model.MetadataLoader; +import com.github.rvesse.airline.parser.ParseResult; import com.github.rvesse.airline.parser.command.CliParser; import com.github.rvesse.airline.utils.AirlineUtils; +/** + * Class for encapsulating and parsing CLIs + * + * @author rvesse + * + * @param + * Command type + */ public class Cli { /** * Creates a builder for specifying a command line in fluent style @@ -43,7 +52,8 @@ public static CliBuilder builder(String name) { * Creates a new CLI from a class annotated with the * {@link com.github.rvesse.airline.annotations.Cli} annotation * - * @param cliClass CLI class + * @param cliClass + * CLI class */ public Cli(Class cliClass) { this(MetadataLoader. loadGlobal(cliClass)); @@ -61,16 +71,70 @@ public Cli(GlobalMetadata metadata) { this.metadata = metadata; } + /** + * Gets the global meta-data + * + * @return Meta-data + */ public GlobalMetadata getMetadata() { return metadata; } + /** + * Parses the arguments to produce a command instance, this may be + * {@code null} if the arguments don't identify a command and there was no + * appropriate default command configured + * + * @param args + * Arguments + * @return Command instance + */ public C parse(String... args) { return parse(ListUtils.unmodifiableList(AirlineUtils.arrayToList(args))); } + /** + * Parses the arguments to produce a command instance, this may be + * {@code null} if the arguments don't identify a command and there was no + * appropriate default command configured + * + * @param args + * Arguments + * @return Command instance + */ private C parse(Iterable args) { CliParser parser = new CliParser(); return parser.parse(metadata, args); } + + /** + * Parses the arguments to produce a result. The result can be inspected to + * see errors (assuming a suitable error handler was used e.g. + * {@code CollectAll}) and to get a command instance. This may be + * {@code null} if the arguments don't identify a command and there was no + * appropriate default command configured + * + * @param args + * Arguments + * @return Parse result + */ + public ParseResult parseWithResult(String... args) { + return parseWithResult(AirlineUtils.arrayToList(args)); + } + + /** + * Parses the arguments to produce a result. The result can be inspected to + * see errors (assuming a suitable error handler was used e.g. + * {@code CollectAll}) and to get a command instance. This may be + * {@code null} if the arguments don't identify a command and there was no + * appropriate default command configured + * + * @param args + * Arguments + * @return Parse result + */ + public ParseResult parseWithResult(Iterable args) { + CliParser parser = new CliParser(); + return parser.parseWithResult(metadata, args); + } } diff --git a/airline-core/src/main/java/com/github/rvesse/airline/SingleCommand.java b/airline-core/src/main/java/com/github/rvesse/airline/SingleCommand.java index 95df41a83..2d6cc8579 100644 --- a/airline-core/src/main/java/com/github/rvesse/airline/SingleCommand.java +++ b/airline-core/src/main/java/com/github/rvesse/airline/SingleCommand.java @@ -27,7 +27,7 @@ import com.github.rvesse.airline.utils.AirlineUtils; /** - * Class for encapsulating single commands + * Class for encapsulating and parsing single commands * * @param * Command type @@ -65,9 +65,9 @@ public static SingleCommand singleCommand(Class command, ParserMetadat private SingleCommand(Class command, Iterable restrictions, ParserMetadata parserConfig) { if (command == null) throw new NullPointerException("command is null"); - this.parserConfig = parserConfig != null ? parserConfig : MetadataLoader.loadParser(command); - this.restrictions = restrictions != null ? IteratorUtils.toList(restrictions.iterator()) : AirlineUtils - .arrayToList(GlobalRestriction.DEFAULTS); + this.parserConfig = parserConfig != null ? parserConfig : MetadataLoader. loadParser(command); + this.restrictions = restrictions != null ? IteratorUtils.toList(restrictions.iterator()) + : AirlineUtils.arrayToList(GlobalRestriction.DEFAULTS); if (this.restrictions.size() == 0) this.restrictions.addAll(AirlineUtils.arrayToList(GlobalRestriction.DEFAULTS)); @@ -92,19 +92,51 @@ public ParserMetadata getParserConfiguration() { return parserConfig; } + /** + * Parses the arguments to produce a command instance + * + * @param args + * Arguments + * @return Command instance + */ public C parse(String... args) { return parse(AirlineUtils.arrayToList(args)); } + /** + * Parses the arguments to produce a command instance + * + * @param args + * Arguments + * @return Command instance + */ public C parse(Iterable args) { SingleCommandParser parser = new SingleCommandParser(); return parser.parse(parserConfig, commandMetadata, restrictions, args); } - + + /** + * Parses the arguments to produce a result. The result can be inspected to + * see errors (assuming a suitable error handler was used e.g. + * {@code CollectAll}) and to get a command instance + * + * @param args + * Arguments + * @return Parse result + */ public ParseResult parseWithResult(String... args) { return parseWithResult(AirlineUtils.arrayToList(args)); } - + + /** + * Parses the arguments to produce a result. The result can be inspected to + * see errors (assuming a suitable error handler was used e.g. + * {@code CollectAll}) and to get a command instance + * + * @param args + * Arguments + * @return Parse result + */ public ParseResult parseWithResult(Iterable args) { SingleCommandParser parser = new SingleCommandParser(); return parser.parseWithResult(parserConfig, commandMetadata, restrictions, args);