将Bash选项字符串转换为Unix环境变量名称,使用VSCode片段转换

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

Converting Bash Option String to Unix Environment Variable Name using vscode snippet Transformation

问题

我正在为sheller扩展编写一个bash脚本片段,需要将用户输入的Bash选项字符串转换为有效的Unix环境变量名称。例如,用户可能输入类似"-my-option-name"或"--another_option="的选项字符串,我需要将其转换为有效的环境变量名称,例如"MY_OPTION_NAME"或"ANOTHER_OPTION"。

为了澄清我的要求并清楚地说明期望的转换,我在JSFiddle上创建了一个JavaScript示例。您可以在这里找到它。该示例展示了不同输入字符串及其转换后的预期输出的表格。

JavaScript示例中处理转换的函数如下:

function stripCapitalizeAndSnake(inputString) {
  const regex = /^-+(.*?)(?:=)?$/;
  const result = inputString.replace(regex, (_, selectedString) => {
    const transformedString = selectedString.replace(/-/g, '_');
    return transformedString.toUpperCase();
  });
  return result;
}

我正在寻求关于如何在Visual Studio Code片段中使用片段转换来实现此转换的指导,或者更好的情况下,代码示例。

非常感谢您的帮助,特别是如果您对vscode、代码片段和转换有经验或知识的话!

以下是一个简化版本的片段,我希望有人可以修改以获得期望的结果。

当前的片段如下:

{
    "OPTION TO ENVIRONMENT": {
        "prefix": "option to environment",
        "body": [
            "#!/usr/bin/env bash",
            "",
            "OPTION=\"${1:--quiet-mode}\"",
            "",
            "echo \"Option        :\${OPTION}\"",
            "echo \"Variable name :${1/^(\\-+)([^=]+)(=)?$/${2:/upcase}/}\""
        ],
        "description": "Convert bash option to a valid shell environment variable"
    }
}

当前的结果:

当触发片段时,如果将默认值保留不变,变量名是正确的,只是"-"没有被替换为"_",这是我的问题。
"QUIET-MODE"需要转换为"QUIET_MODE"。

#!/usr/bin/env bash

OPTION="--quiet-mode"

echo "Option        :${OPTION}"
echo "Variable name :QUIET-MODE"

我已经尝试了一段时间,所以我想问一下,您知道如何做到这一点吗?

附带问题:
您是否了解有关转换的更好文档,除了在这里提到的之外?

英文:

I am working on a bash script snippet for the sheller extension where I need to convert a Bash option string entered by the user into a valid Unix environment variable name. For example, the user may enter an option string like "-my-option-name" or "--another_option=", and I need to transform it into a valid environment variable name like "MY_OPTION_NAME" or "ANOTHER_OPTION".

To clarify my requirements and provide a clear understanding of the desired transformation, I have created a JavaScript example on JSFiddle. You can find it here. The example showcases a table of different input strings and their expected output after the transformation.

The javascript function handling the transformation used in the fiddle.
function stripCapitalizeAndSnake(inputString) {
  const regex = /^-+(.*?)(?:=)?$/;
  const result = inputString.replace(regex, (_, selectedString) => {
    const transformedString = selectedString.replace(/-/g, '_');
    return transformedString.toUpperCase();
  });
  return result;
}

I am seeking guidance or preferably code examples on how to implement this transformation using snippet transforms in Visual Studio Code snippets.

> Thank you in advance for your assistance, especially if you have experience or knowledge about the vscode, code snippets and transformations!

Here is a simplified version of a snippet, which I hope someone can modify for the expected result.

Current snippet

{
    "OPTION TO ENVIRONMENT": {
        "prefix": "option to environment",
        "body": [
            "#!/usr/bin/env bash",
            "",
            "OPTION=\"${1:--quiet-mode}\"",
            "",
            "echo \"Option        :\${OPTION}\"",
            "echo \"Variable name :${1/^(\\-+)([^=]+)(=)?$/${2:/upcase}/}\""
        ],
        "description": "Convert bash option to a valid shell environment variable"
    }
}

Current Result

When triggering the snippet, and leaving the default value as is the Variable name is correct except for the fact that "-" is not replaced with "_" which is my problem.
"QUIET-MODE" needs to be transformed to "QUIET_MODE".

#!/usr/bin/env bash

OPTION="--quiet-mode"

echo "Option        :${OPTION}"
echo "Variable name :QUIET-MODE"

I have been trying to do this for a while now, so I ask you, do you know how to do this?

Ps.
Do you know of a better documentation about transformations, other than is mentioned here?

答案1

得分: 1

使用扩展 Hypersnips

您可以将 JavaScript 添加到代码片段中


这可以通过 VSC 标准代码片段完成

  • 捕获可能的起始“-”字符,在结果中忽略
  • 捕获直到“-”或“=”之间的所有内容,将该组大写
  • 捕获可能的“-”,替换为“_”
  • 捕获可能的“=”,在结果中忽略

应用这些规则全局/重复

{
  "OPTION TO ENVIRONMENT": {
    "prefix": "option to environment",
    "body": [
        "#!/usr/bin/env bash",
        "",
        "OPTION=\"${1:--quiet-mode}\"",
        "",
        "echo \"Option        :${OPTION}\"",
        "echo \"Variable name :${1/(^-+)?([^-=]+)(-?)(=?)/${2:/upcase}${3:+_}/g}\""
    ],
    "description": "将 bash 选项转换为有效的 shell 环境变量"
}
英文:

use the extension Hypersnips

you can add JavaScript to the snippet


It can be done with VSC standard snippets

  • capture possible starting - characters, ignore in result
  • capture all till - or =, UPCASE that group
  • capture possible -, substitute with _
  • capture possible =, ignore in result

apply these rules global/repeatedly

{
  "OPTION TO ENVIRONMENT": {
    "prefix": "option to environment",
    "body": [
        "#!/usr/bin/env bash",
        "",
        "OPTION=\"${1:--quiet-mode}\"",
        "",
        "echo \"Option        :\${OPTION}\"",
        "echo \"Variable name :${1/(^-+)?([^-=]+)(-?)(=?)/${2:/upcase}${3:+_}/g}\""
    ],
    "description": "Convert bash option to a valid shell environment variable"
}

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

发表评论

匿名网友

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

确定