|
17 | 17 | use CodeIgniter\CLI\Attributes\Command; |
18 | 18 | use CodeIgniter\CLI\CLI; |
19 | 19 | use CodeIgniter\CLI\Input\Argument; |
| 20 | +use CodeIgniter\CLI\Input\Option; |
20 | 21 |
|
21 | 22 | /** |
22 | 23 | * Displays the basic usage information for a given command. |
@@ -76,11 +77,9 @@ private function describeHelp(AbstractCommand $command): void |
76 | 77 | CLI::write(lang('CLI.helpArguments'), 'yellow'); |
77 | 78 |
|
78 | 79 | foreach ($command->getArgumentsDefinition() as $argument => $definition) { |
79 | | - $default = ''; |
80 | | - |
81 | | - if (! $definition->required) { |
82 | | - $default = sprintf(' [default: %s]', $this->formatDefaultValue($definition->default)); |
83 | | - } |
| 80 | + $default = ! $definition->required && $this->shouldShowDefault($definition->default) |
| 81 | + ? sprintf(' [default: %s]', $this->formatDefaultValue($definition->default)) |
| 82 | + : ''; |
84 | 83 |
|
85 | 84 | CLI::write(sprintf( |
86 | 85 | '%s%s%s', |
@@ -118,10 +117,15 @@ private function describeHelp(AbstractCommand $command): void |
118 | 117 | $definition->negation !== null ? sprintf('|--%s', $definition->negation) : '', |
119 | 118 | ); |
120 | 119 |
|
| 120 | + $default = $this->shouldShowOptionDefault($definition) |
| 121 | + ? sprintf(' [default: %s]', $this->formatDefaultValue($definition->default)) |
| 122 | + : ''; |
| 123 | + |
121 | 124 | CLI::write(sprintf( |
122 | | - '%s%s%s', |
| 125 | + '%s%s%s%s', |
123 | 126 | CLI::color($this->addPadding($optionString, 2, $maxPadding), 'green'), |
124 | 127 | $definition->description, |
| 128 | + CLI::color($default, 'yellow'), |
125 | 129 | $definition->isArray ? CLI::color(' (multiple values allowed)', 'yellow') : '', |
126 | 130 | )); |
127 | 131 | } |
@@ -155,6 +159,31 @@ private function getMaxPadding(AbstractCommand $command): int |
155 | 159 | return $max + 4; // Account for the extra padding around the option/argument. |
156 | 160 | } |
157 | 161 |
|
| 162 | + /** |
| 163 | + * Decides whether an option's default value is worth displaying. |
| 164 | + */ |
| 165 | + private function shouldShowOptionDefault(Option $definition): bool |
| 166 | + { |
| 167 | + // Only value-accepting options carry a meaningful default. Plain flags |
| 168 | + // and negatable toggles are boolean by nature. |
| 169 | + if (! $definition->acceptsValue) { |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + return $this->shouldShowDefault($definition->default); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Whether a default value is concrete enough to display. `null` and the |
| 178 | + * empty string are treated as "no default". |
| 179 | + * |
| 180 | + * @param list<string>|string|null $value |
| 181 | + */ |
| 182 | + private function shouldShowDefault(array|string|null $value): bool |
| 183 | + { |
| 184 | + return $value !== null && $value !== ''; |
| 185 | + } |
| 186 | + |
158 | 187 | /** |
159 | 188 | * @param list<string>|string $value |
160 | 189 | */ |
|
0 commit comments