-
Notifications
You must be signed in to change notification settings - Fork 0
Parser
Er Galvão Abbott edited this page Sep 29, 2024
·
4 revisions
The following sample was created from my bash
history
, and examples from a couple of man pages:
exit 0
rm -rf ~/foo/test/
cd bin/
ping www.google.com
zypper se traceroute
sudo zypper in traceroute
sudo vi /etc/routes
ls -lAR
ls -lAR /home/zaphod/
ls -lA --recursive /home/zaphod/
nvim --version
git merge --allow-unrelated-histories master
composer require laminas/laminas-log
chown -R zaphod:wwwrun /srw/www/htdocs
git push --set-upstream origin master
ls --classify=always
exit
shelly-api's parser treats the each of the above sample correctly.
- Only verbose flags may contain a value (e.g.:
--classify=always
) and in that case must use the equal sign as an operator and must not contain spaces between operands and operator.
#!/usr/bin/php
<?php
$samples = [
'exit 0',
'rm -rf ~/foo/test/',
'cd bin/',
'ping www.google.com',
'zypper se traceroute',
'sudo zypper in traceroute',
'sudo vi /etc/routes',
'ls -lAR',
'ls -lAR /home/zaphod/',
'ls -lA --recursive /home/zaphod/',
'nvim --version',
'git merge --allow-unrelated-histories master',
'composer require laminas/laminas-log',
'chown -R zaphod:wwwrun /srw/www/htdocs/',
'git push --set-upstream origin master',
'ls --classify=always',
'exit',
];
foreach($samples as $input) {
$segments = explode(' ', trim($input));
$command = array_shift($segments);
echo PHP_EOL . '-----------------------------------------------------------------------------' . PHP_EOL;
echo PHP_EOL . 'Input: ' . $input . PHP_EOL;
echo PHP_EOL . 'Command: ' . $command;
foreach ($segments as $segment) {
$output = '';
$trimmedSegment = trim($segment, ' -');
if ($segment[0] === '-') {
$output .= 'Flag';
if ($segment[1] === '-') {
list($name, $value) = explode('=', $trimmedSegment);
$output = 'Verbose ' . $output . ': ' . $name . ($value === null ? '' : ' with value: ' . $value);
} else {
if (strlen($trimmedSegment) > 1) {
$output .= 's: ' . implode(', ', str_split($trimmedSegment));
} else {
$output .= ': ' . $trimmedSegment;
}
}
} else {
$output .= 'Subcommand / Argument: ' . $trimmedSegment;
}
echo PHP_EOL . $output;
}
echo PHP_EOL;
}
-----------------------------------------------------------------------------
Input: exit 0
Command: exit
Subcommand / Argument: 0
-----------------------------------------------------------------------------
Input: rm -rf ~/foo/test/
Command: rm
Flags: r, f
Subcommand / Argument: ~/foo/test/
-----------------------------------------------------------------------------
Input: cd bin/
Command: cd
Subcommand / Argument: bin/
-----------------------------------------------------------------------------
Input: ping www.google.com
Command: ping
Subcommand / Argument: www.google.com
-----------------------------------------------------------------------------
Input: zypper se traceroute
Command: zypper
Subcommand / Argument: se
Subcommand / Argument: traceroute
-----------------------------------------------------------------------------
Input: sudo zypper in traceroute
Command: sudo
Subcommand / Argument: zypper
Subcommand / Argument: in
Subcommand / Argument: traceroute
-----------------------------------------------------------------------------
Input: sudo vi /etc/routes
Command: sudo
Subcommand / Argument: vi
Subcommand / Argument: /etc/routes
-----------------------------------------------------------------------------
Input: ls -lAR
Command: ls
Flags: l, A, R
-----------------------------------------------------------------------------
Input: ls -lAR /home/zaphod/
Command: ls
Flags: l, A, R
Subcommand / Argument: /home/zaphod/
-----------------------------------------------------------------------------
Input: ls -lA --recursive /home/zaphod/
Command: ls
Flags: l, A
Verbose Flag: recursive
Subcommand / Argument: /home/zaphod/
-----------------------------------------------------------------------------
Input: nvim --version
Command: nvim
Verbose Flag: version
-----------------------------------------------------------------------------
Input: git merge --allow-unrelated-histories master
Command: git
Subcommand / Argument: merge
Verbose Flag: allow-unrelated-histories
Subcommand / Argument: master
-----------------------------------------------------------------------------
Input: composer require laminas/laminas-log
Command: composer
Subcommand / Argument: require
Subcommand / Argument: laminas/laminas-log
-----------------------------------------------------------------------------
Input: chown -R zaphod:wwwrun /srw/www/htdocs/
Command: chown
Flag: R
Subcommand / Argument: zaphod:wwwrun
Subcommand / Argument: /srw/www/htdocs/
-----------------------------------------------------------------------------
Input: git push --set-upstream origin master
Command: git
Subcommand / Argument: push
Verbose Flag: set-upstream
Subcommand / Argument: origin
Subcommand / Argument: master
-----------------------------------------------------------------------------
Input: ls --classify=always
Command: ls
Verbose Flag: classify with value: always
-----------------------------------------------------------------------------
Input: exit
Command: exit