Quantcast
Channel: PHP Gangsta - Der PHP Blog mit Praxisbezug » CLI
Viewing all articles
Browse latest Browse all 2

History und Autocompletion in Konsolentools mit readline

$
0
0

In automatischen Scripten nutzt man Parameter die man seinem PHP Script übergibt damit es weiß was es tun soll. Doch manchmal ist es auch angenehm bei einem manuellen Aufruf den User nach Informationen zu fragen. Dazu gibt es in PHP die readline* Funktionen. Damit diese genutzt werden können muss PHP mit readline-Support kompiliert sein.

Falls das nicht der Fall ist und man PHP selbst kompiliert geht das so:

apt-get install libreadline-dev

und dann muss PHP mit dem Parameter “–with-readline” kompiliert werden.

Ein erstes Beispielscript zeigt wie man die History füllen, löschen und auflisten kann.

#!/usr/bin/php
<?

$historyFile = './readline.hist';
if (is_file($historyFile)) {
    readline_read_history($historyFile);
}

while (true) {
    $line = strtolower(trim(readline("Command: ")));
    echo 'Received [ ' . $line . ' ]' . PHP_EOL;
    readline_add_history($line); // add command to history
    // check special actions
    switch ($line) { // check special actions
        case 'clear': // clear the history
            readline_clear_history();
            break;
        case 'history': // print out the history
            print_r(readline_list_history());
            break;
        case 'quit':
        case 'exit': // quit / exit
            break 2;
        default:
            break;
    }
}

readline_write_history($historyFile);

Eine Benutzung könnte beispielsweise so aussehen:

# ./admintool
Command: show articles
Received [ show articles ]
Command: add user joe
Received [ add user joe ]
Command: history
Received [ history ]
Array
(
    [0] => show articles
    [1] => add user joe
    [2] => history
)
Command: clear
Received [ clear ]
Command: history
Received [ history ]
Array
(
    [0] => history
)
Command: quit
Received [ quit ]

Die History ist auch beim nächsten Aufruf von admintool noch vorhanden da sie in der Datei “readline.hist” permanent gespeichert wird. Mit “Cursor hoch” und “Cursor runter” kann man wie gewohnt durch die letzten Befehle navigieren.

Aus der Konsole kennt man auch die Auto Vervollständigung. Man beginnt einen Befehl zu tippen, drückt “Tab” und der Befehl wird vervollständigt. Auch das ist möglich:

#!/usr/bin/php
<?

readline_completion_function('readlineCompletion');

function readlineCompletion($string, $index) {
    $completion = array(
        'history',
        'quit',
        'exit',
        'clear',
        'clean',
        'cls'
    );
    $matches = array();
    foreach ($completion as $c) {
        if (!empty($string) && strpos($c, $string) === 0) {
            $matches[] = $c;
        }
    }

    return $matches;
}

$line = strtolower(trim(readline("Command: ")));

Ein Beispielaufruf

# ./admintool2
Command: cl<tab><tab>
clean  clear  cls
Command: cl

So kann man also dem Benutzer viel Tipperei und Sucherei in der Hilfe ersparen.

Weitere coole Konsolen-, Cronjob-, Daemon- und Angel-Tipps findet ihr in den tollen Slides von Jeroen Keppens.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images