diff --git a/components/console/single_command_tool.rst b/components/console/single_command_tool.rst index 97cb09bf030..5b419314d4c 100644 --- a/components/console/single_command_tool.rst +++ b/components/console/single_command_tool.rst @@ -6,8 +6,8 @@ In such a case, having to pass the command name each time is tedious. Fortunatel it is possible to remove this need by declaring a single command application:: #!/usr/bin/env php - setName('My Super Command') // Optional + ->setName('File Counter') // Optional ->setVersion('1.0.0') // Optional - ->addArgument('foo', InputArgument::OPTIONAL, 'The directory') - ->addOption('bar', null, InputOption::VALUE_REQUIRED) + ->addArgument('dir', InputArgument::OPTIONAL, 'The directory', default: '.') + ->addOption('all', 'a', InputOption::VALUE_NONE, 'count all files') ->setCode(function (InputInterface $input, OutputInterface $output): int { - // output arguments and options + $dir = realpath($input->getArgument('dir')); + $all = $input->getOption('all'); + $finder = (new Symfony\Component\Finder\Finder()) + ->in($dir) + ->files() + ->ignoreVCSIgnored(!$all) + ; + $count = iterator_count($finder); + $output->writeln( "$dir has $count " . + ($all ? "files" : "files in source control")); + return SingleCommandApplication::SUCCESS; }) ->run(); +Now run it with + + php bin/file-counter.php + + php bin/file-counter.php --all + You can still register a command as usual:: #!/usr/bin/env php