Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

option on command line

by Sara (Acolyte)
on Jul 28, 2002 at 00:55 UTC ( [id://185790]=perlquestion: print w/replies, xml ) Need Help??

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

Hello guys , how can I do this : I am runing my script passing it the -u option , user must use -u <file> to run the script ,,
perl myScript -u <file>
I would like to add another option like -x -m but I want them to be optional ,,, if the user doesn't pass them then it is fine thanks

Replies are listed 'Best First'.
Re: option on command line
by mfriedman (Monk) on Jul 28, 2002 at 01:03 UTC
Re: option on command line
by larsen (Parson) on Jul 28, 2002 at 11:06 UTC
    For this kind of purposes, I use Getopt::Mixed.
    #!/usr/bin/perl use strict; use Getopt::Mixed; use vars qw|$opt_u $opt_x $opt_m|; # Note that you have to use globals... Getopt::Mixed::getOptions( qq|u=s x:i m:i| ); # Option u wants (mandatory) a string # Option x accepts (not mandatory) an integer # Option m too... die "Hey man, you have to provide an username!\n" unless $opt_u ; print "Option 'u' is $opt_u\n";
    Now your script will accept options from the command line, storing them in globals and complaining if you use options without their mandatory argument (i.e., you can't use a bare -u on the command line).

    What makes Getopt::Mixed interesting is the possibility to specify longer synonyms for options. So, you can change a line of the script in this way:

    Getopt::Mixed::getOptions( qq|u=s username>u x:i m:i| );
    username is a synonym for u, so that you can call your script from commandline is these two ways with the same results:
    # getopt.pl -u larsen
    Option 'u' is larsen
    
    # getopt.pl --username larsen
    Option 'u' is larsen
    

    Update: Added an error message in case the user does not provide -u option

Re: option on command line
by Nightblade (Beadle) on Jul 28, 2002 at 01:07 UTC
    Sara, you could paste sample of your code, that we could know where is problem.

    You can do something like that:

    use Getopt::Std; getopts('u:x:m:', \%Opts) $file = $Opts{'f'} or die "Where is your file?"; $x = $Opts{'x'}; $m = $Opts{'m'};

    -f is required, -x and -m is optional,
Re: option on command line
by BazB (Priest) on Jul 28, 2002 at 12:59 UTC

    You should look at your code and simply extend the existing functionality using which ever method is being used already (likely Getopt::Std), unless you expect your argument parsing requirements to become substantially more complex, in which case the modules suggested by mfriedman and larsen will be of interest.

    On a related note, I notice that you have posted a number of SoPW's simply asking fellow Monks to craft code to solve (what appear to be related) problems.
    You should make the effort to understand the code you are faced with, then try to resolve the problem yourself - post your attempts, even if they are faulty!
    You should find that as you spend more time attempting to fix your own problems you should become familiar with basic functions and modules and it will much easier for you to program once you have that understanding.

    Cheers.

    BazB

Re: option on command line
by zentara (Archbishop) on Jul 28, 2002 at 17:11 UTC
    Hi, GetOpts is probably your best bet, but in case you
    want to do it without a module, try something like this.
    #!/usr/bin/perl use warnings; use strict; my $usage = "Usage: script -u username -x xval(optional) -m mval(optio +nal)\n"; my $username=undef; my $xvalue=undef; my $mvalue=undef; while(@ARGV){ my $argin = shift; if ($argin =~ /-u/){$username= shift} if ($argin =~ /-x/){$xvalue=shift} if ($argin =~ /-m/){$mvalue =shift} } if ( !defined $username ){print $usage;exit;} print "username = $username\n -x = $xvalue\n -m = $mvalue\n";
Re: option on command line
by thor (Priest) on Jul 29, 2002 at 09:31 UTC
    I'm a big fan of the ternary operator in cases like this.
    $var_for_opt_a = defined($opt_a) ? $opt_a : "some default value";
    As the others said above, use one of the GetOpt modules as well.

    thor

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found