[Perl] Command option with optional value.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Perl] Command option with optional value.
# 1  
Old 01-28-2014
[Perl] Command option with optional value.

Hi,

I would like to parse command line arguments.
The problem I am facing is that I cannot get it right when an option has a optional value.

Example:
# ./ej.pl --remove
# ./ej.pl --remove all

And the script may allow only one option.
So this is wrong:
# ./ej.pl --dummy --remove

I know that Getopt::Long has the colon available to make an option optional, but how to make an option value optional ??

This is what I have come up with so far, as a basis:

Code:
#!/usr/bin/perl -w #-d
use strict;
use File::Basename;
use Getopt::Long;

my $Program=basename($0);
my %Opts;
my $ArgvNum = @ARGV;

printf "Number of command-line arguments : $ArgvNum\n";

my $OptHelp = 0;
my $OptDummy = 0;
my $OptRemoveLog = 0;

GetOptions (
  'help'    => \$OptHelp,
  'dummy'    => \$OptDummy,
  'remove'   => \$OptRemoveLog,
) or &Usage;

printf "OptHelp = $OptHelp\n";
printf "OptDummy = $OptDummy\n";
printf "OptRemoveLog = $OptRemoveLog\n";

if ($OptHelp) { &Usage; }

if ($OptRemoveLog) {
  &RemoveLog($OptRemoveLog);
  exit 0;
}

if ($OptDummy) {
  &Dummy;
  exit 0;
}

printf "No valid options given\n";
exit 0;

sub Usage() {
die "
Usage:\t$Program [-D] [-R [<option>]] [-h]

--dummy           : runs Dummy
--remove [value]  : removes the log files
                    where [value] is:
                      all
                      log
                      save
                   default value is log
--help           : prints this information

";
}

sub Dummy() {
  printf "Executing Dummy sub\n";
  exit 0;
}

sub RemoveLog() {
  my $Mode = shift;
  printf "Executing RemoveLog sub\n";
  printf "RemoveLog Mode = $Mode\n";
  exit 0;
}

Thanks in advance for suggestions.

ejdv
# 2  
Old 01-28-2014
The best way to do this is to not do this. As you have seen, optional option arguments lead to ambiguous command lines. The only time you can reliably parse them is if they are the last option on the command line and there are no operands.

In VERY specific cases, you can look at an option argument and know that it is an invalid option argument (and in that case use the default), but a typo on a command line in that case can lead to unexpected (and frequently hard to debug) behavior.
# 3  
Old 01-28-2014
Thanks for the quick reply.

So, when I understand it correct, you are saying it is better to always require an option value ?

Like this:
Code:
  'remove=s'   => \$OptRemoveLog,

I guess that is workable.
Why to make it too complicated ? Smilie
# 4  
Old 01-28-2014
Quote:
Originally Posted by ejdv
Thanks for the quick reply.

So, when I understand it correct, you are saying it is better to always require an option value ?

Like this:
Code:
  'remove=s'   => \$OptRemoveLog,

I guess that is workable.
Why to make it too complicated ? Smilie
Yes! An option that takes an option argument should always expect an option argument to be present.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Perl debuggin option

I am getting out of memory issue in perl. I need to debug which function taking more memory constraints. what the commands to find out the memory consuming in the perl program. (1 Reply)
Discussion started by: ramkumar15
1 Replies

3. Solaris

Fsck command without any option

Dear all, I want to execute fsck command,can i execute fsck command without any option asking for more confidence. Thanks and Regards Monoj Das (1 Reply)
Discussion started by: monojcool
1 Replies

4. Shell Programming and Scripting

How to use a variable as a command option?

I am just learning shell scripting and already I found out I have the bad habit of thinking that it is similar to php or c. I learned some basics and now encountered this problem: On shell it is possible to type: $ date --date="2009-10-10 09:08:34" Sat Oct 10 09:08:34 CEST 2009 ... (2 Replies)
Discussion started by: quinestor
2 Replies

5. Shell Programming and Scripting

perl script command line option driven script

could someone show me a sample command line option driven script? i want to see an easy way to write one and how i can execute it using command line options such as typing in read.pl -i <id> -c <cmds> -s <start> -e <end> would read out all the commands run by ID . from start time to... (7 Replies)
Discussion started by: kpddong
7 Replies

6. Shell Programming and Scripting

-n option with grep command

Hi, what is the meaning of -n option before the grep command ? grep command searches for the specified string in the file tmp_crontab.txt but what does -n mean ? With Regards (1 Reply)
Discussion started by: milink
1 Replies

7. HP-UX

who command option not working

Running HP 11.31 on a HP3600. But when I log in as a user the who command works but if I use an option like "who -m" I get nothing. Any thoughts on what is causing this problem. (11 Replies)
Discussion started by: KMRWHUNTER
11 Replies

8. Shell Programming and Scripting

run a perl with option

I have a perl script which runs and send out e-mail if duplicates are found. I would like to run the with option (like -e) so that it will produce the out put only and will not send out e-mail. How can I achieve it. I would appreciate the help. Thanks. Example: ./file1 (sends out e-mail)... (5 Replies)
Discussion started by: amir07
5 Replies

9. Solaris

Why does the 'ps' command with -u option not working?

How can I use the 'ps' command to view current sessions but only for a given process/user, with the -u parm? In older versions of Unix, this used to work, but not in Sun Solaris. Thanks (4 Replies)
Discussion started by: ElCaito
4 Replies

10. UNIX for Dummies Questions & Answers

option for ls command

i'm using SunOS 5.7 and I know theres a ls option for seeing what kind of files are in a directory. I was wondering if there was a ls option that could see if the files are txt or files that can be opened in vi (1 Reply)
Discussion started by: eloquent99
1 Replies
Login or Register to Ask a Question