User specified errors with getopts colon feature

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

User specified errors with getopts colon feature

问题

I want the ability to specify whether to use Normal Error Reporting or Silent Error Reporting provided with getopts.

然后我可以像这样调用我的bash函数

mari -s -n VAL -z VAL

-s 将设置静默错误报告(即设置 opstring=":n:z:"),否则将使用普通错误报告(opstring="n:z:")。

目前我正在函数内部硬编码 opstring

mari() 
{

 impl=""
 if [[ "$impl"  == "SILENT" ]]; then
   opstring=":n:z:"  # Short options string
 else
   opstring="n:z:"
 fi

 while getopts "$opstring" opname; do
  case ${opname} in
    ("n")
      if [ -n "$OPTARG" ]; then
        echo "The string is not empty"
      else
        echo "The string is empty"
      fi
      ;;
    ("z")
      if [ -z "$OPTARG" ]; then
        echo "The string is empty"
      else
        echo "The string is not empty"
      fi
      ;;
    (?)
      ## Invalid Option Found, OPNAME set to '?'
      echo "Invalid option: -$OPTARG" 1>&2
      exit 1
      ;;
    (:)
      ## Required option argument not found, OPNAME set to ':'
      echo "Option -$OPTARG requires an argument" 1>&2
      exit 1
      ;;
  esac
 done
}
英文:

I want the ability to specify whether to use Normal Error Reporting or Silent Error Reporting provided with getopts.

Then I would be able to call my bash function like this

mari -s -n VAL -z VAL

The -s would set Silent Error Reporting (i.e. set opstring=":n:z:"), otherwise Normal Error Reporting (opstring="n:z:") will be used.

Currently I am hardwiring opstring inside the function.

mari() 
{

 impl=""
 if [[ "$impl"  == "SILENT" ]]; then
   opstring=":n:z:"  # Short options string
 else
   opstring="n:z:"
 fi

 while getopts "$opstring" opname; do
  case ${opname} in
    ("n")
      if [ -n "$OPTARG" ]; then
        echo "The string is not empty"
      else
        echo "The string is empty"
      fi
      ;;
    ("z")
      if [ -z "$OPTARG" ]; then
        echo "The string is empty"
      else
        echo "The string is not empty"
      fi
      ;;
    (?)
      ## Invalid Option Found, OPNAME set to '?'
      echo "Invalid option: -$OPTARG" 1>&2
      exit 1
      ;;
    (:)
      ## Required option argument not found, OPNAME set to ':'
      echo "Option -$OPTARG requires an argument" 1>&2
      exit 1
      ;;
  esac
 done
}

答案1

得分: 1

修改opstring一旦遇到s

?是通配符,它匹配任何内容。你需要引用它。

mari() {
    local opstring
	opstring="sn:z:"
	while getopts "$opstring" opname; do
		case ${opname} in
		"s")
			opstring=":${opstring##:}"
			;;
		"n")
			if [ -n "$OPTARG" ]; then
				echo "字符串不为空"
			else
				echo "字符串为空"
			fi
			;;
		"z")
			if [ -z "$OPTARG" ]; then
				echo "字符串为空"
			else
				echo "字符串不为空"
			fi
			;;
		'?')
			## 无效选项,将OPNAME设置为'?'。
			echo "无效选项: -$OPTARG" 1>&2
			exit 1
			;;
		':')
			## 所需的选项参数未找到,将OPNAME设置为':'。
			echo "选项 -$OPTARG 需要一个参数" 1>&2
			exit 1
			;;
		esac
	done
}

echo "没有无效选项:"
mari -s -bla
echo "无效选项:"
mari -bla
英文:

Modify the opstring once s is encountered.

? is a glob, it matches anything. You have to quote it.

mari() {
    local opstring
	opstring="sn:z:"
	while getopts "$opstring" opname; do
		case ${opname} in
		"s")
			opstring=":${opstring##:}"
			;;
		"n")
			if [ -n "$OPTARG" ]; then
				echo "The string is not empty"
			else
				echo "The string is empty"
			fi
			;;
		"z")
			if [ -z "$OPTARG" ]; then
				echo "The string is empty"
			else
				echo "The string is not empty"
			fi
			;;
		'?')
			## Invalid Option Found, OPNAME set to '?'
			echo "Invalid option: -$OPTARG" 1>&2
			exit 1
			;;
		':')
			## Required option argument not found, OPNAME set to ':'
			echo "Option -$OPTARG requires an argument" 1>&2
			exit 1
			;;
		esac
	done
}

echo "no invalid option:"
mari -s -bla
echo "invalid option:"
mari -bla

huangapple
  • 本文由 发表于 2023年5月6日 21:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189223.html
匿名

发表评论

匿名网友

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

确定