Parsing a string with GetOpt Long GetOptions

0 votes

I need to parse a string in the same manner as CLI arguments upon being passed to Getopt::Long. The string could have possible command line arguments that it gets by using a Read-Eval-Print-Loop program. Like say, this is my string:

$str = '--infile /tmp/infile_location --outfile /tmp/outfile'

Now, I need to have it parsed with GetOptions in order to have the scope for adding new options later.
I figured maybe splitting the string at its whitespaces and replacing @ARGV with new array could let me call GetOptions on it. So, here's how I've tried doing it so far:

my @arg_arr = split (/\s/, $input_line);

# This is done so that GetOptions reads these new arguments
@ARGV = @arg_arr;
print "ARGV is : @ARGV\n";
GetOptions (
            'infile=s'  => \$infile,
            'outfile=s' => \$outfile

But this just seems like a workaround, is there a more direct approach or a better way to do it? Please Help!

Nov 15, 2018 in Others by Bharani
• 4,620 points

1 answer to this question.

0 votes

Hey, have a look at the section where it says parsing options from an arbitrary string in the man page for Getopt::Long. It seems to be doing the exact same thing you're trying to accomplish.

answered Nov 15, 2018 by nirvana
• 3,150 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

Execute a shell script with php

One of the possible reason for this ...READ MORE

answered Feb 24, 2019 in Others by Omkar
• 69,170 points
+1 vote
0 answers

How to split a number with coma betweeen two colunms

Jul 3, 2019 in Others by anonymous
0 votes
1 answer

How to deal with a Project Manager with controlling nature?

It depends on whether the Project Manager ...READ MORE

answered Jul 29, 2020 in Others by Sirajul
• 59,090 points
0 votes
1 answer

Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery.

Hi@akhtar, You need a MaterialApp or a WidgetsApp around your widget. They ...READ MORE

answered Aug 27, 2020 in Others by MD
• 95,300 points
0 votes
1 answer

Issue with the perl script

I don't really know what might the ...READ MORE

answered Jun 22, 2018 in Other DevOps Questions by ajs3033
• 7,280 points
0 votes
1 answer

Bitcoin private key: translate from bash to perl

use bignum; # Get arbitrary precision arithmetic # ...READ MORE

answered Sep 4, 2018 in Blockchain by slayer
• 29,310 points
0 votes
1 answer

Using Bitcoin's sendmany function with perl

Replace the line  my $action = $api->call('sendmany','',%recipients); with  my ...READ MORE

answered Sep 11, 2018 in Blockchain by digger
• 26,720 points
0 votes
1 answer

JSON valid numerical param in Perl

Use 0.0 + sprintf('%.8f', $btc), 0.0 + $data{'amount'} READ MORE

answered Sep 11, 2018 in Blockchain by digger
• 26,720 points