Sponsored Content
Top Forums Shell Programming and Scripting [Perl] Command option with optional value. Post 302885687 by ejdv on Tuesday 28th of January 2014 02:39:44 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Params::Validate::Dummy(3pm)				User Contributed Perl Documentation			      Params::Validate::Dummy(3pm)

NAME
Params::Validate::Dummy - Stub for Params::Validate SYNOPSIS
use Params::Validate::Dummy qw(); use Module::Optional qw(Params::Validate); DESCRIPTION
This module provides stub routines for those who don't have Params::Validate installed. For more details, please refer to the documentation for Params::Validate. The code here is just stub routines which do NOTHING at all, passing through any arguments in the API and prototypes of Params::Validate. In particular, the dummy stubs do not do defaulting, validation, untainting or anything else that Params::Validate does. If you need this functionality, either provide it yourself in the surrounding code, or don't use this module and insist that the real Params::Validate is installed. "validate", "validate_pos" The parameter list is passed through as a return value. "validate_with" Returns the value of the params option. "set_options", "validation_options" These do nothing at all. Data types: "SCALAR", "SCALARREF", "ARRAYREF", "HASHREF", "GLOB", "GLOBREF", "BOOLEAN", "CODEREF", "HANDLE", "OBJECT", "UNDEF", "UNKNOWN" In the Params::Validate module, these are constants implemented by subs that return numbers. This module implements the same functions. SUPPORT
See Module::Optional perl v5.10.1 2010-12-17 Params::Validate::Dummy(3pm)
All times are GMT -4. The time now is 10:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy