m_jaser has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone,

I'm running into a scenario where i need to use GetOpt twice in the script, the first time is easy as it is parsing the @ARGV at the Script Input, however, the second time, i need to give 'STDIN'/<> for user to enter input which contains new directory (where the script will continue to execute) and also give the chance to change options provided in the beginning with @ARGV.

I'd be so grateful if you can help me resolve this scenario?

Here is an example at Script input: perl Analyzer.pl -t 2 -c "4 1 2 3 4" . . While the Script is still active, i have this example message: To process another log file, enter the path to the log file, or enter +'x' to exit: /cygdrive/h/eFUT_Testing/Test_Log/Log2 -t 1 -c "2 1 2"

Thank You

Replies are listed 'Best First'.
Re: How to process Getopt::Long from STDIN
by kcott (Archbishop) on Mar 12, 2017 at 04:49 UTC

    G'day m_jaser,

    Here's a very basic example of how you might go about this.

    #!/usr/bin/env perl use strict; use warnings; use Data::Dump; use Getopt::Long qw{GetOptionsFromString}; my ($x, $y, $z) = (0, 0, 0); my %opts = ('x=i', \$x, 'y=i', \$y, 'z=i', \$z); dd \%opts; GetOptions(%opts); dd \%opts; print 'Enter new: '; chomp(my $new = <>); GetOptionsFromString($new, %opts); dd \%opts;

    Sample run:

    $ pm_1184309_getopt_long_opts_from_string.pl -x 1 -y 2 { "x=i" => \0, "y=i" => \0, "z=i" => \0 } { "x=i" => \1, "y=i" => \2, "z=i" => \0 } Enter new: -y 3 -z 4 -x 0 { "x=i" => \0, "y=i" => \3, "z=i" => \4 }

    In "Re^2: How to process Getopt::Long from STDIN", you asked:

    "I'm not sure how this works with GetOptionsFromString perhaps?"

    That may answer your question. See the Getopt::Long documentation for details.

    — Ken

      Thanks a lot Ken...! That works perfectly.
Re: How to process Getopt::Long from STDIN
by stevieb (Canon) on Mar 12, 2017 at 03:02 UTC

    Speaking from some experience before any real thought, there are a couple of ways you may be able to do this, depending on what's actually going on in your code. So, with that said, it'd help if you could show the code that presents this problem (a small, but fully working example that demonstrates and duplicates the issue at hand).

      Thanks for your quick response...! Here's what the first part of the code looks like:

      my ( $Carrier_Option, $printHelp, $version, $Nr_of_TP, $is_BS, $my_legend, $curr_dir, ); GetOptions( "c|C|carriers=s"=>\$Carrier_Option, #string "t|T|TP=i"=>\$Nr_of_TP, #numeric "h|H|Help"=>\$printHelp, #boolean "v|V|Version"=>\$version, #boolean "b|B|BS|bs|Bs"=>\$is_BS, #boolean "l|l|legend=s"=>\$my_legend, #string "a|A|altitude=i"=>\$BS_RadioHeight, "d|D|Directory=s"=>\$curr_dir, );

      I run the script at first as follows:

      perl Analyzer.pl -t 2 -c "4 1 2 3 4"

      If the options are not entered, then default settings are considered in the script's execution, and the active directory is taken from the current local working directory. The second part of the script (which is where i need the help) is still not complete, but my plan is to have something as follows:

      print "To process another log file, enter the path to the log file alo +ng with the options, or enter 'x' to exit: "; my $New_User_Input=<STDIN>; chomp $New_User_Input; my $new_options=GetOptionsFromString($New_User_Input);

      I'm not sure how this works with GetOptionsFromString perhaps?? or some other method?

      I hope this makes it a little clearer to what I intend to do.

      Thanks...!