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
Kwiki::Command(3pm)					User Contributed Perl Documentation				       Kwiki::Command(3pm)

NAME
Kwiki::Command - Kwiki Command Line Tool Module SYNOPSIS
> kwiki -new mykwiki > cd mykwiki > kwiki -install Kwiki::RecentChanges Kwiki::Archive::Rcs Kwiki::Revisions > vim config.yaml > kwiki -update > kwiki -remove RecentChanges > kwiki -new_view admin DESCRIPTION
Kwiki::Command is the module that does all the work of the "kwiki" command line tool. You can use "kwiki" to install a new Kwiki, to update a Kwiki configuration, to add and remove Kwiki plugins and to download Kwiki plugins from CPAN. When you download the CPAN modules they can either be installed in the system Perl libraries or locally right in your kwiki dist. This is useful if you don't have root permissions for your installation. USAGES
There are many different commands you can do with the "kwiki" command line tool. o -new Create a new kwiki with the command: kwiki -new You must be inside an empty directory. Alternatively you can say: kwiki -new path/to/kwiki The target directory must be empty or must not exist yet. AUTHOR
Brian Ingerson <INGY@cpan.org> COPYRIGHT
Copyright (c) 2004. Brian Ingerson. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html perl v5.12.4 2011-09-17 Kwiki::Command(3pm)
All times are GMT -4. The time now is 05:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy