Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash/php - any way to implement tab key autocomplete for arguments?

Tags:

linux

bash

php

I have a PHP command line script which kicks off a job to a job server, with the job name being an argument. The job names are namespaced, such as Foo:Bar_Baz_JobName. Is there a way I could implement auto-completion, like how typing the first few letters of a filename and pressing tab bash completes the file name for you. I know it can be done, because tab-completion works on ubuntu with apt-get, I just don't know if it can be done in PHP.

like image 741
Harold1983- Avatar asked Jul 09 '10 19:07

Harold1983-


2 Answers

Autocompletion is done using the GNU readline library, which apparently is accessible from PHP. Specifically, look at readline_completion_function. Usage is fairly simple; you call readline_completion_function with one argument, a callback function that handles the completion. The callback function takes the first few letters (basically, whatever you type before pressing TAB) as input and should return an array of possible matches.

like image 147
David Z Avatar answered Oct 08 '22 11:10

David Z


About readline_completion_function

I would say that considering the type of interaction you are looking for (apt-get autocomplete) this method is not good.

In fact before to get a autocomplete function you have to run the script and then you'll access the autocomplete function.

The autocomplete function is partial. So, it like the bash autocomplete not the zsh.

In the shell:

➜  ~ php test.php (enter) 
Custom command: (tab)
a b c
Custom command: (tab)
a b c

The code is:

<?php
// test.php
class AutoController
{

    private static function getCommandsArray()
    {
        $my_dir = array('a', 'b', 'c');
        return $my_dir;
    }

    /**
     * The callback which is returning an array with strings, which will be
     * auto-completed.
     *
     * @param $input
     * @param $index
     * @return array
     */
    private static function completionCallback($input, $index)
    {
        return self::getCommandsArray();
    }

    /**
     * The method which is handling the autocompletion. After it's runned, you can
     * autocomplete your commands by hitting the tab-button.
     */
    public function actionCompl()
    {
        readline_completion_function(array('self', 'completionCallback'));

        $command_input = readline("Custom command: ");

        passthru('echo ' . $command_input);
    }
}

$a = new AutoController();
$a->actionCompl();

About runtime autocomplete

As suggested by c9s it is possible to obtain this kind of functionality but extending autocomplete functionality of your bash, not with PHP.

So, if you see the autocomplete function of CLIFramework for example you'll see:

BashGenerator.php
ZshGenerator.php

Which is used to generate the bash script to extends the bash or zsh autocomplete.

So, it depends on the shell you are using the way you have to make the autocomplete.

Some references:

  • zsh autocompletion
  • bash autocompletion
  • fish autocompletion (the link is more python oriented than php...)
like image 7
borracciaBlu Avatar answered Oct 08 '22 13:10

borracciaBlu