Skip to content

Commit

Permalink
feat: document Either
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 20, 2024
1 parent 4b0900d commit d288c72
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/annotations/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ These values will always be parsed:
@DefaultValue("foo") @Argument String string
```

#### Either

[Either](../core/index.md#either) may be used as a parameter type. Cloud will use the generic parameters to
determine which parsers to use.

```java
@Command("command <either>")
public void yourCommand(Either<Integer, Boolean> either) {
// ...
}
```

### Flags

Flags can be generated by using the `@Flag` annotation.
Expand Down
19 changes: 19 additions & 0 deletions docs/core/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,25 @@ that you create by calling `AggregateCommandParser.builder()`.
}).build();
```

### Either

You may use `ArgumentParser.firstOf(ParserDescriptor, ParserDescriptor)` to create a parser for the type `Either<A, B>`.
The parser will first attempt to parse the primary type `A`, and if this fails it will fall back on the
fallback type `B`. The suggestions of both the primary and fallback parsers will be joined when using the parser
as the suggestion provider.

```java title="Example of Either"
commandBuilder.required("either", ArgumentParser.firstOf(integerParser(), booleanParser()))
.handler(context -> {
Either<Integer, Boolean> either = context.get("either");
if (either.primary().isPresent()){
int integer = either.primary().get();
} else {
boolean bool = either.fallback().get();
}
});
```

### Custom Parsers

Cloud allows you to create your own parsers.
Expand Down

0 comments on commit d288c72

Please sign in to comment.