You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/aesh/annotation-processor.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -192,4 +192,4 @@ public class DeployCommand extends BaseCommand {
192
192
-**Java 8+** -- The processor targets source version 8
193
193
-**No code changes required** -- Drop in the dependency and the processor runs automatically
194
194
-**Fully backward compatible** -- Removing the dependency reverts to the reflection path with no behavior change
195
-
-**Works with all Aesh features** -- Custom validators, completers, converters, activators, renderers, result handlers, and option parsers are all supported
195
+
-**Works with all Aesh features** -- Custom validators, completers, converters, activators, renderers, result handlers, option parsers, default value providers, optional-value options, negatable options, stop-at-positional, and inherited options are all supported
Copy file name to clipboardExpand all lines: content/docs/aesh/command-definition.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,7 @@ The `@CommandDefinition` annotation is used to define a command class.
25
25
|`validator`|`Class<? extends CommandValidator>`|`NullCommandValidator.class`| Validator to run before execution |
26
26
|`resultHandler`|`Class<? extends ResultHandler>`|`NullResultHandler.class`| Handler to run after execution |
27
27
|`activator`|`Class<? extends CommandActivator>`|`NullCommandActivator.class`| Activator to check if command is available |
28
+
|`defaultValueProvider`|`Class<? extends DefaultValueProvider>`|`NullDefaultValueProvider.class`| Dynamic default value resolver |
28
29
|`stopAtFirstPositional`|`boolean`|`false`| Stop option parsing after the first positional argument |
29
30
|`helpUrl`|`String`|`""`| URL to documentation (shown in `--help` output) |
30
31
@@ -68,6 +69,71 @@ Return one of the following:
68
69
-`CommandResult.FAILURE` - Command failed
69
70
-`CommandResult.RETURN` - Return from current subcommand
70
71
72
+
## Dynamic Default Value Provider
73
+
74
+
The `defaultValueProvider` attribute specifies a class that resolves option defaults at runtime. This is useful when defaults come from configuration files, environment variables, or other external sources not known at compile time.
75
+
76
+
### Implementing a Provider
77
+
78
+
Create a class that implements `DefaultValueProvider`:
When determining option values, the precedence is (highest to lowest):
120
+
121
+
1.**User-provided value** -- Explicitly set on the command line
122
+
2.**Dynamic default** -- Returned by the `DefaultValueProvider` (if non-null)
123
+
3.**Static default** -- The `defaultValue` from the annotation
124
+
4.**null** -- If nothing else is set
125
+
126
+
If the provider returns `null` for an option, aesh falls back to the static `defaultValue`. This lets you use annotation defaults as fallbacks:
127
+
128
+
```java
129
+
// Provider returns "from-config" for template -> uses "from-config"
130
+
// Provider returns null for editor -> falls back to static default "vi"
131
+
@Option(defaultValue="vi")
132
+
privateString editor;
133
+
```
134
+
135
+
See [Options - Dynamic Default Values](/docs/aesh/options#dynamic-default-values) for more details.
136
+
71
137
## Stop at First Positional
72
138
73
139
When `stopAtFirstPositional = true`, option parsing stops as soon as the first positional argument is consumed. All remaining tokens are treated as positional arguments, even if they look like options.
For defaults that must be resolved at runtime (e.g., from configuration files, databases, or user profiles), use a `DefaultValueProvider`. The provider is registered on the command, not on individual options:
// debug = config value if set, else "4004", unless user provided a value
505
+
returnCommandResult.SUCCESS;
506
+
}
507
+
}
508
+
```
509
+
510
+
If the provider returns `null` for an option, the static `defaultValue` from the annotation is used as a fallback. See [Command Definition - Dynamic Default Value Provider](/docs/aesh/command-definition#dynamic-default-value-provider) for more details.
511
+
474
512
### Priority Order
475
513
476
514
When determining option values, Æsh uses this priority (highest to lowest):
477
515
478
516
1.**Command-line argument** - Explicitly provided by user
479
-
2.**Environment variable / System property** - If referenced in `defaultValue`
480
-
3.**Fallback value** - Value after `:` in the `defaultValue` expression
481
-
4.**Field initializer** - Java field initialization value
482
-
5.**null** - If nothing else is set
517
+
2.**Dynamic default** - From `DefaultValueProvider` (if non-null)
518
+
3.**Static default / Environment variable / System property** - From `defaultValue` annotation
519
+
4.**Fallback value** - Value after `:` in the `defaultValue` expression
520
+
5.**Field initializer** - Java field initialization value
0 commit comments