在解析阶段包含可选数组。

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

Incorporating optional arrays in case parsing stage

问题

以下是翻译好的内容:

以下的bash函数用于将项目填充到数组中。

语法是

    multary OPTIONS ary1 ary2 ary3 "item1" "item2" marker "item3" marker "item4"

数组 `ary1`、`ary2` 和 `ary3` 是可选的。我想找到一种简化参数读取部分的方法,以便在处理位置参数的 `CASE` 部分中合并 `PART2` 中的数组解析代码。


multary ()
{
  ## 传递带有分隔符的多个数组。

  ## 分隔符作为分隔符
  local dna="***" dnb="***"

  while (( $# > 0 )); do
   parg="$1"
   case "$parg" in
     ("-m1") dna="$2" ; shift 2 ;;
     ("-m2") dnb="$2" ; shift 2 ;;
     ("--") shift 1 ; break ;;
     (*) break ;;
   esac
  done

  ##====================================================
  ## PART2

  if [[ "$(declare -p $1)" =~ "declare -a" ]]; then
    typeset -n _ary1="$1"
    _ary1=() ; shift 1
    echo "_ary1 是一个数组"
  fi

  if [[ "$(declare -p $1)" =~ "declare -a" ]]; then
    typeset -n _ary2="$1"
    _ary2=() ; shift 1
    echo "_ary2 是一个数组"
  fi

  if [[ "$(declare -p $1)" =~ "declare -a" ]]; then
    typeset -n _ary3="$1"
    _ary3=() ; shift 1
    echo "_ary3 是一个数组"
  fi
}
英文:

The following bash function fills items into arrays.

The syntax is

multary OPTIONS ary1 ary2 ary3 "item1" "item2" marker "item3" marker "item4"

The arrays ary1, ary2, ary3 are optional. I want to find a way to simplify the argument reading part so that the array parsing code in PART2 is incorporated in the CASE part for processing positional arguments.

multary ()
 {
  ## Passing multiple arrays with marker between them.

  ## Marker as break
  local dna="***" dnb="***"

  while (( $# > 0 )); do
   parg="$1"
   case "$parg" in
     ("-m1") dna="$2" ; shift 2 ;;
     ("-m2") dnb="$2" ; shift 2 ;;
     ("--") shift 1 ; break ;;
     (*) break ;;
   esac
  done

  ##====================================================
  ## PART2

  if [[ "$(declare -p $1)" =~ "declare -a" ]]; then
    typeset -n _ary1="$1"
    _ary1=() ; shift 1
    echo "_ary1 is an array"
  fi

  if [[ "$(declare -p $1)" =~ "declare -a" ]]; then
    typeset -n _ary2="$1"
    _ary2=() ; shift 1
    echo "_ary2 is an array"
  fi

  if [[ "$(declare -p $1)" =~ "declare -a" ]]; then
    typeset -n _ary3="$1"
    _ary3=() ; shift 1
    echo "_ary3 is an array"
  fi
}

答案1

得分: 2

#!/usr/bin/env bash

multary(){
    while (( $# > 0 )); do
        case "$1" in
        ("-m") declare -n __ary="$2"; __ary=(); shift 2;;
        (*) __ary+=("$1"); shift;;
        esac
    done
}
multary -m ary1 "item1" "item2" -m ary2 "item3" -m ary3 "item4"
declare -p ary1
declare -p ary2
declare -p ary3
# Output :
# declare -a ary1=([0]="item1" [1]="item2")
# declare -a ary2=([0]="item3")
# declare -a ary3=([0]="item4")
英文:

You don't need markers :

#!/usr/bin/env bash

multary(){
    while (( $# > 0 )); do
        case "$1" in
        ("-m") declare -n __ary="$2"; __ary=(); shift 2;;
        (*) __ary+=("$1"); shift;;
        esac
    done
}
multary -m ary1 "item1" "item2" -m ary2 "item3" -m ary3 "item4"
declare -p ary1
declare -p ary2
declare -p ary3
# Output :
# declare -a ary1=([0]="item1" [1]="item2")
# declare -a ary2=([0]="item3")
# declare -a ary3=([0]="item4")

答案2

得分: 0

#!/bin/bash

multary(){
    source <(
        awk '
            /^-m/{ f=1; getline arr; print arr"=()"; next }
            f{ print arr"+=("$0")" }
       ' < <(printf "%q\n" "$@")
    )
}

multary -m ary1 "item1 itemX" "item2" -m ary2 "item3" -m ary3 "item4"
declare -p ary1
declare -p ary2
declare -p ary3
# Output:
declare -a ary1=([0]="item1 itemX" [1]="item2")
declare -a ary2=([0]="item3")
declare -a ary3=([0]="item4")
英文:
#!/bin/bash

multary(){
        source <(
            awk '
                /^-m/{ f=1; getline arr; print arr"=()"; next }
                f{ print arr"+=("$0")" }
           ' < <(printf "%q\n" "$@")
        )
}

multary -m ary1 "item1 itemX" "item2" -m ary2 "item3" -m ary3 "item4"
declare -p ary1
declare -p ary2
declare -p ary3

# Output:
declare -a ary1=([0]="item1 itemX" [1]="item2")
declare -a ary2=([0]="item3")
declare -a ary3=([0]="item4")

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

发表评论

匿名网友

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

确定