Skip to content

Commit

Permalink
Runtime: Add controls on mandatory options
Browse files Browse the repository at this point in the history
  • Loading branch information
yannoff committed Mar 9, 2024
1 parent 2c5ad8d commit 70931db
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use Yannoff\Component\Console\Command;
use Yannoff\Component\Console\Definition\Option;
use Yannoff\Component\Console\Exception\RuntimeException;
use Yannoff\PhpCodeCompiler\Directory;
use Yannoff\PhpCodeCompiler\PharBuilder;

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

$output = $this->getOption('output');
$main = $this->getOption('main');
$main = $this->require('main');
$output = $this->require('output');

$this
->initBuilder($main)
Expand Down Expand Up @@ -225,6 +226,26 @@ protected function fullpath(string $file): string
return getcwd() . '/' . $file;
}

/**
* Try to get the required option, raise an exception if not set
*
* @param string $option The option name
*
* @return mixed
*
* @throws RuntimeException If the required option is not set
*/
protected function require(string $option)
{
$value = $this->getOption($option);

if (null === $value) {
throw new RuntimeException("Mandatory option --{$option} is missing");
}

return $value;
}

/**
* Return the contents wrapped in a comments block
*
Expand Down

0 comments on commit 70931db

Please sign in to comment.