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:

  1. if call without arguments ./my_program.pl, default value of $release_model used.
  2. if call argument model (e.g. ./my_program.pl --model /another/directory), provided value assigned $release_model.
  3. 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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -