CompletionSuggestions example

$shell = $input->getOption('shell');
            if (!$shell) {
                throw new \RuntimeException('The "--shell" option must be set.');
            }

            if (!$completionOutput = $this->completionOutputs[$shell] ?? false) {
                throw new \RuntimeException(sprintf('Shell completion is not supported for your shell: "%s" (supported: "%s").', $shellimplode('", "', array_keys($this->completionOutputs))));
            }

            $completionInput = $this->createCompletionInput($input);
            $suggestions = new CompletionSuggestions();

            $this->log([
                '',
                '<comment>'.date('Y-m-d H:i:s').'</>',
                '<info>Input:</> <comment>("|" indicates the cursor position)</>',
                ' '.(string) $completionInput,
                '<info>Command:</>',
                ' '.(string) implode(' ', $_SERVER['argv']),
                '<info>Messages:</>',
            ]);

            
$shell = $input->getOption('shell');
            if (!$shell) {
                throw new \RuntimeException('The "--shell" option must be set.');
            }

            if (!$completionOutput = $this->completionOutputs[$shell] ?? false) {
                throw new \RuntimeException(sprintf('Shell completion is not supported for your shell: "%s" (supported: "%s").', $shellimplode('", "', array_keys($this->completionOutputs))));
            }

            $completionInput = $this->createCompletionInput($input);
            $suggestions = new CompletionSuggestions();

            $this->log([
                '',
                '<comment>'.date('Y-m-d H:i:s').'</>',
                '<info>Input:</> <comment>("|" indicates the cursor position)</>',
                ' '.(string) $completionInput,
                '<info>Command:</>',
                ' '.(string) implode(' ', $_SERVER['argv']),
                '<info>Messages:</>',
            ]);

            

    public function complete(array $input): array
    {
        $currentIndex = \count($input);
        if ('' === end($input)) {
            array_pop($input);
        }
        array_unshift($input$this->command->getName());

        $completionInput = CompletionInput::fromTokens($input$currentIndex);
        $completionInput->bind($this->command->getDefinition());
        $suggestions = new CompletionSuggestions();

        $this->command->complete($completionInput$suggestions);

        $options = [];
        foreach ($suggestions->getOptionSuggestions() as $option) {
            $options[] = '--'.$option->getName();
        }

        return array_map('strval', array_merge($options$suggestions->getValueSuggestions()));
    }
}
abstract public function getExpectedOptionsOutput(): string;

    abstract public function getExpectedValuesOutput(): string;

    public function testOptionsOutput()
    {
        $options = [
            new InputOption('option1', 'o', InputOption::VALUE_NONE, 'First Option'),
            new InputOption('negatable', null, InputOption::VALUE_NEGATABLE, 'Can be negative'),
        ];
        $suggestions = new CompletionSuggestions();
        $suggestions->suggestOptions($options);
        $stream = fopen('php://memory', 'rw+');
        $this->getCompletionOutput()->write($suggestionsnew StreamOutput($stream));
        fseek($stream, 0);
        $this->assertEquals($this->getExpectedOptionsOutput()stream_get_contents($stream));
    }

    public function testValuesOutput()
    {
        $suggestions = new CompletionSuggestions();
        $suggestions->suggestValues([
            
$this->expectException(LogicException::class);
        $this->expectExceptionMessage('Cannot set suggested values if the option does not accept a value.');

        new InputOption('foo', null, InputOption::VALUE_NONE, '', null, ['foo']);
    }

    public function testCompleteArray()
    {
        $values = ['foo', 'bar'];
        $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, $values);
        $this->assertTrue($option->hasCompletion());
        $suggestions = new CompletionSuggestions();
        $option->complete(new CompletionInput()$suggestions);
        $this->assertSame($valuesarray_map(fn (Suggestion $suggestion) => $suggestion->getValue()$suggestions->getValueSuggestions()));
    }

    public function testCompleteClosure()
    {
        $values = ['foo', 'bar'];
        $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input): array => $values);
        $this->assertTrue($option->hasCompletion());
        $suggestions = new CompletionSuggestions();
        $option->complete(new CompletionInput()$suggestions);
        

    public function complete(array $input): array
    {
        $currentIndex = \count($input);
        if ('' === end($input)) {
            array_pop($input);
        }
        array_unshift($input$this->command->getName());

        $completionInput = CompletionInput::fromTokens($input$currentIndex);
        $completionInput->bind($this->command->getDefinition());
        $suggestions = new CompletionSuggestions();

        $this->command->complete($completionInput$suggestions);

        $options = [];
        foreach ($suggestions->getOptionSuggestions() as $option) {
            $options[] = '--'.$option->getName();
        }

        return array_map('strval', array_merge($options$suggestions->getValueSuggestions()));
    }
}
$this->expectException(\LogicException::class);
        $this->expectExceptionMessage('A default value for an array argument must be an array.');
        $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
        $argument->setDefault('default');
    }

    public function testCompleteArray()
    {
        $values = ['foo', 'bar'];
        $argument = new InputArgument('foo', null, '', null, $values);
        $this->assertTrue($argument->hasCompletion());
        $suggestions = new CompletionSuggestions();
        $argument->complete(new CompletionInput()$suggestions);
        $this->assertSame($valuesarray_map(fn (Suggestion $suggestion) => $suggestion->getValue()$suggestions->getValueSuggestions()));
    }

    public function testCompleteClosure()
    {
        $values = ['foo', 'bar'];
        $argument = new InputArgument('foo', null, '', null, fn (CompletionInput $input): array => $values);
        $this->assertTrue($argument->hasCompletion());
        $suggestions = new CompletionSuggestions();
        $argument->complete(new CompletionInput()$suggestions);
        
Home | Imprint | This part of the site doesn't use cookies.