rvesse / airline

Java annotation-based framework for parsing Git like command line structures with deep extensibility
https://rvesse.github.io/airline/
Apache License 2.0
128 stars 20 forks source link

Support partial restrictions #36

Closed rvesse closed 8 years ago

rvesse commented 8 years ago

Currently when a restriction is applied to an @Option or @Arguments field it is enforced on every value that the option/argument takes regardless of the arity of said option

It would be nice to be able to specify a restriction that only applies to a specific value e.g.

@Option(name = "--example", arity = 2)
@PartialRestriction(index = 0, restriction = @NotBlank)
private List<String> examples;

Note that the above approach may not work because I'm not sure we can specify that the restriction field takes any annotation (though if we can that would be great)

The other approach would be to add an appliesTo field to all the existing restrictions where -1 indicates applies to all (the current default behaviour) and a positive integer indicates it applies to only the specific value

rvesse commented 8 years ago

Turns out we can't use Annotation as the general type of a restriction but we can use Class<? extends Annotation> so the definition will need to look something like the following:

@Option(name = "--example", arity = 2)
@PartialRestriction(index = 0, restriction = NotBlank.class)
@NotBlank
private List<String> examples;

And we'll need special logic in MetadataLoader to load the list of @PartialRestriction and modify generated restrictions as necessary

I have run into one bug which is that if you want to partially apply a postValidate() based restriction this does not work because postValidate() assumes the ability to see the full state. So we need to change that to finalValidate() method and introduce a new postValidate() method that sees the strongly typed value at the point at which it is introduced

rvesse commented 8 years ago

More thoughts:

rvesse commented 8 years ago

Will also need to document Migration because some of these changes are breaking

Finally it would be ideal if restrictions that make no sense as @Partial error ASAP

rvesse commented 8 years ago

Also it would be nice if the current means of enforcing restrictions is moved from the parsers into ParseState so people can't write parsers that don't enforce the restrictions and we don't have to duplicate code so much

rvesse commented 8 years ago

Everything but HelpHint support is done

rvesse commented 8 years ago

Added HelpHint support so closing this now