/** * @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; }
/** * 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; }
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')); }
/** * @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); } }
/** * 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); }