Read Multiple Values for Common Key into Array#43
Conversation
lipkau
left a comment
There was a problem hiding this comment.
Interesting suggestion.
This can be a problem, as implementations on this vary:
https://en.wikipedia.org/wiki/INI_file#Duplicate_names
|
Regarding the duplicate names: I think it is up to the program how to interprete the ini file. However, when dealing with ini files on the command line, you usually want to do changes to an existing file. If you know, a program does not support multiple entries, don't create any. If a program does, then the tool is of no help if it can't support that. |
lipkau
left a comment
There was a problem hiding this comment.
Agreed. That's why I didn't reject the PR.
I made suggestions on how to change the code.
here is my POC for the changes:
$section = "section"
$name = "key"
$delimiter = " = "
$ini = @{section = @{}}
"value", "second value", "third value", "forth value" | % {
$value = $_
if ( -not $ini[$section][$name]) {
$ini[$section][$name] = @($value)
}
else {
$ini[$section][$name] += $value
}
}
$ini[$section]["other"] = "stuff"
$ini[$section][$name]
$ini[$section][$name].gettype()
"" > dumpfile.txt
"key", "other" | % {
$key = $_
$ini[$section][$key] | Foreach-Object {"$key$delimiter$_"} | Add-Content -Path dumpfile.txt
}
cat dumpfile.txt|
Also: |
|
@lipkau Please let me know if you think more work is required on this, before it can be merged. |
|
Dear @lipkau FYI - Similar problem as with versions 1.2.38 / 42 and 2.0.0.0: This change introduces a breaking change between version 2.0.5 and 2.0.6. Example: my.ini behavior 2.0.5 > Import-Module PsIni -MaximumVersion 2.0.5
> $c = Get-IniContent -FilePath C:\temp\my.ini
> $c.Trace
Name Value
---- -----
Path C:\temp
PurgeAfterDays 5
Level Warning
> $c.Trace.Path.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Objectbehavior 2.0.6 > Import-Module PsIni -MinimumVersion 2.0.6
> $c = Get-IniContent -FilePath C:\temp\my.ini
> $c.Trace
Name Value
---- -----
Path {C:\temp}
PurgeAfterDays {5}
Level {Warning}
PS C:\Users\ese> $c.Trace.Path.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.ArrayBecause I have to support PsIni version 2.x, I have to deal with both behaviors. Best regards |
|
I am surprised it works for you.
|
|
I expressed myself wrong: I don't have to support Powershell 2.0, I need to support PsIni in Version 2.X. I told my users: "To use my module, you need PsIni in Major/Minor-Version 2.0, because my module reads and writes ini-files." I don't have control over all systems where my module is running. Some of my ini-related module-functions are now failling when someone updates PsIni from <=2.0.5 to 2.0.6 because of the behavior changes in this pull request. In my next module version, I will use PsIni as nested module to be sure, which version of PsIni my module is using. This will remove the external dependency to PsIni. |
Values should only be of type [array] when necessary. [string] otherwise as mentioned by @EliaSaSe in #43 (comment)
|
should be fixed |
|
Cool. Thank you. |
Read Multiple Values for Common Key into Array
Values should only be of type [array] when necessary. [string] otherwise as mentioned by @EliaSaSe in #43 (comment)
Dear lipkau,
thank you very much for the great tool. When I wanted to use it, I encountered the problem that the ini files I have to modify contain several keys in a section with the same name. Therefore, I'd like to propose to read in those entries as an array.
Please consider adding this feature. This pullrequest is a simple approach to solve that problem. Probably, Set-IniContent and Out-IniFile needs adaption, too. I think you will know best which adjustments need to be made. However, if you are not able to do the necessary changes, please let me know what is required.
Kind regards
Konstantin