0

I want to write a DOS script with multiple optional option flags. I have to parse these optional options.

An example:

get_resolution.bat /?
get_resolution.bat /h input.jpg
get_resoltion.bat /v input.jpg

Under *NIX this can be done with getopts.

The same examples under *nix:

get_resolution -?
get_resolution -h input.jpg
get_resolution -v input.jpg

In the *nix script txt file get_resolution one would then write:

while getopts ?hv flag  
do  
  case $flag in  
    ?) man get_resolution  
    h) get_horizontal_resolution $1
    v) get_vertical_resolution $1                       ;;
  esac
done

Does there exist a DOS equivalent for the *nix getopts?

1
  • 3
    You’re most certainly not talking about DOS. Please read the tag descriptions carefully and edit your question accordingly.
    – Daniel B
    Commented Nov 17, 2018 at 18:45

1 Answer 1

1

In batch scripts all arguments are stored in variable %*. Each argument can accessed by variable %1 for the first argument, and %2 for the second and so on. You can handle the arguments like this until %9.

More elegant argument handling requires the use of shift-command.

Take a look this Stackoverflow question: https://stackoverflow.com/questions/14286457/using-parameters-in-batch-files-at-windows-command-line

It has more information about handling arguments in batch scripts.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .