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

Here's another step along the way. Based on tybalt89's beginning. Handles arbritrary nesting levels (I think). (Update: Does not handle arbitrary nesting levels of identical switches. E.g., change  -fizz_boff ... -fizz_boff- to  -tex_args ... -tex_args- in the example code below. (sigh)) Lots of assumptions about syntax of the c.l. This needs more testing, and I'm sure proxie will be back with lots of updated and expanded specifications.

use 5.014; # needs /r modifier for s/// (also uses 5.10+ \K) use warnings; use strict; use Data::Dump qw(pp); my $cl = q(execute test_number_1 -tex -tex_args -sub_args +debug_dir=. +/ -fizz_boff -sub_args +debug_dir=./ -constraint parity_en,random_en +-sub_args '"' ruck=1 '"' -bar -xyzzy -bar- -constraint dual_en +-sub_args -cd -sub_args 2596.slow -fizz_boff- -sub_args test -seed 1 +-tex_args- -opt 1 -foo -x y -zz 42 -foo- -tag 2 -last 1); print qq{>$cl< \n\n}; my $in = ' '; # indentation for each level of nesting my $rin = qr{ \Q$in\E }xms; # regex for 1 indent level my $clump = qr{ (?: -sub_args \s+ \S+ | \S) .*? (?= \s+ - | \Z) }xms; my $answer; $answer .= s/^-/$in-/gmr . qq{\n} for $cl =~ m{ $clump }xmsg; # ok pp $answer; print "\n\n"; 1 while $answer =~ s{ (^ ($rin+) - [^\n]+) \n \K ((?: ^ \2 - [^\n]+ \n)*?) (?= ^ \1 -) } { $3 =~ s{^}{$in}xmsgr }xmsge; print qq{>>>$answer<<< \n\n};
Output:
c:\@Work\Perl\monks\proxie>perl prettyprint_cl_1.pl >execute test_number_1 -tex -tex_args -sub_args +debug_dir=./ -fizz_bo +ff -sub_args +debug_dir=./ -constraint parity_en,random_en -sub_args +'"' ruck=1 '"' -bar -xyzzy -bar- -constraint dual_en -sub_args +-cd -sub_args 2596.slow -fizz_boff- -sub_args test -seed 1 -tex_args- + -opt 1 -foo -x y -zz 42 -foo- -tag 2 -last 1< "execute test_number_1\n -tex\n -tex_args\n -sub_args +debug_ +dir=./\n -fizz_boff\n -s ub_args +debug_dir=./\n -constraint parity_en,random_en\n -sub_a +rgs '\"' ruck=1 '\"'\n -bar \n -xyzzy\n -bar-\n -constraint dual_en\n -sub_args -cd\n + -sub_args 2596.slow\n -f izz_boff-\n -sub_args test\n -seed 1\n -tex_args-\n -opt 1 +\n -foo\n -x y\n -zz 42\n -foo-\n -tag 2\n -last 1\n" >>>execute test_number_1 -tex -tex_args -sub_args +debug_dir=./ -fizz_boff -sub_args +debug_dir=./ -constraint parity_en,random_en -sub_args '"' ruck=1 '"' -bar -xyzzy -bar- -constraint dual_en -sub_args -cd -sub_args 2596.slow -fizz_boff- -sub_args test -seed 1 -tex_args- -opt 1 -foo -x y -zz 42 -foo- -tag 2 -last 1 <<<
Sorry about any wraparound.


Give a man a fish:  <%-{-{-{-<

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:53 UTC
    This is good enough to get ideas, thank!