Hello Monks!
I have script which uses GetOptions to parse options. My script allows the to use the --job option to pass everything the user wants (scripts, additional parameters, etc.). For example:
my_script.pl --option1 --option2 --job some_job.pl -option1 --option2
+--option3
My script runs the
some_job.pl -option1 --option2 --option3 part at some point. Before the GetOptions parse, I have the following code:
my $job_flag = 0;
foreach my $i (0..$#ARGV) {
​$opt{"job"} .= $ARGV[$i]." " if($job_flag);
​$job_flag = 1 if($ARGV[$i] =~ /^(\-\-|\-)job$/);
​undef $ARGV[$i] if($job_flag);
}
The code just saves everything after --job into the $opt{job} and removes it from the ARGV so GetOptions won't parse it. Now I noticed that if there is quotes inside the job part, it will remove those quotes. For example:
my_script.pl --option1 --option2 --job some_job.pl -option1 'some_job2
+.pl -x abc' -option2
Then the Dumper of ARGS is:
$VAR1 = [
+
+
'--option1',
'--option2',
'--job',
'some_job.pl',
'-option1',
'some_job2.pl -x abc',
'-option2'
];
I would expect:
$VAR1 = [
+
+
'--option1',
'--option2',
'--job',
'some_job.pl',
'-option1',
'\'some_job2.pl -x abc\'',
'-option2'
];
How can I know that there were quotes? I guess I'll need to think of another way to do this.
So there won't happen and X-Y problem, I'll explain what I'm trying to do. I want to get user's command and execute it at some point. It could have parameters and I don't want the user to insert it into file first. How can it be done?
2021-06-25 Athanasius fixed code tag.