Shifting positional arguments after call to getopts

huangapple go评论75阅读模式
英文:

Shifting positional arguments after call to getopts

问题

Getopts使用OPTIND来存储位置参数的编号。接下来的做法是将位置参数进行如下移动:

shift "$(( OPTIND - 1 ))"

为什么要这样做?

英文:

Getopts uses OPTIND to store the positional argument number. It is then a practice to shift the positional arguments in the following way.

  shift "$(( OPTIND - 1 ))"

For what purpose does one do that ?

答案1

得分: 1

使用shift "$(( OPTIND - 1 ))"来移动位置参数的目的是为了移除已经由getopts处理过的所有选项及其相应的参数。这确保了剩余的参数是非选项参数,可以分别处理。OPTIND包含下一个由getopts处理的参数的索引,因此从中减去1得到已处理的选项数量,即要移动的参数数量。

英文:

The purpose of shifting the positional arguments using shift "$(( OPTIND - 1 ))" after using getopts is to remove all the options and their corresponding arguments that have already been processed by getopts. This ensures that the remaining arguments are the non-option arguments that can be processed separately. OPTIND contains the index of the next argument to be processed by getopts, so subtracting 1 from it gives the number of options processed, which is the number of arguments to shift.

huangapple
  • 本文由 发表于 2023年5月7日 20:45:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定