/** * Restructure this data for output by converting it into a table * transformation object. * * @param FormatterOptions $options Options that affect output formatting. * @return Consolidation\OutputFormatters\Transformations\TableTransformation */ public function restructure(FormatterOptions $options) { $data = [$this->getArrayCopy()]; $options->setConfigurationDefault('list-orientation', true); $tableTransformer = $this->createTableTransformation($data, $options); return $tableTransformer; }
/** * Return a set of InputOption based on the annotations of a command. * @param FormatterOptions $options * @return InputOption[] */ public function automaticOptions(FormatterOptions $options, $dataType) { $automaticOptions = []; // At the moment, we only support automatic options for --format // and --fields, so exit if the command returns no data. if (!isset($dataType)) { return []; } $validFormats = $this->validFormats($dataType); if (empty($validFormats)) { return []; } $availableFields = $options->get(FormatterOptions::FIELD_LABELS); $hasDefaultStringField = $options->get(FormatterOptions::DEFAULT_STRING_FIELD); $defaultFormat = $hasDefaultStringField ? 'string' : ($availableFields ? 'table' : 'yaml'); if (count($validFormats) > 1) { // Make an input option for --format $description = 'Format the result data. Available formats: ' . implode(',', $validFormats); $automaticOptions[FormatterOptions::FORMAT] = new InputOption(FormatterOptions::FORMAT, '', InputOption::VALUE_OPTIONAL, $description, $defaultFormat); } if ($availableFields) { $defaultFields = $options->get(FormatterOptions::DEFAULT_FIELDS, [], ''); $description = 'Available fields: ' . implode(', ', $this->availableFieldsList($availableFields)); $automaticOptions[FormatterOptions::FIELDS] = new InputOption(FormatterOptions::FIELDS, '', InputOption::VALUE_OPTIONAL, $description, $defaultFields); $automaticOptions[FormatterOptions::FIELD] = new InputOption(FormatterOptions::FIELD, '', InputOption::VALUE_OPTIONAL, "Select just one field, and force format to 'string'.", ''); } return $automaticOptions; }
/** * @inheritdoc */ public function write(OutputInterface $output, $dom, FormatterOptions $options) { if (is_array($dom)) { $schema = $options->getXmlSchema(); $dom = $schema->arrayToXML($dom); } $dom->formatOutput = true; $output->writeln($dom->saveXML()); }
/** * @inheritdoc */ public function overrideOptions($structuredOutput, FormatterOptions $options) { $defaultField = $options->get(FormatterOptions::DEFAULT_STRING_FIELD, [], ''); $userFields = $options->get(FormatterOptions::FIELDS, [FormatterOptions::FIELDS => $options->get(FormatterOptions::FIELD)]); $optionsOverride = $options->override([]); if (empty($userFields) && !empty($defaultField)) { $optionsOverride->setOption(FormatterOptions::FIELDS, $defaultField); } return $optionsOverride; }
public function testFormatterOptions() { $configurationData = [FormatterOptions::DEFAULT_FORMAT => 'table', 'test' => 'one', 'try' => 'two']; $userOptions = ['try' => 'three']; $defaults = [FormatterOptions::DEFAULT_FORMAT => 'var_export', 'try' => 'four', 'default-only' => 'defaulty']; // Create a StringInput object and ensure that Symfony Console is working right. $input = $this->createStringInput('test --format=yaml --include-field-labels'); $testValue = $input->getOption(FormatterOptions::INCLUDE_FIELD_LABELS); $this->assertTrue($testValue); $testValue = $input->getOption(FormatterOptions::FORMAT); $this->assertEquals('yaml', $testValue); // $options->get() only returns the default parameter is there is // no matching key in configuration, userOptions or defaults. $options = new FormatterOptions($configurationData, $userOptions); $this->assertEquals('', $options->get('default-only')); $this->assertEquals('defaulty', $options->get('default-only', $defaults)); $this->assertEquals('defaulty', $options->get('default-only', $defaults, 'irrelevant')); $this->assertEquals('three', $options->get('try')); $this->assertEquals('three', $options->get('try', $defaults)); $this->assertEquals('three', $options->get('try', $defaults, 'irrelevant')); $this->assertFalse($options->get('no-such-key')); $this->assertFalse($options->get('no-such-key', $defaults)); $this->assertEquals('last-chance', $options->get('no-such-key', $defaults, 'last-chance')); // Change a user option $options = new FormatterOptions($configurationData, $userOptions); $options->setOption('try', 'changed'); $this->assertEquals('changed', $options->get('try')); $this->assertEquals('changed', $options->get('try', $defaults)); $this->assertEquals('changed', $options->get('try', $defaults, 'irrelevant')); // Configuration has higher priority than defaults $options = new FormatterOptions($configurationData, $userOptions); $this->assertEquals('table', $this->getFormat($options)); $this->assertEquals('table', $this->getFormat($options, $defaults)); // Override has higher priority than configuration and defaults $options = new FormatterOptions($configurationData, $userOptions); $newOptions = $options->override([FormatterOptions::DEFAULT_FORMAT => 'json']); $this->assertEquals('json', $this->getFormat($newOptions)); $this->assertEquals('json', $this->getFormat($newOptions, $defaults)); $options = new FormatterOptions($configurationData, $userOptions); $options->setConfigurationDefault(FormatterOptions::DEFAULT_FORMAT, 'php'); $this->assertEquals('table', $this->getFormat($options)); $options = new FormatterOptions($configurationData, $userOptions); $options->setConfigurationData([]); $this->assertEquals('', $this->getFormat($options)); // It is only possible to override options that appear in '$default' // with $input; if there are no defaults, then the --format=yaml // option will not be picked up. $options = new FormatterOptions($configurationData, $userOptions); $options->setInput($input); $this->assertEquals('table', $options->get(FormatterOptions::DEFAULT_FORMAT)); $this->assertEquals('table', $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, 'irrelevant')); // We won't see the default value unless the configuration value is empty. $options = new FormatterOptions([], $userOptions); $this->assertEquals('var_export', $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, 'irrelevant')); }
public function prepare(CommandData $commandData, FormatterOptions $options) { $width = $this->getTerminalWidth(); if (!$width) { $width = $this->defaultWidth; } // Enforce minimum and maximum widths $width = min($width, $this->getMaxWidth($commandData)); $width = max($width, $this->getMinWidth($commandData)); $options->setWidth($width); }
/** * @inheritdoc */ public function write(OutputInterface $output, $data, FormatterOptions $options) { $defaults = $this->getDefaultFormatterOptions(); $includeFieldLabels = $options->get(FormatterOptions::INCLUDE_FIELD_LABELS, $defaults); if ($includeFieldLabels && $data instanceof TableTransformation) { $headers = $data->getHeaders(); $this->writeOneLine($output, $headers, $options); } foreach ($data as $line) { $this->writeOneLine($output, $line, $options); } }
/** * @inheritdoc */ public function write(OutputInterface $output, $tableTransformer, FormatterOptions $options) { $table = new Table($output); $table->setStyle('compact'); foreach ($tableTransformer as $rowid => $row) { $rowLabel = $tableTransformer->getRowLabel($rowid); $output->writeln(''); $output->writeln($rowLabel); $sectionData = new PropertyList($row); $sectionOptions = new FormatterOptions([], $options->getOptions()); $sectionTableTransformer = $sectionData->restructure($sectionOptions); $table->setRows($sectionTableTransformer->getTableData(true)); $table->render(); } }
/** * Determine the formatter that should be used to render * output. * * If the user specified a format via the --format option, * then always return that. Otherwise, return the default * format, unless --pipe was specified, in which case * return the default pipe format, format-pipe. * * n.b. --pipe is a handy option introduced in Drush 2 * (or perhaps even Drush 1) that indicates that the command * should select the output format that is most appropriate * for use in scripts (e.g. to pipe to another command). * * @return string */ protected function getFormat(FormatterOptions $options) { // In Symfony Console, there is no way for us to differentiate // between the user specifying '--format=table', and the user // not specifying --format when the default value is 'table'. // Therefore, we must make --field always override --format; it // cannot become the default value for --format. if ($options->get('field')) { return 'string'; } $defaults = []; if ($options->get('pipe')) { return $options->get('pipe-format', [], 'tsv'); } return $options->getFormat($defaults); }
/** * Wrap the table data * @param array $data * @param TableStyle $tableStyle * @param FormatterOptions $options * @return array */ protected function wrap($headers, $data, TableStyle $tableStyle, FormatterOptions $options) { $wrapper = new WordWrapper($options->get(FormatterOptions::TERMINAL_WIDTH)); $wrapper->setPaddingFromStyle($tableStyle); if (!empty($headers)) { $headerLengths = array_map(function ($item) { return strlen($item); }, $headers); $wrapper->setMinimumWidths($headerLengths); } return $wrapper->wrap($data); }
function testSimpleList() { $expected = <<<EOT ------- -------- One apple Two banana Three carrot ------- -------- EOT; $data = $this->simpleListExampleDataUsingAssociativeList(); $this->assertFormattedOutputMatches($expected, 'table', $data); $data = $this->simpleListExampleData(); $this->assertFormattedOutputMatches($expected, 'table', $data); $expected = <<<EOT ----- -------- I apple II banana III carrot ----- -------- EOT; // If we provide field labels, then the output will change to reflect that. $formatterOptionsWithFieldLables = new FormatterOptions(); $formatterOptionsWithFieldLables->setFieldLabels(['one' => 'I', 'two' => 'II', 'three' => 'III']); $this->assertFormattedOutputMatches($expected, 'table', $data, $formatterOptionsWithFieldLables); $expectedDrushStyleTable = <<<EOT One : apple Two : banana Three : carrot EOT; // If we provide field labels, then the output will change to reflect that. $formatterOptionsWithFieldLables = new FormatterOptions(); $formatterOptionsWithFieldLables->setTableStyle('compact')->setListDelimiter(':'); $this->assertFormattedOutputMatches($expectedDrushStyleTable, 'table', $data, $formatterOptionsWithFieldLables); // Adding an extra field that does not exist in the data set should not change the output $formatterOptionsWithExtraFieldLables = new FormatterOptions(); $formatterOptionsWithExtraFieldLables->setFieldLabels(['one' => 'I', 'two' => 'II', 'three' => 'III', 'four' => 'IV']); $this->assertFormattedOutputMatches($expected, 'table', $data, $formatterOptionsWithExtraFieldLables); $expectedRotated = <<<EOT ------- -------- -------- One Two Three ------- -------- -------- apple banana carrot ------- -------- -------- EOT; $this->assertFormattedOutputMatches($expectedRotated, 'table', $data, new FormatterOptions(['list-orientation' => false])); $expectedList = <<<EOT apple banana carrot EOT; $this->assertFormattedOutputMatches($expectedList, 'list', $data); $expectedReorderedList = <<<EOT carrot apple EOT; $options = new FormatterOptions([FormatterOptions::FIELDS => 'three,one']); $this->assertFormattedOutputMatches($expectedReorderedList, 'list', $data, $options); $expectedCsv = <<<EOT One,Two,Three apple,banana,carrot EOT; $this->assertFormattedOutputMatches($expectedCsv, 'csv', $data); $expectedCsvNoHeaders = 'apple,banana,carrot'; $this->assertFormattedOutputMatches($expectedCsvNoHeaders, 'csv', $data, new FormatterOptions(), ['include-field-labels' => false]); // Next, configure the formatter options with 'include-field-labels', // but set --include-field-labels to turn the option back on again. $options = new FormatterOptions(['include-field-labels' => false]); $input = new StringInput('test --include-field-labels'); $optionDefinitions = [new InputArgument('unused', InputArgument::REQUIRED), new InputOption('include-field-labels', null, InputOption::VALUE_NONE)]; $definition = new InputDefinition($optionDefinitions); $input->bind($definition); $testValue = $input->getOption('include-field-labels'); $this->assertTrue($testValue); $hasFieldLabels = $input->hasOption('include-field-labels'); $this->assertTrue($hasFieldLabels); $this->assertFormattedOutputMatches($expectedCsvNoHeaders, 'csv', $data, $options); $options->setInput($input); $this->assertFormattedOutputMatches($expectedCsv, 'csv', $data, $options); }