How to add optional argument in perl -
i want command line argument parsing directory path in perl. want make argument optional. when user gives path, showed assigned variable $release_model else execute other code have written finding directory main directory. new perl somehow coded following. can help?
getopt::long $command_line = $getoptions(\%opts, "help|h","email=s@","test","model"); if($command_line==0){ print "$program: no arguments given"; usage{}; } else die "no arguments given" but doesn't accept model optional argument , throws error.
i started working perl.
it quite hard guess after code provided contains lots of errors , other features not described. start learning simple, here hope matches requirements.
#!/usr/bin/perl use strict; use warnings; use getopt::long; $release_model = '/path/to/"main directory"'; # default value getoptions( 'model=s' => \$release_model ) or die("error in command line arguments\n"); print "release model is: $release_model\n"; if save file (e.g. my_program.pl) , make executable can see provides these features:
- if call without arguments
./my_program.pl, default value of$release_modelused. - if call argument
model(e.g../my_program.pl --model /another/directory), provided value assigned$release_model. - if call wrong arguments (e.g.
./my_program.pl --mdoel), prints reasonable error message , exits.
try yourself. , go , read tutorial on perl if want serious work.
Comments
Post a Comment