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,550 points
61 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,040 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 23 in Others by Omkar
• 65,850 points
21 views
0 votes
1 answer
0 votes
1 answer

Validating String against USPS state abbreviations

This is how I'd do it: private static ...READ MORE

answered Nov 9, 2018 in Others by DataKing99
• 8,100 points
49 views
0 votes
1 answer

Using/Handling colon inside a JSF generated HTML element ID in CSS selector

Yes, you can.  Just Backslash (\) the colon.  Like ...READ MORE

answered Nov 14, 2018 in Others by DataKing99
• 8,100 points
144 views
0 votes
0 answers

How to communicate gRPC example working with Node.js?

Hi Guys, I am working as a Node.js ...READ MORE

Dec 5, 2018 in Others by camillelola
• 140 points
143 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,040 points
26 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,040 points
144 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
• 27,620 points
48 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
• 27,620 points
17 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.