From 932c308c54f54c0c5cd6b138a6a474f6d0e447bb Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Fri, 29 Nov 2024 09:20:50 -0500 Subject: [PATCH 1/2] More detailed Single File Application example The original example didn't work because it didn't return the integer value. I expanded the example to something that's marginally useful (and more real-world). --- components/console/single_command_tool.rst | 33 ++++++++++++++++------ 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/components/console/single_command_tool.rst b/components/console/single_command_tool.rst index 97cb09bf030..eecc9c84fbf 100644 --- a/components/console/single_command_tool.rst +++ b/components/console/single_command_tool.rst @@ -6,25 +6,40 @@ 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 From 3fbe9f4bf1fac6b13e0de73aa29686da662783e7 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Fri, 29 Nov 2024 09:39:33 -0500 Subject: [PATCH 2/2] remove trailing spaces, add missing use statement --- components/console/single_command_tool.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/console/single_command_tool.rst b/components/console/single_command_tool.rst index eecc9c84fbf..5b419314d4c 100644 --- a/components/console/single_command_tool.rst +++ b/components/console/single_command_tool.rst @@ -8,12 +8,13 @@ it is possible to remove this need by declaring a single command application:: #!/usr/bin/env php setName('File Counter') // Optional ->setVersion('1.0.0') // Optional @@ -34,9 +35,9 @@ it is possible to remove this need by declaring a single command application:: }) ->run(); -Now run it with +Now run it with - php bin/file-counter.php + php bin/file-counter.php php bin/file-counter.php --all