英文:
Use a space in a flag value in a Golang directive
问题
在使用指令时,如何在标志值中包含空格?
背景
flag
包在处理标志时,根据 Go 的调用方式而有所不同。在命令行中,对标志的 值 进行引用是有效的,但在指令(例如 go generate
)中则不起作用。
换句话说,从命令行调用时,Flag1
将接收到正确的值(即 4 个单词,3 个空格)。
有效的方式
% go run -tags generate main.go -Flag1="flag value with spaces"
无效的方式
然而,在指令文件中以相同的方式调用命令,例如使用 go generate ./...
,Flag1
将无法接收到正确的值。在指令文件(例如 generate.go
)中,添加以下行:
//go:generate go run -tags generate main.go -Flag1="flag value with spaces" -Flag2=next
Flag1
的值没有被正确设置,而 Flag2
则根本没有被设置。
英文:
How do you get spaces in flag values when using directives?
Context
The flag
package receives and/or handles flags with spaces differently depending on how Go is called. Quoting the flag value works from the command-line but not with a directive, such as go generate
, for example.
In other words, called from the command line, Flag1
will receive the correct value (i.e., 4 words, 3 spaces).
Works
% go run -tags generate main.go -Flag1="flag value with spaces"
Doesn't Work
However, calling the exact same command in a directive file, e.g., with go generate ./...
, Flag1
will not receive the correct value. Take a directive file (e.g., generate.go
) with this line in it:
//go:generate go run -tags generate main.go -Flag1="flag value with spaces" -Flag2=next
Flag1
is not set correctly and Flag2
is not set at all.
答案1
得分: 1
go run
命令的参数由你的shell解析。go:generate
指令进行自己的解析,而这种解析与shell的解析方式不同。
相关的go:generate文档如下所示:
指令的参数是通过空格分隔的标记或双引号字符串,当生成器运行时,它们作为单独的参数传递给生成器。
引号字符串使用Go语法,并在执行之前进行评估;引号字符串在生成器中作为单个参数出现。
每个参数由空格分隔。一个参数可以是一个“标记”或一个双引号字符串。文档没有描述什么是标记,但我们可以假设它指的是一系列非空格字符。
引号字符串会被评估。这意味着引号会被移除,转义序列会被取消转义等。实现使用strconv.Unquote来评估字符串。
根据上述内容,使用以下方式:
//go:generate go run -tags generate main.go "-Flag1=flag value with spaces"
这里的重要更改是将"
移到一个由空格跟随的位置。标记内部的引号会被忽略。
英文:
The arguments to go run
are parsed by your shell. The go:generate directive does its own parsing and that parsing is different from shells.
The relevant go:generate documentation is:
> The arguments to the directive are space-separated tokens or double-quoted strings passed to the generator as individual arguments when it is run.
> Quoted strings use Go syntax and are evaluated before execution; a quoted string appears as a single argument to the generator.
Each argument is separated by spaces. An argument is either a "token" or a double-quoted string. The documentation does not describe a token, but we can assume it means a sequence of non-space characters.
Quoted strings are evaluated. That means that quotes are removed, escape sequences are unescaped, etc. The implementation evaluates the string using strconv.Unquote.
Based on the above, use:
//go:generate go run -tags generate main.go "-Flag1=flag value with spaces"
The important change here is the "
is moved forward to a position where it's proceeded by a space. Quotes inside of a token are ignored.
答案2
得分: 0
从一个旧的、相关但不完全相同的问题推断,将整个标志键、破折号和值都用引号括起来。
//go:generate go run -tags generate main.go "-Flag1=flag value with spaces"
这种语法的优点是可以在命令行或指令文件中使用。
目前的文档中没有提供明确的答案(据我所知):
> 引号括起来的字符串使用Go语法,在执行之前进行评估;引号括起来的字符串在生成器中作为单个参数出现。
英文:
Extrapolating from an old, related, but not the same question, enclose the entire flag key, dash, and value in quotes.
//go:generate go run -tags generate main.go "-Flag1=flag value with spaces"
This syntax has the advantage of working on the command line or in directive files.
None of the current documentation (that I could find) provides a clear answer:
- The
flag
documentation does not mention flag values with spaces. - The directive documentation provides this cryptic hint that leads you to the correct solution (though a bit more clarity would be helpful).
> Quoted strings use Go syntax and are evaluated before execution; a quoted string appears as a single argument to the generator.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论