|
| 1 | +--- |
| 2 | +date: '2026-01-11T15:00:00+01:00' |
| 3 | +draft: false |
| 4 | +title: 'Activators' |
| 5 | +--- |
| 6 | + |
| 7 | +Activators control whether options, arguments, or entire commands are available/enabled. |
| 8 | + |
| 9 | +## OptionActivator |
| 10 | + |
| 11 | +Enable or disable an option based on conditions: |
| 12 | + |
| 13 | +```java |
| 14 | +public class SslOptionActivator implements OptionActivator { |
| 15 | + |
| 16 | + @Override |
| 17 | + public boolean isActivated(CompleterInvocation invocation) { |
| 18 | + MyCommand cmd = (MyCommand) invocation.getCommand(); |
| 19 | + // SSL options only active if secure mode is enabled |
| 20 | + return cmd.secureMode; |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Usage: |
| 26 | + |
| 27 | +```java |
| 28 | +@CommandDefinition(name = "server") |
| 29 | +public class ServerCommand implements Command<CommandInvocation> { |
| 30 | + |
| 31 | + @Option(name = "secure", hasValue = false, description = "Enable secure mode") |
| 32 | + private boolean secureMode = false; |
| 33 | + |
| 34 | + @Option( |
| 35 | + name = "cert", |
| 36 | + activator = SslOptionActivator.class, |
| 37 | + description = "SSL certificate (only in secure mode)" |
| 38 | + ) |
| 39 | + private String certPath; |
| 40 | + |
| 41 | + @Option( |
| 42 | + name = "key", |
| 43 | + activator = SslOptionActivator.class, |
| 44 | + description = "SSL private key (only in secure mode)" |
| 45 | + ) |
| 46 | + private String keyPath; |
| 47 | + |
| 48 | + @Override |
| 49 | + public CommandResult execute(CommandInvocation invocation) { |
| 50 | + return CommandResult.SUCCESS; |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## CommandActivator |
| 56 | + |
| 57 | +Control whether a command is visible/available: |
| 58 | + |
| 59 | +```java |
| 60 | +public class AdminCommandActivator implements CommandActivator { |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean isActivated(CommandInvocation invocation) { |
| 64 | + // Only show admin commands if user is admin |
| 65 | + return isAdmin(invocation); |
| 66 | + } |
| 67 | + |
| 68 | + private boolean isAdmin(CommandInvocation invocation) { |
| 69 | + // Check if user has admin privileges |
| 70 | + // This could check environment variables, config files, etc. |
| 71 | + return System.getProperty("user.role", "user").equals("admin"); |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Usage: |
| 77 | + |
| 78 | +```java |
| 79 | +@CommandDefinition( |
| 80 | + name = "shutdown", |
| 81 | + description = "Shutdown the server", |
| 82 | + activator = AdminCommandActivator.class |
| 83 | +) |
| 84 | +public class ShutdownCommand implements Command<CommandInvocation> { |
| 85 | + |
| 86 | + @Override |
| 87 | + public CommandResult execute(CommandInvocation invocation) { |
| 88 | + invocation.println("Shutting down..."); |
| 89 | + return CommandResult.SUCCESS; |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Conditional Validation |
| 95 | + |
| 96 | +Use activators to conditionally require options: |
| 97 | + |
| 98 | +```java |
| 99 | +public class OutputFileActivator implements OptionActivator { |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean isActivated(CompleterInvocation invocation) { |
| 103 | + MyCommand cmd = (MyCommand) invocation.getCommand(); |
| 104 | + // Output file option only active when not verbose |
| 105 | + return !cmd.verbose; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +@CommandDefinition(name = "process") |
| 110 | +public class ProcessCommand implements Command<CommandInvocation> { |
| 111 | + |
| 112 | + @Option(name = "verbose", hasValue = false, description = "Verbose output") |
| 113 | + private boolean verbose = false; |
| 114 | + |
| 115 | + @Option( |
| 116 | + name = "output", |
| 117 | + activator = OutputFileActivator.class, |
| 118 | + required = true, |
| 119 | + description = "Output file (required unless verbose)" |
| 120 | + ) |
| 121 | + private String outputFile; |
| 122 | + |
| 123 | + @Override |
| 124 | + public CommandResult execute(CommandInvocation invocation) { |
| 125 | + return CommandResult.SUCCESS; |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +When `--verbose` is used, the `--output` option is hidden and not required. |
| 131 | + |
| 132 | +## Accessing Runtime Context |
| 133 | + |
| 134 | +Activators can access the Aesh context: |
| 135 | + |
| 136 | +```java |
| 137 | +public class ContextAwareActivator implements OptionActivator { |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean isActivated(CompleterInvocation invocation) { |
| 141 | + AeshContext context = invocation.getAeshContext(); |
| 142 | + // Access runtime context for more complex conditions |
| 143 | + return context.someCondition(); |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
0 commit comments