问题标签 [getopts]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
242 浏览

bash - How can I share options parsing code within bash functions when using getopts?

How can I clean up this options parsing code in a Bash function? It is copy-pasted into a number of functions that need to have a simple "-h" flag for printing usage. I would rather have a generic routine with the logical equivalent of

For example:

The functions are to be "sourced" in one's dot-bashrc file.

Here is what the code looks like (note: some of the functions do take options via flags):

Ordinarily I would use something like node-commander or node-optimist and write the script in JavaScript (or perhaps Python) for scripting needs but I'm trying to make Bash work this time. But Bash is bashing me.

0 投票
2 回答
100 浏览

unix - 在 getops 中 SED 多个字符

我正在尝试创建一个可以使用命令的脚本,例如

它会删除test.txt中的任何字符abc或。d

这是我到目前为止的代码:

但这只会消除我文件中的“abcd”,而不是 a 或 b 或 c 或 d,而不是 acdb 等。如何让 SED 将 OPTARG 作为单独的字符读取?

0 投票
6 回答
81669 浏览

bash - 读取 $OPTARG 以获取可选标志?

我希望能够在我的脚本中接受强制标志和可选标志。这是我到目前为止所拥有的。

但是,以下输出:

...输出:

如您所见,可选标志不会像使用必需标志那样使用 $OPTARG 提取参数。有没有办法在不摆脱整洁的“:)”错误处理的情况下读取可选标志上的 $OPTARG?

========================================

编辑:我最终听从了下面吉尔伯特的建议。这是我所做的:

非常感谢大家。到目前为止,这非常有效。

0 投票
2 回答
4009 浏览

linux - 如何阻止 getopts 将空白或 null 或其他选项作为选项值

我(大概是每个人)不时遇到这个问题,但我自己找不到任何好的解决方法。当getopts寻找一个论点时,它实际上会采用下一个论点,即使它是一个选项。这就是我为阻止它所做的(代码片段):

但它仍然将空白/空值作为有效参数。所以,这有效:

但这些不是:

我相当期待Missing argument for -c错误。我在这里有什么遗漏吗?或者有人知道解决方法吗?任何输入将不胜感激。干杯!!


更新(主要基于devnull的回复):

为了完整起见,现在我有了这个小功能:

那么这可以像这样使用:

对于所有需要参数的选项。干杯!!

PS。由于某种原因,*[[:space:]]*在函数中使用时不起作用。

0 投票
3 回答
1915 浏览

bash - How to Use Bash getopts Command to Get All Arguments Together

I have couple of files needed to be parsed here, called parse.sh. I have to enable optional arguments for it with -l for line, -f for field. So to run the program would be ./parse.sh -l 5 -f 14 foo. Without -l or -f arguments, I want the program to default parse all lines and all fields. If -l is specified, I want it to parse just that line of foo, and further if -f is specified too, I want it to parse just that field. I see getopts usually work like this:

BUT this is not what I need, because I want -l and -f work together sometimes. I am thinking maybe I should do getopts to parse all options into array, then write code based on parsing that array? Any better choices?

This is my code:

0 投票
2 回答
832 浏览

bash - 函数中调用的 getopts 未拾取标志

我有以下从我的调用中调用的脚本.bash_profile

当我执行

该标志被完全忽略——我从未看到“覆盖所有本地更改!”的消息。我在某个地方错过了什么吗?

更新:确实有效,但仅在我一次运行脚本时。从第二次开始,该标志将被忽略。

0 投票
1 回答
147 浏览

linux - UNIX 基本 getopts 麻烦

我最近才开始使用 UNIX,但在尝试实现 getopts 时遇到了问题。

下面的函数找到一个文件,然后将其删除到回收站,尽管我正在尝试将 getopts 与 -i 一起使用,它会在移动后显示一条消息。语法工作正常,但是当我使用 getotps 命令实现 while 循环时,它不再工作。

谁能给我任何有用的建议,将不胜感激

0 投票
1 回答
1806 浏览

ksh - ksh getopts 未知选项错误

我正在使用 getopts 将选项传递给 ksh 脚本,但 getopts 无法识别这些选项。

这是我给 getopts 的使用字符串的选项部分

这是我的 getopts 块

这是使用选项运行脚本的输出

0 投票
2 回答
7017 浏览

bash - 在 bash 脚本中解析参数的最佳方法

所以我一直在阅读有关 getopts、getopt 等的信息,但我还没有找到解决问题的确切方法。

我的脚本使用的基本思想是:

如果通过了 -d,则不需要 TEXT。请注意,TEXT 通常是一段文本。

我的主要问题是,一旦 getopts 完成对标志的解析,我就无法知道 TEXT 参数的位置。我可以假设 TEXT 是最后一个参数,但是,如果用户搞砸并执行以下操作:

那么程序将不会意识到用法不正确。

我最接近的是使用 getopt 和 IFS 做

唯一的问题是 TEXT 可能是一段很长的文本,并且由于空格,此方法会拆分文本的每个单词。

我认为我最好的选择是使用正则表达式来确保正确形成用法,然后使用 getopts 提取参数,但如果有更简单的解决方案会很好

0 投票
1 回答
775 浏览

bash - 在 getopts 中更改 no args 默认情况 - bash

每当我使用 getopts 并且我没有为给定标志提供任何参数时,我都会收到以下消息:"option requires an argument -- d"

我想删除此消息并允许用户使用该read命令重新键入选项。

这是我在getopts中的情况: