用于从[string]解析输入参数-值对的本机机制 [英] Native mechanism for parsing input parameter-value pairs from [string]
问题描述
是否存在一个本机的内置结构,该结构使用与cmdlet参数相同的规则自动将控制台输入解析为变量?
Is there a native, built-in construct that automatically parses console inputs into variables using the same rules as cmdlet parameters?
例如,许多cmdlet接受-parameter1 unspaced-value1 -parameter2:"spaced value2"
形式的参数-值对.
For example, many cmdlets accept parameter-value pairs in the form of -parameter1 unspaced-value1 -parameter2:"spaced value2"
.
我想使用Read-Host
读取字符串,然后将字符串解析为参数-值对,然后将它们存储到$variables
中,类似于int main(argv, argc)
,但可能以关联数组或类似形式出现. /p>
I want to read a string using maybe Read-Host
, and then parse the string into parameter-value pairs and store them into $variables
, something like int main(argv, argc)
, but maybe in an associative array or something similar.
推荐答案
不确定我是否完全了解您想要的内容,但您肯定是在描述可以简单定义为
Not sure if i understand exactly what you want but you are definitely describing a hashtable which can simply be defined as
@{Name="Value";AnotherName="AnotherValue"}
或更可读
@{
Name="Value"
AnotherName="AnotherValue"
}
两者都将输出以下内容,这些内容可以分配给一个变量,该变量可用于 splatting (稍后会详细介绍)
Both would output the following which could be assigned to a variable which could be used for splatting (more on that later)
Name Value
---- -----
Name Value
AnotherName AnotherValue
好的,那很好,但是您正在寻找一种将Read-Host
一起使用的方式的方法吗?这可能是 ConvertFrom-StringData
保存一天的地方.以最简单的形式:
Ok, that's nice but you were looking for a way to use this adhoc with Read-Host
maybe? This could be where ConvertFrom-StringData
saves the day. In it's simplest form :
PS C:\Users\Cameron> $data = "Data=Awesome"
PS C:\Users\Cameron> $data | ConvertFrom-StringData
Name Value
---- -----
Data Awesome
好的...很好.但是Read-Host
呢?让我们现在尝试吧!
Ok... that's great.. but what about Read-Host
. Let's try that now!
PS C:\Users\Cameron> Read-Host | ConvertFrom-StringData
Something=Blah
Name Value
---- -----
Something Blah
如果您想做多个键/值对,这会变得很复杂,因为Read-Host
似乎不喜欢在提示中插入新行.为了作弊,我使用-Split
和-Join
.拆分会分解成对,然后连接最终会创建一个以换行符分隔的字符串,ConvertFrom-StringData
会更好地发挥作用. 旁注:其中有一个帖子,其中包含用于创建从读取主机输入的多行输入但这比较简单.
This can get complicated if you want to do more than one key/value pair since Read-Host
doesn't seem to like inserting new lines in the prompt. To cheat I use -Split
and -Join
. Split break up the pairs and join ends up creating a newline delimited string which ConvertFrom-StringData
plays with better. Side Note There is a post that has a snippet for creating multiline input from Read-Host but this is simpler.
PS C:\Users\Cameron> ((Read-Host) -Split ";") -Join "`r`n" | ConvertFrom-StringData
Key1=Value1;Key2=Value2;Key3=Value3 <---- That is what I typed in as a response to Read-Host
Name Value
---- -----
Key1 Value1
Key2 Value2
Key3 Value3
如果您仍在阅读,请使用splatting并给出使用此逻辑的实际原因.
If you are still reading lets use splatting and give an actual reason to use this logic.
PS C:\Users\Cameron> $results = ((Read-Host) -Split ";") -Join "`r`n" | ConvertFrom-StringData
Get-ChildItem @results
Filter=*.txt;Path=C:\\temp <---- That is what I typed in as a response to `Read-Host`
LastWriteTime : 10/22/2014 11:43:38 PM
Length : 653018
Name : out1.txt
....output truncated....
注意:您应该注意到在第二个反斜杠Path=C:\\temp
中键入的路径.原因来自 ConvertFrom-StringData
Note: You should notice in the path I typed in the second backslash Path=C:\\temp
. The reason comes from the TechNet article for ConvertFrom-StringData
ConvertFrom-StringData
支持的转义字符序列为 常规机器翻译工具允许的.即,cmdlet 可以将反斜杠(\
)解释为字符串数据中的转义字符 通过使用Regex.Unescape
方法而不是Windows PowerShell 反引号字符(`
),通常会在 一个脚本.在here字符串中,反引号字符不 工作.您还可以通过以下方式在结果中保留字面反斜线: 以前面的反斜杠转义,如下所示:\\
.未逃脱 反斜杠字符,例如文件中常用的字符 路径,可能会在结果中显示为非法的转义序列.
ConvertFrom-StringData
supports escape character sequences that are allowed by conventional machine translation tools. That is, the cmdlet can interpret backslashes (\
) as escape characters in the string data by using theRegex.Unescape
Method, instead of the Windows PowerShell backtick character (`
) that would normally signal the end of a line in a script. Inside the here-string, the backtick character does not work. You can also preserve a literal backslash in your results by escaping it with a preceding backslash, like this:\\
. Unescaped backslash characters, such as those that are commonly used in file paths, can render as illegal escape sequences in your results.
小更新
虽然((Read-Host) -Split ";") -Join "`r`n" | ConvertFrom-StringData
可以正常工作,但是我确信下面的内容要简单得多.
While the ((Read-Host) -Split ";") -Join "`r`n" | ConvertFrom-StringData
works I'm sure that the following is much simpler.
(Read-Host).Replace(";","`r`n") | ConvertFrom-StringData
这篇关于用于从[string]解析输入参数-值对的本机机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!