处理可选函数参数 [英] Handle optional function arguments

查看:66
本文介绍了处理可选函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图创建一个可重用的函数,该函数最多可以包含4个参数(尽管解决方案应该是任意的)。我想 zip 与AWS CloudFormation的参数一起使用,如果我不要将位置参数传递给 update_stack ,它不会包含在 CF_ARGS 中。

Trying to make a reusable function that can take up to 4 parameters (though the solution it should be arbitrary). I want to zip these with parameters for AWS CloudFormation such that if I don't pass a positional argument to update_stack it won't get included in CF_ARGS.

##
# Update the stack with the given template
# @param: stack_name
# @param: template_body
# @param: [tags]
# @param: [parameters]
##
update_stack() {
    # Handle optional func parameters
    CF_PARAMS=("--stack-name" "--template-body" "--tags" "--parameters")
    # Only include a param if we have a value supplied as an argument. (+1 to ignore script name)
    CF_ARGS=($(for i in "${!CF_PARAMS[@]}"; do j=$(($i+1)); if [ -n "${!j}" ]; then echo "${CF_PARAMS[$i]} ${!j}"; fi; done))

    # Should make the call as "aws cloudformation create-stack --stack-name $1 --template-body $2"
    # If $3 or $4 are not supplied then it should not add them to CFG_ARGS
    aws cloudformation create-stack "${CFG_ARGS[@]}"
}

我想我有 CF_ARGS 构造按预期工作,我只是不知道如何在bash函数调用中应用参数,而我的Google技能在如何找到解决方案方面失败了。请帮忙!

I think I've got the CF_ARGS construction working as expected I just don't know how to apply arguments in a bash function call and my google skills have failed on how to find a solution for this. Please help!

谢谢,
Alex

Thanks, Alex

PS。我从 awscli 中得到的错误是(也许是双引号?):

PS. The error I've getting from awscli is (maybe the double quotes?):

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument --stack-name is required

编辑(1) :

感谢@Barmar提供您的第一个解决方案。这使我陷入了这个错误:

Thanks @Barmar for your first solution. That got me to this error:

Unknown options: Description:, Handles, Authentication, &, Access, permissions, Resources:, ##, #, Policies, ##, PolicyDevelopers:, Type:, AWS::IAM::Policy, Properties:, PolicyName:, Developers-cf, #, @todo:, Remove, suffix, PolicyDocument:, Version:, "2012-10-17", Statement:, -, Effect:, Allow, NotAction:, -, iam:*, -, sts:AssumeRole, ...

-template-body参数是多行字符串(确切地说是YAML文件),是否每个参数都用双引号引起来? :/

The "--template-body" argument is a multi-line string (YAML file to be exact), is there anyway to double-quote each argument? :/

推荐答案

您要将 CF_ARGS 设置为单个字符串,不是数组。您需要在其周围包装另一组括号才能得到一个数组。

You're setting CF_ARGS to a single string, not an array. You need to wrap another set of parentheses around it to get an array.

CF_ARGS=($(for i in "${!CF_PARAMS[@]}"; do j=$(($i+1)); if [ -n "${!j}" ]; then echo "${CF_PARAMS[$i]} ${!j}"; fi; done))

并使其处理<$ c的每一行输出$ c> for 循环作为单个参数,可以将 IFS 设置为仅包含换行符:

And to make it treat each line of output from the for loop as a single argument, you can set IFS to only contain newline:

IFS=$'\n'
CF_ARGS=($(for i in "${!CF_PARAMS[@]}"; do j=$(($i+1)); if [ -n "${!j}" ]; then echo "${CF_PARAMS[$i]} ${!j}"; fi; done))

这篇关于处理可选函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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