Bash函数如何检测它是否正在使用位置参数或命名参数? [英] How can a Bash function detect whether it is being used with positional or named arguments?

查看:136
本文介绍了Bash函数如何检测它是否正在使用位置参数或命名参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Bash函数,它能够尝试使用位置参数或命名参数,从而通过$ {1}$ {2}方法和命名参数可以使用 getopts 来访问。我有一些示例代码和示例用法如下所示,以说明我正在尝试做什么。问题是我对变量 $ {*} 所做的检查仅仅是字符 - 的grep >,这大大限制了更多参数的字符内容。在Bash中检查命名参数的方法是什么?



示例代码:

 功能1(){
#标志
argumentsFlag1 =
#N:命名的参数
#号码:位置参数
verboseFlag1 =0
#v:详细
silentFlag1 =0
#0:标准输出
#1:无标准输出
#选项和参数
#确定是否使用位置参数或命名参数。
#如果参数包含 - ,则假定命名参数,否则假定为
#。
if [$(echo$ {*}| grep - )];然后
argumentsFlag1 = N
,否则
argumentsFlag1 = P
科幻
#手柄命名参数
如果[ $ {argumentsFlag1}= =n];那么
OPTIND = 1;而getopts我:sv选项;在
中做
案例$ {options}
input1 =$ {OPTARG}
;;
v)
verboseFlag1 = 1
;;
s)
silentFlag1 = 0
;;
\?)
echoinvalid option - $ {OPTARG}
return
;;
:)
echo选项 - $ {OPTARG}需要参数
return
;;
esac
完成
#句柄位置参数
elif [$ {argumentsFlag1}==p];那么
input1 =$ {1}
fi
#默认值
if [-z$ {verboseFlag1}];那么
verboseFlag1 = 0
fi
#-------------------------------- ----------------------------------------------
if [$ {argumentsFlag1}==n];那么
echo假定的命名参数
elif [$ {argumentsFlag1}==p];那么
echo假定的位置参数
fi
echoinput:$ {input1}
}

示例用法:

  $ function1 zappo 
位置参数假设
输入:zappo

$ function1 -i zappo
假定的命名参数
输入:zappo

编辑:请注意,我不想同时使用位置和命名参数 。我试图让函数具有一种状态,在这种状态下,它将参数解释为位置或仅用于解释 getopts 作为位置参数和命名参数的混合。有一些不使用 getopts 的情况。想象下面的想法......

我有一个从一种数据类型到另一种数据类型的函数。此功能有两种模式:

快速模式



此模式可以以下列方式使用:

 函数fileName1 fileName2 

它使用内部假设和测量自主地将一个文件转换为另一个文件。

高级模式





 函数-i fileName1 -o fileName2 -m  - r 100 -v 

可以在此模式下使用位置参数,但它们必须放在 getopts的位置的选项和参数。

解决方案

使用 getopts 来处理任何命名参数,并将它们从堆栈中移出,如此操作。如果之后有任何遗留的,他们是你的立场论点。


I'm writing a Bash function that is to be capable of attempting to use either positional or named arguments, whereby positional arguments are accessed in the usual "${1}", "${2}" way and named arguments are accessed using getopts. I have some example code and example usage shown below in order to illustrate what I'm trying to do. The problem is that the check I'm doing on the variable ${*} is just a grep for the character -, which limits greatly the character content of further arguments. What would be a more intelligent or robust way of checking for named arguments in Bash?

example code:

function1(){
    # flags
        argumentsFlag1=""
            # n: named arguments
            # p: positional arguments
        verboseFlag1="0"
            # v: verbose
        silentFlag1="0"
            # 0: standard output
            # 1: no standard output
    # options and arguments
        # Determine if positional or named arguments are used.
            # If the arguments contain "-", then named arguments are assumed,
            # otherwise positional arguments are assumed.
                if [ "$(echo "${*}" | grep "-")" ]; then
                    argumentsFlag1="n"
                else
                   argumentsFlag1="p"
                fi
        # handle named arguments
            if [ "${argumentsFlag1}" == "n" ]; then
                OPTIND=1; while getopts "i:sv" options; do
                    case "${options}" in
                        i)
                            input1="${OPTARG}"
                            ;;
                        v)
                            verboseFlag1=1
                            ;;
                        s)
                            silentFlag1=0
                            ;;
                        \?)
                            echo "invalid option -"${OPTARG}""
                            return
                            ;;
                        :)
                            echo "option -"${OPTARG}" requires an argument"
                            return
                            ;;
                    esac
                done
        # handle positional arguments
            elif [ "${argumentsFlag1}" == "p" ]; then
                input1="${1}"
        fi
    # default values
        if [ -z "${verboseFlag1}" ]; then
            verboseFlag1=0
        fi
# ------------------------------------------------------------------------------
    if  [ "${argumentsFlag1}" == "n" ]; then
        echo "named arguments assumed"
    elif [ "${argumentsFlag1}" == "p" ]; then
        echo "positional arguments assumed"
    fi
    echo "input: "${input1}""
}

example usage:

$ function1 zappo
positional arguments assumed
input: zappo

$ function1 -i zappo
named arguments assumed
input: zappo

EDIT: Please note that I am not trying to use positional and named arguments at the same time. I am trying to get the function to have a state in which it is interpreting the arguments solely as positional or solely for interpretation by getopts as a mix of positional and named arguments. There are to be instances in which getopts is not used. Imagine the following idea...

I have a function that is used from one data type to another. This function has two modes:

quick mode

This mode can be used in a manner such as the following:

function fileName1 fileName2

It converts one file to another using internal assumptions and measurements made autonomously.

advanced mode

This mode can be used in a manner such as the following:

function -i fileName1 -o fileName2 -m -r 100 -v

There can be positional arguments used in this mode, but they must be placed after the getopts positional options and arguments.

解决方案

Use getopts to process any named arguments, shifting them off the stack as you do so. If any remain afterward, they are your positional arguments.

这篇关于Bash函数如何检测它是否正在使用位置参数或命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆