Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Help with parsing command line to make more readable

by kcott (Archbishop)
on Aug 20, 2018 at 12:19 UTC ( [id://1220712]=note: print w/replies, xml ) Need Help??


in reply to Help with parsing command line to make more readable

G'day proxie,

Welcome to the Monastery.

"Can someone help me get started on the most efficient reg exps to do this?"

When Perl's string handling functions can do the job, you'll generally find that they'll be measurably, more efficient than regular expressions. Here's a solution that doesn't use regular expressions. Use Benchmark to compare the efficiency of this code against other posted solutions.

#!/usr/bin/env perl use strict; use warnings; my $cmd = q{execute test_number_1 -tex -tex_args -sub_args +debug_dir= +./ -sub_args +debug_dir=./ -constraint parity_en,random_en -sub_args +'"' ruck=1 '"' -constraint dual_en -sub_args -cd -sub_args 2596.slow +-sub_args test -seed 1 -tex_args- -opt 1 -tag 2}; my $TAB = ' ' x 4; my $depth = 0; my @tokens = split ' ', $cmd; my @parts; cmd_format(\@tokens, \@parts, $depth); print "$_\n" for @parts; sub cmd_format { my ($tokens, $parts, $depth) = @_; for (my $i = 0; $i <= $#$tokens; ++$i) { my $token = $tokens->[$i]; if (-1 == index $token, '-', 0) { my @starts = ($token); while (-1 == index $tokens[$i + 1], '-', 0) { push @starts, $tokens[++$i]; } push @$parts, $TAB x $depth . "@starts"; } elsif ($token eq '-tex') { push @$parts, $TAB x ($depth + 1) . $token; cmd_format([@{$tokens}[$i + 1 .. $#$tokens]], $parts, $dep +th + 2); last; } elsif ($token eq '-tex_args') { push @$parts, $TAB x $depth . $token; my $args = []; while ($tokens->[$i + 1] ne '-tex_args-') { push @$args, $tokens->[++$i]; } cmd_format($args, $parts, $depth + 1); push @$parts, $TAB x $depth . $tokens->[++$i]; } else { my @opts = ($token, $tokens->[++$i]); for ($i + 1 .. $#$tokens) { last unless -1 == index $tokens->[$i + 1], '-', 0; push @opts, $tokens->[++$i]; } push @$parts, $TAB x $depth . "@opts"; } } }

Output:

execute test_number_1 -tex -tex_args -sub_args +debug_dir=./ -sub_args +debug_dir=./ -constraint parity_en,random_en -sub_args '"' ruck=1 '"' -constraint dual_en -sub_args -cd -sub_args 2596.slow -sub_args test -seed 1 -tex_args- -opt 1 -tag 2

— Ken

Replies are listed 'Best First'.
Re^2: Help with parsing command line to make more readable
by proxie (Novice) on Aug 21, 2018 at 03:52 UTC
    Thanks for the suggestion!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1220712]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found