如何在 mac 中使用“getopt”命令让 bash 处理长参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12152077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:08:15  来源:igfitidea点击:

How can I make bash deal with long param using "getopt" command in mac?

macosbashgnu

提问by qiushuitian

I want to make my bash script deal with long parameters. I found getopt, but it isn't supported in OS X. Can anyone tell me why getoptwas implemented by BSD, but not GNU? I tried building getoptin GNU C lib, but it failed for my poor skills with Linux.

我想让我的 bash 脚本处理长参数。我找到了getopt,但它在 OS X 中不受支持。谁能告诉我为什么getopt是由 BSD 实现的,而不是由 GNU 实现的?我尝试getopt在 GNU C 库中构建,但由于我在 Linux 上的技能不佳而失败了。

Did anyone do this work?

有没有人做过这个工作?

回答by Denis Rouzaud

There is a brew bottle for getopt.

getopt有一个酿造瓶

Just run brew install gnu-getopt.

就跑brew install gnu-getopt

You can either specify the path for it like /usr/local/Cellar/gnu-getopt/1.1.6/bin/getopt

您可以为它指定路径,例如 /usr/local/Cellar/gnu-getopt/1.1.6/bin/getopt

Or use brew link --force gnu-getoptso it will be linked in /usr/local/bin/

或者使用brew link --force gnu-getopt它会被链接到/usr/local/bin/

Just be aware that forcing linking might be corrupting your system (as it replaces the system getopt by the gnu one).

请注意,强制链接可能会损坏您的系统(因为它用 gnu 替换了系统 getopt)。

See also other answer suggesting to define FLAGS_GETOPT_CMD.

另请参阅建议定义的其他答案FLAGS_GETOPT_CMD

回答by sMyles

I recommend using Homebrew to install gnu-getoptand then adding $FLAGS_GETOPT_CMDto your ~/.bash_profilefile to specify the cmd path for getopt, pointing at the homebrew location, like so:

我建议使用 Homebrew 安装gnu-getopt,然后添加$FLAGS_GETOPT_CMD到您的~/.bash_profile文件中以指定 getopt 的 cmd 路径,指向 homebrew 位置,如下所示:

brew install gnu-getopt

Then follow directions from brew to add to your local path:

然后按照 brew 的指示添加到您的本地路径:

sudo echo 'export PATH="/usr/local/opt/gnu-getopt/bin:$PATH"' >> ~/.bash_profile

Then you can add FLAGS_GETOPT_CMD:

然后你可以添加FLAGS_GETOPT_CMD

sudo echo 'export FLAGS_GETOPT_CMD="$(brew --prefix gnu-getopt)/bin/getopt"' >> ~/.bash_profile

Open a new terminal, or run . ~/.bash_profilein existing terminal to load changes

打开一个新终端,或. ~/.bash_profile在现有终端中运行以加载更改

Run echo $FLAGS_GETOPT_CMDto confirm it was actually set in your console

运行echo $FLAGS_GETOPT_CMD以确认它实际上已在您的控制台中设置

回答by ghoti

It's generally a better idea to use getoptsinstead, and stick with short options. You can see getopts in action in this StackOverflow Q&A. Short options are more standard throughout OSX command line tools, and consistency is a good thing.

使用它通常是一个更好的主意getopts,并坚持使用简短的选项。您可以在此 StackOverflow 问答中看到 getopts 的作用。短选项在 OSX 命令行工具中更为标准,一致性是一件好事。

Also, getopts is built in to bash, so it's definitely available in OSX, as well as every other platform that can run bash.

此外,getopts 内置于 bash,因此它绝对适用于 OSX 以及可以运行 bash 的所有其他平台。

Note that there is a getoptis also available in OSX. From Terminal, type man getoptto see its documentation. It doesn't support long options. This is a good reason not to use long options when you're writing tools to run on OSX.

请注意,getoptOSX 中也有一个。在终端中,键入man getopt以查看其文档。它不支持长选项。这是在编写在 OSX 上运行的工具时不使用长选项的一个很好的理由。

If you want to do this anyway, you can install getoptfrom macports. Alternately, if you want better portability, you can roll your own long argument handling.

如果您无论如何都想这样做,您可以getoptmacports安装。或者,如果您想要更好的可移植性,您可以推出自己的长参数处理。

Post some code, and we'll help debug it.

发布一些代码,我们将帮助调试它。