Perl: script command line parameters restrictions
Hello,
So here is the part where getopts is defined: Code:
my $ret = GetOptions ( 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 !! :) |
Add a test for mandatory variables, something like
Code:
my %mandatory = ( Code:
# which has the priority? |
Quote:
|
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 .... |
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 |
Quote:
Code:
if (!$file){ But what would be the best to prevent double usage of parameter: ./script.pl --action ADD --action DELETE ? |
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? |
Quote:
|
that is solved in post #2 I think. Another approach can be to use:
Code:
my $ret = GetOptions ( |
Quote:
if operation eq add && operation eq del => $operation=add |
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 |
Quote:
|
Quote:
|
Quote:
In this case $action would contain DEL Maybe add a prompt in case of DEL action, so the user will confirm deletion |
Quote:
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. |