Skip to content

Commit 70931db

Browse files
committed
Runtime: Add controls on mandatory options
1 parent 2c5ad8d commit 70931db

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Command/Compile.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use Yannoff\Component\Console\Command;
1919
use Yannoff\Component\Console\Definition\Option;
20+
use Yannoff\Component\Console\Exception\RuntimeException;
2021
use Yannoff\PhpCodeCompiler\Directory;
2122
use Yannoff\PhpCodeCompiler\PharBuilder;
2223

@@ -54,8 +55,8 @@ public function execute()
5455
$files = $this->getOption('file') ?? [];
5556
$meta = $this->getOption('meta') ?? [];
5657

57-
$output = $this->getOption('output');
58-
$main = $this->getOption('main');
58+
$main = $this->require('main');
59+
$output = $this->require('output');
5960

6061
$this
6162
->initBuilder($main)
@@ -225,6 +226,26 @@ protected function fullpath(string $file): string
225226
return getcwd() . '/' . $file;
226227
}
227228

229+
/**
230+
* Try to get the required option, raise an exception if not set
231+
*
232+
* @param string $option The option name
233+
*
234+
* @return mixed
235+
*
236+
* @throws RuntimeException If the required option is not set
237+
*/
238+
protected function require(string $option)
239+
{
240+
$value = $this->getOption($option);
241+
242+
if (null === $value) {
243+
throw new RuntimeException("Mandatory option --{$option} is missing");
244+
}
245+
246+
return $value;
247+
}
248+
228249
/**
229250
* Return the contents wrapped in a comments block
230251
*

0 commit comments

Comments
 (0)