LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2018, 02:19 AM   #1
fritz001
Member
 
Registered: Aug 2004
Posts: 174

Rep: Reputation: 18
Exclamation Perl: script command line parameters restrictions


Hello,

So here is the part where getopts is defined:

Code:
my $ret = GetOptions (
      "mgmt|m=s"     => \$mgmt,
      "file|f=s"     => \$file,
      "user|u=s"     => \$cuser,
      "password|p=s" => \$cpass,
      "help|h|?"     => \$help,
      "add|a=s"      => \$is_added,
      "delete|d=s"   => \$is_deleted,
 ) or die ("Something went wrong!\n");
usage() and exit 0 if $help;
What I couldn't figure out how to do it properly :
1. I would like parameters mgmt,file,user,password to be mandatory ; (add or delete to be optional )
2. if parameter "add" is used "delete" to be excluded and vice-versa I mean to force somehow both parameters "add" and "delete" not to be used together.

Any hints/ideas are greatly appreciated !!
 
Old 02-22-2018, 06:45 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Add a test for mandatory variables, something like
Code:
my %mandatory = (
	'mgmt' => $mgmt,
	'file' => $file,
	'user' => $cuser,
	'password' => $cpass
);

die "Error: --$_ is mandatory\n" for grep {!$mandatory{$_}} keys %mandatory;
And another test for add and delete options
Code:
# which has the priority?
# if $is_added
undef $is_deleted if $is_added;
# or if $is_deleted
# undef $is_added if $is_deleted;
 
Old 02-22-2018, 03:51 PM   #3
fritz001
Member
 
Registered: Aug 2004
Posts: 174

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by keefaz View Post
Add a test for mandatory variables, something like
Code:
my %mandatory = (
	'mgmt' => $mgmt,
	'file' => $file,
	'user' => $cuser,
	'password' => $cpass
);

die "Error: --$_ is mandatory\n" for grep {!$mandatory{$_}} keys %mandatory;
And another test for add and delete options
Code:
# which has the priority?
# if $is_added
undef $is_deleted if $is_added;
# or if $is_deleted
# undef $is_added if $is_deleted;
Thanks, just saved my day !!
 
Old 03-06-2018, 08:54 AM   #4
fritz001
Member
 
Registered: Aug 2004
Posts: 174

Original Poster
Rep: Reputation: 18
Well, better to resurrect this thread:

I'm interested in modifying certain requirements

As previously explained, script should require certain parameters

However here is what I'd like to change

1. script require as mandatory parameter --action
1a. if --action =ADD => parameter --file should be mandatory also
1b. if --action =DELETE => parameter --file to be optional

2. to prevent multiple usage of same parameter --action ADD --action DELETE

just an example:
./script.pl --mgmt XXX --user AAA --password VVV --action ADD --file users.txt
./script.pl --mgmt XXX --user AAA --password VVV --action DELETE --file users.txt ( in this case file can be optional parameter )

So far couldn't find anywhere this kind of complex example ....
 
Old 03-06-2018, 11:36 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
There is more than one way of course, basically it's simple as testing $action value
Code:
# -f test is same as in shell for testing if $file exists and is a file

die "Error: bad file '$file' in --file option\n" if $action eq 'ADD' and ! -f $file;
Of course an error function would be prefered than die in a finished script

Last edited by keefaz; 03-06-2018 at 11:37 AM.
 
Old 03-07-2018, 02:07 AM   #6
fritz001
Member
 
Registered: Aug 2004
Posts: 174

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by keefaz View Post
There is more than one way of course, basically it's simple as testing $action value
Code:
# -f test is same as in shell for testing if $file exists and is a file

die "Error: bad file '$file' in --file option\n" if $action eq 'ADD' and ! -f $file;
Of course an error function would be prefered than die in a finished script
Meantime choose something like

Code:
if (!$file){
        if ($action eq "add"){
                print "Option --action require --file\n";
                exit 101;
        }
}

But what would be the best to prevent double usage of parameter: ./script.pl --action ADD --action DELETE ?
 
Old 03-07-2018, 08:17 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
You have to choose which option has priority over the other option
ie: if both options are set, which one to keep, which one to discard?
 
Old 03-08-2018, 02:30 AM   #8
fritz001
Member
 
Registered: Aug 2004
Posts: 174

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by keefaz View Post
You have to choose which option has priority over the other option
ie: if both options are set, which one to keep, which one to discard?
in this case only "--action ADD" should have priority if both are used
 
Old 03-08-2018, 02:41 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,363

Rep: Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418
that is solved in post #2 I think. Another approach can be to use:
Code:
my $ret = GetOptions (
      "mgmt|m=s"      => \$mgmt,
      "file|f=s"      => \$file,
      "user|u=s"      => \$cuser,
      "password|p=s"  => \$cpass,
      "help|h|?"      => \$help,
      "operation|o=s" => \$operation,
 ) or die ("Something went wrong!\n");

if $operation eq "add" => do an add
if $operation eq "del" => do a delete
else ???
 
Old 03-09-2018, 09:32 AM   #10
fritz001
Member
 
Registered: Aug 2004
Posts: 174

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by pan64 View Post
that is solved in post #2 I think. Another approach can be to use:
Code:
my $ret = GetOptions (
      "mgmt|m=s"      => \$mgmt,
      "file|f=s"      => \$file,
      "user|u=s"      => \$cuser,
      "password|p=s"  => \$cpass,
      "help|h|?"      => \$help,
      "operation|o=s" => \$operation,
 ) or die ("Something went wrong!\n");

if $operation eq "add" => do an add
if $operation eq "del" => do a delete
else ???
Well< i think I've choose this solution not sure if 100% proof:

if operation eq add && operation eq del => $operation=add
 
Old 03-09-2018, 09:38 AM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I didn't think enough with script options...
If operation option is passed twice at command line, it seems only the last value is stored in $operation

please do some tests

Last edited by keefaz; 03-09-2018 at 09:46 AM.
 
Old 03-10-2018, 02:06 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,363

Rep: Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418
Quote:
Originally Posted by fritz001 View Post
if operation eq add && operation eq del => $operation=add
operation cannot be eq add and eq del in the same time, that is nonsense.
 
Old 03-10-2018, 05:44 PM   #13
fritz001
Member
 
Registered: Aug 2004
Posts: 174

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by pan64 View Post
operation cannot be eq add and eq del in the same time, that is nonsense.
this is a script that is supposed to be run by multiple users; I have some of them trying to run it like this ./script.pl --action ADD --action DEL
 
Old 03-10-2018, 07:23 PM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by fritz001 View Post
this is a script that is supposed to be run by multiple users; I have some of them trying to run it like this ./script.pl --action ADD --action DEL
Did you make some test about this? It seems only the last value is stored in $action, which is logic somewhat
In this case $action would contain DEL

Maybe add a prompt in case of DEL action, so the user will confirm deletion

Last edited by keefaz; 03-10-2018 at 07:26 PM.
 
Old 03-11-2018, 03:24 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,363

Rep: Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418
Quote:
Originally Posted by fritz001 View Post
this is a script that is supposed to be run by multiple users; I have some of them trying to run it like this ./script.pl --action ADD --action DEL
that is still irrelevant. A variable cannot store two different values in the same time.
The variable is local to the process, other users will run a different process (using the same variable names), they cannot conflict with each other. But within one process every variable have a well defined value.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] there is one name 4 for command-line-parameters in shell script miillerje Linux - Newbie 3 10-21-2012 07:25 AM
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 10:08 PM
Perl Script works from command line, but not when evoked by Nagios gfem Linux - General 10 06-05-2008 03:59 PM
PERL script OK at command line, not in browser alvo Programming 4 12-19-2004 08:28 AM
Perl shell-out to script dunna work. Works on command line. Why? jlangelier Linux - Software 1 08-28-2004 02:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration