forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertFrom-StringData.cs
More file actions
95 lines (79 loc) · 2.67 KB
/
Copy pathConvertFrom-StringData.cs
File metadata and controls
95 lines (79 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Management.Automation;
using System.Text.RegularExpressions;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Class comment.
/// </summary>
[Cmdlet(VerbsData.ConvertFrom, "StringData", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096602", RemotingCapability = RemotingCapability.None)]
[OutputType(typeof(Hashtable))]
public sealed class ConvertFromStringDataCommand : PSCmdlet
{
private string _stringData;
/// <summary>
/// The list of properties to display.
/// These take the form of an PSPropertyExpression.
/// </summary>
/// <value></value>
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
[AllowEmptyString]
public string StringData
{
get
{
return _stringData;
}
set
{
_stringData = value;
}
}
/// <summary>
/// Gets or sets the delimiter.
/// </summary>
[Parameter(Position = 1)]
public char Delimiter { get; set; } = '=';
/// <summary>
/// </summary>
protected override void ProcessRecord()
{
Hashtable result = new(StringComparer.OrdinalIgnoreCase);
if (string.IsNullOrEmpty(_stringData))
{
WriteObject(result);
return;
}
string[] lines = _stringData.Split('\n', StringSplitOptions.TrimEntries);
foreach (string line in lines)
{
if (string.IsNullOrEmpty(line) || line[0] == '#')
continue;
int index = line.IndexOf(Delimiter);
if (index <= 0)
{
throw PSTraceSource.NewInvalidOperationException(
ConvertFromStringData.InvalidDataLine,
line);
}
string name = line.Substring(0, index);
name = name.Trim();
if (result.ContainsKey(name))
{
throw PSTraceSource.NewInvalidOperationException(
ConvertFromStringData.DataItemAlreadyDefined,
line,
name);
}
string value = line.Substring(index + 1);
value = value.Trim();
value = Regex.Unescape(value);
result.Add(name, value);
}
WriteObject(result);
}
}
}