2 namespace Drush\Commands;
6 * Set up local Drush configuration.
9 use Drush\Log\LogLevel;
10 use Consolidation\AnnotatedCommand\AnnotationData;
11 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
12 use Consolidation\OutputFormatters\Options\FormatterOptions;
14 use Consolidation\AnnotatedCommand\CommandData;
16 class ExampleCommands extends DrushCommands
19 * Demonstrate output formatters. Default format is 'table'.
21 * @command example:table
26 * @default-string-field second
27 * @usage example-table --format=yaml
28 * @usage example-table --format=csv
29 * @usage example-table --fields=first,third
30 * @usage example-table --fields=III,II
34 * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
36 public function exampleTable($options = ['format' => 'table'])
39 'en' => [ 'first' => 'One', 'second' => 'Two', 'third' => 'Three' ],
40 'de' => [ 'first' => 'Eins', 'second' => 'Zwei', 'third' => 'Drei' ],
41 'jp' => [ 'first' => 'Ichi', 'second' => 'Ni', 'third' => 'San' ],
42 'es' => [ 'first' => 'Uno', 'second' => 'Dos', 'third' => 'Tres' ],
44 $data = new RowsOfFields($tableData);
46 // Add a render function to transform cell data when the output
47 // format is a table, or similar. This allows us to add color
48 // information to the output without modifying the data cells when
49 // using yaml or json output formats.
50 $data->addRendererFunction(
51 // n.b. There is a fourth parameter $rowData that may be added here.
52 function ($key, $cellData, FormatterOptions $options, $rowData) {
53 if ($key == 'first') {
54 return "<comment>$cellData</>";
64 * Demonstrate an alter hook with an option
66 * @hook alter example-table
67 * @option french Add a row with French numbers.
68 * @usage example-table --french
70 public function alterFormatters($result, CommandData $commandData)
72 if ($commandData->input()->getOption('french')) {
73 $result['fr'] = [ 'first' => 'Un', 'second' => 'Deux', 'third' => 'Trois' ];