fork download
  1. use Data::Dumper;
  2. $_ = 'command1 --max-size=2M a=1 --fast =2 --all --type="some value" --b= --c=1';
  3. my ($command, @switch, %param, @error);
  4. while (/\G\s*((?!--)\S+)?(?(1)|\s+(?>--|)((?<=--)[a-z\d-]+)(="?|(?=\s))((?<![="])|(?<=")[^"]*(?=")|(?<==)(?!")\S*(?!"))"?(?=\s|$))/ig) {
  5. push(@error, $1) if ($1) && ($command);
  6. $command = $1 if ($1) && !($command);
  7. push(@switch, $2) if !($1) && !($3);
  8. $param->{$2} = $4 if !($1) && ($3);
  9. }
  10. print "Command:\n'$command'\n";
  11. print "\n";
  12. print "Switch(es):\n";
  13. print "'$_'\n" for (@switch);
  14. print "\n";
  15. print "Parameters:\n";
  16. print Dumper($param);
  17. print "\n";
  18. print "Syntax error(s):\n";
  19. print "'$_'\n" for (@error);
Success #stdin #stdout 0.01s 5164KB
stdin
Standard input is empty
stdout
Command:
'command1'

Switch(es):
'fast'
'all'

Parameters:
$VAR1 = {
          'c' => '1',
          'b' => '',
          'type' => 'some value',
          'max-size' => '2M'
        };

Syntax error(s):
'a=1'
'=2'