@@ -6,25 +6,40 @@ In such a case, having to pass the command name each time is tedious. Fortunatel
6
6
it is possible to remove this need by declaring a single command application::
7
7
8
8
#!/usr/bin/env php
9
- <?php
10
- require __DIR__.'/vendor/autoload.php';
11
-
9
+ <?php // bin/file-counter.php
10
+ require __DIR__.'/../ vendor/autoload.php';
11
+
12
12
use Symfony\Component\Console\Input\InputArgument;
13
13
use Symfony\Component\Console\Input\InputInterface;
14
- use Symfony\Component\Console\Input\InputOption;
15
14
use Symfony\Component\Console\Output\OutputInterface;
16
15
use Symfony\Component\Console\SingleCommandApplication;
17
-
16
+
18
17
(new SingleCommandApplication())
19
- ->setName('My Super Command ') // Optional
18
+ ->setName('File Counter ') // Optional
20
19
->setVersion('1.0.0') // Optional
21
- ->addArgument('foo ', InputArgument::OPTIONAL, 'The directory')
22
- ->addOption('bar ', null , InputOption::VALUE_REQUIRED )
20
+ ->addArgument('dir ', InputArgument::OPTIONAL, 'The directory', default: '. ')
21
+ ->addOption('all ', 'a' , InputOption::VALUE_NONE, 'count all files' )
23
22
->setCode(function (InputInterface $input, OutputInterface $output): int {
24
- // output arguments and options
23
+ $dir = realpath($input->getArgument('dir'));
24
+ $all = $input->getOption('all');
25
+ $finder = (new Symfony\Component\Finder\Finder())
26
+ ->in($dir)
27
+ ->files()
28
+ ->ignoreVCSIgnored(!$all)
29
+ ;
30
+ $count = iterator_count($finder);
31
+ $output->writeln( "$dir has $count " .
32
+ ($all ? "files" : "files in source control"));
33
+ return SingleCommandApplication::SUCCESS;
25
34
})
26
35
->run();
27
36
37
+ Now run it with
38
+
39
+ php bin/file-counter.php
40
+
41
+ php bin/file-counter.php --all
42
+
28
43
You can still register a command as usual::
29
44
30
45
#!/usr/bin/env php
0 commit comments