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
This accepts commands like `script.php input.txt` or `script.php -v input.txt output.txt`.
116
-
117
-
Arguments can appear anywhere on the command line - they don't have to come after options.
118
-
119
-
120
86
Additional Configuration
121
87
------------------------
122
88
@@ -151,12 +117,178 @@ Available keys:
151
117
|`Parser::Enum`| Array of allowed values |
152
118
153
119
120
+
Fluent API
121
+
==========
122
+
123
+
When you need more control over option definitions, use the fluent API with `addSwitch()`, `addOption()`, and `addArgument()` methods. This approach gives you access to all features including normalizers, enums, and precise control over each parameter:
124
+
125
+
```php
126
+
use Nette\CommandLine\Parser;
127
+
128
+
$parser = new Parser;
129
+
$parser
130
+
->addSwitch('--verbose', '-v')
131
+
->addOption('--output', '-o')
132
+
->addArgument('file');
133
+
134
+
$args = $parser->parse();
135
+
```
136
+
137
+
By default, `parse()` reads from `$_SERVER['argv']`. You can pass a custom array for testing:
**Switches** are flags without values, like `--verbose` or `-v`. They parse as `true` when present, `null` when absent:
150
+
151
+
```php
152
+
$parser->addSwitch('--verbose', '-v');
153
+
// --verbose → true
154
+
// -v → true
155
+
// (not used) → null
156
+
```
157
+
158
+
**Options** accept values, like `--output file.txt`. The value can be separated by space or `=`:
159
+
160
+
```php
161
+
$parser->addOption('--output', '-o');
162
+
// --output file.txt → 'file.txt'
163
+
// --output=file.txt → 'file.txt'
164
+
// -o file.txt → 'file.txt'
165
+
// --output → throws exception (value required)
166
+
// (not used) → null
167
+
```
168
+
169
+
Note that the option itself is always optional - not using it returns null. However, when used, the value is required by default. Set `optionalValue: true` to allow the option without a value (parses as `true`):
Use `fallback` to specify the value when an option or argument is not provided. For options with `optionalValue: true`, note that using the option without a value still parses as `true`, while the fallback is used only when the option is not present at all:
|`Unexpected parameter foo.`| Extra positional argument |
176
308
|`Value of option --format must be json, or xml.`| Value not in enum |
177
309
178
-
Use `isEmpty()` to check if no command-line arguments were provided:
310
+
Use `isEmpty()` to check if no command-line arguments were provided (i.e., user ran just `script.php` with nothing after it):
179
311
180
312
```php
181
313
if ($parser->isEmpty()) {
@@ -185,10 +317,46 @@ if ($parser->isEmpty()) {
185
317
```
186
318
187
319
320
+
Handling --help and --version
321
+
-----------------------------
322
+
323
+
When your script has required arguments, running `script.php --help` would normally fail because the required argument is missing. Use `parseOnly()` to check for info options first:
324
+
325
+
```php
326
+
$parser = new Parser;
327
+
$parser
328
+
->addSwitch('--help', '-h')
329
+
->addSwitch('--version', '-V')
330
+
->addArgument('input'); // required
331
+
332
+
// First, check info options (no validation, no exceptions)
0 commit comments