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,660 points
1,669 views

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,130 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

Open S3 object as a string with Boto3

s3 = boto3.resource('s3') import boto3 s3 = obj obj(bucket, ...READ MORE

answered Mar 21, 2022 in Others by gaurav
• 23,260 points
965 views
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,220 points
9,679 views
+1 vote
0 answers

How to split a number with coma betweeen two colunms

Jul 3, 2019 in Others by anonymous
870 views
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,300 points
901 views
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,370 points
1,303 views
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,740 points
1,428 views
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,740 points
639 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP