Skip to content

Commit

Permalink
Merge pull request #28 from darvil82/dev
Browse files Browse the repository at this point in the history
Version 1.2
  • Loading branch information
darvil82 authored May 26, 2024
2 parents 5885505 + fb609d7 commit fd2b6b4
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@Argument.Define(required = true, positional = true, description = "The name of the user.")
public String name;

@Argument.Define(type = StringArgumentType.class, description = "The surname of the user.")
@Argument.Define(type = String.class, description = "The surname of the user.")
public Optional<String> surname;

@Argument.Define(names = {"age", "a"}, description = "The age of the user.", prefix = Argument.Prefix.PLUS)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.darvil"
version = "1.1.0"
version = "1.2.0"
description = "Command line argument parser"

dependencies {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/lanat/Argument.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lanat;

import lanat.argumentTypes.ActionArgumentType;
import lanat.argumentTypes.DummyArgumentType;
import lanat.exceptions.ArgumentAlreadyExistsException;
import lanat.parsing.errors.handlers.ArgumentTypeError;
import lanat.parsing.errors.handlers.ParseErrors;
Expand Down Expand Up @@ -757,7 +756,7 @@ public void invokeCallbacks() {
@NotNull String description() default "";

/** @see ArgumentBuilder#type(ArgumentType) */
@NotNull Class<? extends ArgumentType<?>> type() default DummyArgumentType.class;
@NotNull Class<?> type() default Void.class;

/**
* Specifies the prefix character for this argument.
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/lanat/ArgumentBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package lanat;

import lanat.argumentTypes.DummyArgumentType;
import lanat.exceptions.ArgumentTypeInferException;
import lanat.exceptions.CommandTemplateException;
import lanat.utils.Builder;
Expand Down Expand Up @@ -51,13 +50,16 @@ public class ArgumentBuilder<Type extends ArgumentType<TInner>, TInner> implemen
final var annotation = field.getAnnotation(Argument.Define.class);
assert annotation != null : "The field must have an @Argument.Define annotation.";

// if the type is not a dummy type (it was specified on the annotation), instantiate it and return it
if (annotation.type() != DummyArgumentType.class)
return UtlReflection.instantiate(annotation.type());
var annotationType = annotation.type();
var annotationTypeDefined = annotationType != Void.class;

// if the type is not a Void, instantiate it or try to infer it if its not an ArgumentType
if (annotationTypeDefined && ArgumentType.class.isAssignableFrom(annotationType))
return (ArgumentType<?>)UtlReflection.instantiate(annotationType);

// try to infer the type from the field type. If it can't be inferred, return null
try {
return ArgumentTypeInfer.get(field.getType());
return ArgumentTypeInfer.get(annotationTypeDefined ? annotationType : field.getType());
} catch (ArgumentTypeInferException e) {
return null;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/lanat/CommandTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@
* If no name is specified, the name of the field will be used.
* </p>
* <p>
* The type of the argument (that extends {@link ArgumentType}) may be specified in the annotation with the
* {@link Argument.Define#type()} parameter. Note that any type specified in the annotation must have a public,
* The type of the argument may be specified in the annotation with the
* {@link Argument.Define#type()} parameter. If an {@link ArgumentType} subclass is specified, it must have a public,
* no-argument constructor. If the Argument Type to use has a constructor with arguments, the type must be then
* specified in {@link CommandTemplate#beforeInit(CommandBuildContext)} instead, by setting
* {@link ArgumentBuilder#type(ArgumentType)} to the argument builder corresponding to the argument
* being defined.
* </p>
* <p>
* If no Argument Type is specified on the annotation, the Argument Type will be attempted to be inferred from the
* field type if possible, which is the case for some built-in types, such as
* {@link String}, {@link Integer}, {@link java.io.File}, etc.
* If a class that does not subclass {@link ArgumentType} is specified on the annotation, or simply none is specified,
* the Argument Type will be attempted to be inferred from the field/annotation type if
* possible. (See {@link ArgumentTypeInfer})
* </p>
*
* <strong>Example:</strong>
* <pre>{@code
* @Command.Define
* public class ParentCommand extends CommandTemplate {
* @Argument.Define(names = {"name", "n"}, type = StringArgumentType.class)
* @Argument.Define(names = {"name", "n"}, type = String.class) // type: StringArgumentType
* public String name;
*
* @Argument.Define // name: "numbers". type: TupleArgumentType<Integer>
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/lanat/argumentTypes/DummyArgumentType.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public PrettyErrorFormatter(@NotNull ErrorContext currentErrorContext) {
final var formatter = this.getErrorLevelFormatter();
final String tokensFormatting = this.getGeneratedView().withConcatGap(" ").toString();

final var longestLineLength = UtlString.removeSequences(UtlString.getLongestLine(contents)).length();
final var longestLineLength = UtlString.getLengthIgnoreSequences(UtlString.getLongestLine(contents));

return formatter.withContents(" ┌─%s".formatted(this.getErrorLevel())).toString()
// only add a new line if there are tokens to display
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
import lanat.Command;
import lanat.CommandTemplate;
import lanat.argumentTypes.BooleanArgumentType;
import lanat.argumentTypes.FloatArgumentType;
import lanat.argumentTypes.IntegerArgumentType;
import lanat.argumentTypes.StringArgumentType;

import java.util.Optional;

public class CmdTemplates {
@Command.Define(names = "cmd1")
public static class CmdTemplate1 extends CommandTemplate {
@Argument.Define(type = IntegerArgumentType.class)
@Argument.Define(type = Integer.class)
public Integer number;

@Argument.Define(type = StringArgumentType.class)
@Argument.Define(type = String.class)
public String text;

@Argument.Define(names = { "name1", "f" }, type = BooleanArgumentType.class)
Expand All @@ -31,7 +29,7 @@ public static class CmdTemplate1 extends CommandTemplate {

@Command.Define(names = "cmd1-1")
public static class CmdTemplate1_1 extends CommandTemplate {
@Argument.Define(type = FloatArgumentType.class)
@Argument.Define(type = Float.class)
public Float number;

@Argument.Define(type = IntegerArgumentType.class)
Expand Down

0 comments on commit fd2b6b4

Please sign in to comment.