LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   mandatory and optional arguments (http://www.linuxquestions.org/questions/programming-9/mandatory-and-optional-arguments-885432/)

nano2 06-09-2011 10:15 AM

mandatory and optional arguments
 
Hi ,
I am using getopts to take parse optional arguments but i also pass in mandatory arguments first
[code]
#!/bin/bash


print_usage()
{
perror "$@"
}
arg1="$1"
arg2="$2"
shift 2
while getopts f:d: opt
do
case $opt in
f) file="${OPTARG}";;
d) dir="${OPTARG}";;
?) print_usage $0
exit 1;;
esac
done
echo "Argument 1 = $arg1"
echo "Argument 2 = $arg2"
echo "File=$file"
echo "Dir=$dir"

[code]

./script arg1 arg2 -f file -d dir this works fine

But when i pass in the wrong number of mandatory arguments and pass in the optional argument the command line gets all messed up like so

./script arg1 -f BLAH -d DIR
Argument 1 = arg1
Argument 2 = -f
File=
Dir=

Any ideas how to stop this ???

Reuti 06-09-2011 11:01 AM

Do it the other way round which is also more common: first the optional arguments, and when they run out the mandatory ones follow. After the getopt you can use:
Code:

shift $((OPTIND-1))
to have $1 set to the first mandatory argument. You can also check $# then for the number of mandatory arguments.

nano2 06-10-2011 02:35 AM

I would prefer to have the mandatory first and then followed by the options . IF so how can i get rid of the options getting missed up as mandatory ?

Reuti 06-10-2011 03:34 AM

Then you will have to read your mandatory arguments in a loop one by one (and shift only once after you handled the argument). If it starts with a "-" (or there is no additonal one), exit the loop and call the getopts for the remaining ones.
Code:

INDEX=0
while [ -n "$1" -a "${1:0:1}" != "-" ]; do
    ARGUMENT[$((++INDEX))]="$1"
    shift
done
if [ "$INDEX" -ne 2 ]; then
    echo "Error"
    exit 1
fi
echo "${ARGUMENT[1]}"
echo "${ARGUMENT[2]}"


onebuck 06-10-2011 08:10 AM

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

konsolebox 06-10-2011 08:39 AM

Quote:

Originally Posted by nano2 (Post 4381048)
Hi ,
I am using getopts to take parse optional arguments but i also pass in mandatory arguments first
Code:

#!/bin/bash


print_usage()
{
    perror "$@"
}
arg1="$1"
arg2="$2"
shift 2
while getopts f:d: opt
do
  case $opt in
              f) file="${OPTARG}";;
              d) dir="${OPTARG}";;
              ?) print_usage $0
            exit 1;;
  esac
done
echo "Argument 1 = $arg1"
echo "Argument 2 = $arg2"
echo "File=$file"
echo "Dir=$dir"

./script arg1 arg2 -f file -d dir this works fine

But when i pass in the wrong number of mandatory arguments and pass in the optional argument the command line gets all messed up like so

./script arg1 -f BLAH -d DIR
Argument 1 = arg1
Argument 2 = -f
File=
Dir=

Any ideas how to stop this ???

I do not use getopts much but perhaps you might get an idea from this example:
Code:

#!/bin/bash

ARGS=()

while [[ $# -gt 0 ]]; do
        case "$1" in
        -f)
                FILE=$2
                shift
                ;;
        -d)
                DIR=$2
                shift
                ;;
        *)
                ARGS[${#ARGS[@]}]=$1
                ;;
        esac
        shift
done

echo "Argument 1: ${ARGS[0]}"
echo "Argument 2: ${ARGS[1]}"
echo "Extra Arguments:" "${ARGS[@]:2}"

echo "File: $FILE"
echo "Directory: $DIR"


Reuti 06-10-2011 11:22 AM

@konsolebox: Yes, you can parse the (optional) argumemnts on your own, this works too. One advantage of using getopts is, that you can just concatenate several options like ls -lhrt instead of ls -l -h -r -t.

konsolebox 06-10-2011 05:20 PM

Quote:

Originally Posted by Reuti (Post 4382015)
@konsolebox: Yes, you can parse the (optional) argumemnts on your own, this works too. One advantage of using getopts is, that you can just concatenate several options like ls -lhrt instead of ls -l -h -r -t.

yeah i know :)
i had used getopts .. 6 years ago but there was a reason why i stopped using it.. i already forgot.


All times are GMT -5. The time now is 10:34 PM.