Skip to content

Commit 7545eb8

Browse files
committed
Document sortOptions and help option ordering
1 parent fe56a65 commit 7545eb8

5 files changed

Lines changed: 63 additions & 0 deletions

File tree

content/docs/aesh/command-definition.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The `@CommandDefinition` annotation is used to define a command class.
2727
| `activator` | `Class<? extends CommandActivator>` | `NullCommandActivator.class` | Activator to check if command is available |
2828
| `defaultValueProvider` | `Class<? extends DefaultValueProvider>` | `NullDefaultValueProvider.class` | Dynamic default value resolver |
2929
| `stopAtFirstPositional` | `boolean` | `false` | Stop option parsing after the first positional argument |
30+
| `sortOptions` | `boolean` | `false` | Sort help options alphabetically by name (after explicit option order) |
3031
| `helpUrl` | `String` | `""` | URL to documentation (shown in `--help` output) |
3132
| `helpGroup` | `String` | `""` | Group heading when listed as a subcommand in parent's help |
3233
| `helpSectionProvider` | `Class<? extends HelpSectionProvider>` | `NullHelpSectionProvider.class` | Provider for dynamic help sections |
@@ -176,6 +177,43 @@ With the command above:
176177

177178
Note that `--help` (and `--version`) before the first positional still work normally when `generateHelp = true`. Only tokens after the first positional are treated as passthrough arguments.
178179

180+
## Option Ordering in Help
181+
182+
Use `sortOptions` to control how options are ordered in help output:
183+
184+
- `sortOptions = false` (default): options are ordered by explicit option `order`, then declaration order
185+
- `sortOptions = true`: options are ordered by explicit option `order`, then alphabetical name
186+
187+
```java
188+
@CommandDefinition(
189+
name = "build",
190+
description = "Build project",
191+
sortOptions = true,
192+
generateHelp = true
193+
)
194+
public class BuildCommand implements Command<CommandInvocation> {
195+
196+
@Option(description = "Always first", order = 10)
197+
private boolean ci;
198+
199+
@Option(description = "Build profile", order = 20)
200+
private String profile;
201+
202+
@Option(description = "Clean output")
203+
private boolean clean;
204+
205+
@Option(description = "Verbose output")
206+
private boolean verbose;
207+
208+
@Override
209+
public CommandResult execute(CommandInvocation invocation) {
210+
return CommandResult.SUCCESS;
211+
}
212+
}
213+
```
214+
215+
The `order` attribute is defined on `@Option`, `@OptionList`, and `@OptionGroup`.
216+
179217
### CommandInvocation
180218

181219
Provides access to:

content/docs/aesh/group-commands.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class GitCommand implements GroupCommand<CommandInvocation> {
3838
| `groupCommands` | `Class[]` | `{}` | Array of subcommand classes |
3939
| `generateHelp` | `boolean` | `false` | Auto-generate `--help` option |
4040
| `aliases` | `String[]` | `{}` | Alternative names for the group |
41+
| `sortOptions` | `boolean` | `false` | Sort help options alphabetically by name (after explicit option order) |
4142
| `helpGroup` | `String` | `""` | Group heading when listed as a subcommand in parent's help |
4243
| `helpSectionProvider` | `Class<? extends HelpSectionProvider>` | `NullHelpSectionProvider.class` | Provider for dynamic help sections |
4344

content/docs/aesh/option-groups.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The `@OptionGroup` annotation defines key=value pair options collected into a `M
2424
| `renderer` | `Class<? extends OptionRenderer>` | `NullOptionRenderer.class` | Custom renderer |
2525
| `parser` | `Class<? extends OptionParser>` | `AeshOptionParser.class` | Custom parser |
2626
| `visibility` | `OptionVisibility` | `BRIEF` | Controls help and completion visibility (see [Options - Visibility Levels](../options#visibility-levels)) |
27+
| `order` | `int` | `Integer.MAX_VALUE` | Explicit help-order position (see [Options - Help Option Ordering](../options#help-option-ordering)) |
2728

2829
## Basic Example
2930

content/docs/aesh/option-lists.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The `@OptionList` annotation defines options that accept multiple values separat
2828
| `helpGroup` | `String` | `""` | Group heading for this option in help output (see [Options - Help Grouping](../options#help-grouping)) |
2929
| `exclusiveWith` | `String[]` | `{}` | Names of mutually exclusive options (see [Options - Mutually Exclusive Options](../options#mutually-exclusive-options)) |
3030
| `visibility` | `OptionVisibility` | `BRIEF` | Controls help and completion visibility (see [Options - Visibility Levels](../options#visibility-levels)) |
31+
| `order` | `int` | `Integer.MAX_VALUE` | Explicit help-order position (see [Options - Help Option Ordering](../options#help-option-ordering)) |
3132

3233
## Basic Example
3334

content/docs/aesh/options.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The `@Option` annotation defines command-line options (flags with values).
3636
| `allowedValues` | `String[]` | `{}` | Restricts option to a fixed set of valid values |
3737
| `exclusiveWith` | `String[]` | `{}` | Names of mutually exclusive options (without `--` prefix) |
3838
| `visibility` | `OptionVisibility` | `BRIEF` | Controls help and completion visibility (`BRIEF`, `FULL`, `HIDDEN`) |
39+
| `order` | `int` | `Integer.MAX_VALUE` | Explicit help-order position (lower values appear first) |
3940
| `descriptionUrl` | `String` | `""` | URL for option documentation (clickable in supported terminals) |
4041
| `url` | `boolean` | `false` | Treat option value as a URL (rendered as clickable link) |
4142

@@ -543,6 +544,27 @@ ProcessedOptionBuilder.builder()
543544
.build();
544545
```
545546

547+
## Help Option Ordering
548+
549+
Use `order` on options to explicitly control help output order:
550+
551+
```java
552+
@Option(description = "Always first", order = 10)
553+
private boolean ci;
554+
555+
@Option(description = "Shown after --ci", order = 20)
556+
private String profile;
557+
```
558+
559+
Options are ordered by:
560+
561+
1. `order` (lower first)
562+
2. Then by command-level `sortOptions` behavior:
563+
- `sortOptions = false` (default): declaration order
564+
- `sortOptions = true`: alphabetical by option name
565+
566+
`order` also works on `@OptionList` and `@OptionGroup`.
567+
546568
## Mutually Exclusive Options
547569

548570
The `exclusiveWith` property declares that two or more options cannot be used together. If a user provides both, a `MutuallyExclusiveOptionException` is thrown during parsing.

0 commit comments

Comments
 (0)