Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Using Getopt::Long and prompting for arguments

by azredwing (Sexton)
on May 01, 2008 at 07:38 UTC ( [id://683873]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I'm relatively new at coding in Perl, but I've learned enough to get around. I recently discovered how awesome the CPAN really is, and I'm starting to switch my programs over to use Getopt::Long to capture command-line arguments that users may use on my programs.

One program I have written does data analysis and writes the results to a default location. I implemented a way to allow users to change the location to write to, but how I did this is not compatible with the Getopt::Long module, and quite frankly is extremely inelegant.

What I'd like to be able to do is the following (pseudo-code):

if($user_wants_to_save && ! $location_specified){ $save_location = ask_user_for_save_location(); }else{ $save_location = $location_specified; }

What I have thus implemented is:

GetOptions( 's|save_to:s' => \$user_wants{CUSTOM_SAVE_PLACE}, );

So if the user uses the -s or --save_to flag, it will use the next argument as the save location. But say the user *doesn't* specify the save location in the command line. How would I go about asking_user_for_location()?

Thanks all.

Replies are listed 'Best First'.
Re: Using Getopt::Long and prompting for arguments
by why_bird (Pilgrim) on May 01, 2008 at 08:25 UTC
    From the CPAN description of GetOpt::Long, for variables which can take a value:

    "In the option specification, the option name is followed by an equals sign = and the letter s. The equals sign indicates that this option requires a value. The letter s indicates that this value is an arbitrary string. Other possible value types are i for integer values, and f for floating point values. Using a colon : instead of the equals sign indicates that the option value is optional. In this case, if no suitable value is supplied, string valued options get an empty string '' assigned, while numeric options are set to 0."

    So if you set the value to be optional, and if a user does not enter a filename, the value of $user_wants{CUSTOM_SAVE_PLACE} will be ''.

    if ((defined $user_wants{CUSTOM_SAVE_PLACE}) && ($user_wants{CUSTOM_SA +VE_PLACE} == '')){ print "Please enter a save location\n"; ..etc. }


    edit:added 'if defined'
    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......
Re: Using Getopt::Long and prompting for arguments
by tachyon-II (Chaplain) on May 01, 2008 at 07:50 UTC
    my $SAVE_PLACE; GetOptions( 's|save_to:s' => \$SAVE_PLACE, ); # here the value of $SAVE_PLACE will be: # undef if no -s --save_to # 0 if -s --save_to but no optional argument # <str> if -s=<str> while ( defined $SAVE_PLACE and not $SAVE_PLACE ) { $SAVE_PLACE = prompt("Save to where: "); } sub prompt { print @_; chomp(my $ans = <>); $ans }
Re: Using Getopt::Long and prompting for arguments
by mscharrer (Hermit) on May 01, 2008 at 08:04 UTC
    From Perl Best Practices, page 220:
    use IO::Prompt; my $answer = prompt 'Please enter XXX: ';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://683873]
Approved by pKai
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found