LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: script command line parameters restrictions (https://www.linuxquestions.org/questions/programming-9/perl-script-command-line-parameters-restrictions-4175624207/)

fritz001 02-22-2018 02:19 AM

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 !! :)

keefaz 02-22-2018 06:45 AM

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;


fritz001 02-22-2018 03:51 PM

Quote:

Originally Posted by keefaz (Post 5822784)
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 !!

fritz001 03-06-2018 08:54 AM

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

keefaz 03-06-2018 11:36 AM

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

fritz001 03-07-2018 02:07 AM

Quote:

Originally Posted by keefaz (Post 5827981)
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 ?

keefaz 03-07-2018 08:17 AM

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?

fritz001 03-08-2018 02:30 AM

Quote:

Originally Posted by keefaz (Post 5828292)
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

pan64 03-08-2018 02:41 AM

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


fritz001 03-09-2018 09:32 AM

Quote:

Originally Posted by pan64 (Post 5828594)
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

keefaz 03-09-2018 09:38 AM

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

pan64 03-10-2018 02:06 AM

Quote:

Originally Posted by fritz001 (Post 5829035)
if operation eq add && operation eq del => $operation=add

operation cannot be eq add and eq del in the same time, that is nonsense.

fritz001 03-10-2018 05:44 PM

Quote:

Originally Posted by pan64 (Post 5829260)
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 :D :D

keefaz 03-10-2018 07:23 PM

Quote:

Originally Posted by fritz001 (Post 5829499)
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

pan64 03-11-2018 03:24 AM

Quote:

Originally Posted by fritz001 (Post 5829499)
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 :D :D

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.


All times are GMT -5. The time now is 12:20 AM.