在程序参数中保留换行符和制表符 - Powershell

发布于 2025-01-17 20:52:49 字数 851 浏览 0 评论 0 原文

我正在使用PowerShell来调用打印python配置文件的程序:

[distutils]
index-servers =
    some-pypi

[some-pypi]
repository: https://path/to/pypi

我要做的是将整个输出作为字符串作为字符串传递给另一个命令。 不断发生的事情是,每当我将其传递给消耗它的程序时,PowerShell最终都会删除所有Newline和Tab特殊字符。 这会创建buggy输出。

这是一些相关命令及其输出。

> command to print config

它完好无损地打印出所有新线和标签字符。

> $settings = command to print config
> echo $settings

当回声时,它会完好无损地打印所有内容和标签字符。

> consuming-program $settings

由于表达式解决将所有输出作为多个参数提供给程序而不是单个参数,因此无法解析。

> consuming-program "$settings"

这删除了所有Newline和Tab字符。

> consuming-program "$(command to print config)"

还取出所有新线字符。

我想念什么?我如何保留原始输出并将其作为一个特殊字符完好无损的单一参数将其输入我的程序中?

I'm using Powershell to make a call to a program that prints a Python config file that's structured like this:

[distutils]
index-servers =
    some-pypi

[some-pypi]
repository: https://path/to/pypi

What I'm trying to do is pass the entirety of that output as a string to another command. What keeps on happening is that any time I go to pass it to the program that consumes it, Powershell ends up taking out all the newline and tab special characters. This creates buggy output.

Here are some relevant commands and their outputs.

> command to print config

Which prints everything with the newline and tab characters intact.

> $settings = command to print config
> echo $settings

Which, when echoed, prints everything with the newline and tab characters intact.

> consuming-program $settings

This fails to parse as the expression resolves providing all that output as multiple arguments to the program instead of a single argument.

> consuming-program "$settings"

This takes out all of the newline and tab characters.

> consuming-program "$(command to print config)"

Also takes out all the newline characters.

What am I missing? How can I retain the original output and feed it into my program as a single argument with the special characters intact??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2025-01-24 20:52:49

之所以发生这种情况,是因为命令的输出被捕获为字符串的 array 而不是单个字符串。

例如,如果我们将此命令用作基准:

C:\> dotnet

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.

然后在PowerShell中捕获输出,我们可以看到它是一系列字符串 - 每行输出一条 - 而不是单个多行字符串:

PS> $x = dotnet
PS> $x.GetType().FullName
System.Object[]

当您输出 $ x时, 数组中显示每个项目

PS> $x

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.

到控制台上它在单独行上的 - 它通过以单个空间作为分离器连接到数组中的项目来序列化:

 Usage: dotnet [options] Usage: dotnet [path-to-application]  Options:   -h|--help         Display help.   --info            Display .NET information.   --list-sdks       Display the installed SDKs.   --list-runtimes   Display the installed runtimes.  path-to-application:   The path to an application .dll file to execute.

这基本上是Powershell在某些情况下如何序列阵列。例如,比较这两个命令的输出:

PS> @( "aaa", "bbb", "ccc" )
aaa
bbb
ccc

PS> write-host @( "aaa", "bbb", "ccc" )
aaa bbb ccc

@iron在评论中建议的一个修复程序是在您的命令中使用该字符串之前序列化字符串:

PS> $y = $x | Out-String
PS> $y

然后您可以使用 $ y 作为您的命令,保留了原始格式。

This is happening because the output from the command is captured as an array of strings rather than a single string.

For example if we use this command as our baseline:

C:\> dotnet

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.

and then capture the output in PowerShell we can see it's an array of strings - one per line of output - rather than a single multi-line string:

PS> $x = dotnet
PS> $x.GetType().FullName
System.Object[]

When you output $x to the console it shows each item in the array on a separate line so it appears visually to be the same as the console output:

PS> $x

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.

but when you use it as command argument something else is happening - it's being serialised to a single string by joining the items in the array with a single space as a separator:

 Usage: dotnet [options] Usage: dotnet [path-to-application]  Options:   -h|--help         Display help.   --info            Display .NET information.   --list-sdks       Display the installed SDKs.   --list-runtimes   Display the installed runtimes.  path-to-application:   The path to an application .dll file to execute.

This is basically just how PowerShell serialises arrays in some scenarios. For example, compare the output from these two commands:

PS> @( "aaa", "bbb", "ccc" )
aaa
bbb
ccc

PS> write-host @( "aaa", "bbb", "ccc" )
aaa bbb ccc

One fix, as suggested by @iRon in the comments, is to serialise the string yourself before using it in your command:

PS> $y = $x | Out-String
PS> $y

And then you can use $y as your command argument with the original formatting preserved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文