You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PhpCliProgram/tests/Commands/ReadFile.php

47 lines
1.3 KiB
PHP

<?php
namespace jrosset\Tests\Commands;
use jrosset\CliProgram\Validation\CommandWithValidation;
use jrosset\CliProgram\Validation\Validators\FilesystemValidationOption;
use jrosset\CliProgram\Validation\Validators\FileValidator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ReadFile extends CommandWithValidation {
/**
* @inheritdoc
*/
protected static $defaultDescription = 'Read a file';
/**
* @inheritDoc
*/
protected function configure (): void {
parent::configure();
$this->addArgument(
'file',
InputArgument::REQUIRED,
'The file to read',
null,
new FileValidator(
/** @lang PhpRegExp */ '#\.txt$#i',
FilesystemValidationOption::IS_READABLE
)
);
}
/**
* @inheritDoc
*/
protected function execute (InputInterface $input, OutputInterface $output): int {
if (($fileContent = file_get_contents($input->getArgument('file'))) === false) {
return Command::FAILURE;
}
$output->writeln($fileContent);
return Command::SUCCESS;
}
}