Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Introduce a cmdlet for manipulation of list strings #7975
Manipulating such lists with text parsing is cumbersome, error-prone, and sometimes requires platform-specific separators, so a dedicated cmdlet, say
Operations should be idempotent by default (desired-state logic):
Separators can either be specified:
# WISHFUL THINKING PS> Update-ListString -Separator ';' 'a;b;c' -Append 'd', 'e' a;b;c;d;e PS> Update-ListString -Separator ';' 'a;b;c' -Prepend 'd' d;a;b;c PS> Update-ListString -Separator ';' 'a;b;c;d' -Remove 'd', 'b' a;c PS> Update-ListString -Separator ';' 'a;;c' -RemoveEmpty a;c PS> Update-ListString -Separator ';' 'a;b;a;c' -RemoveDuplicate a;b;c PS> Update-ListString -Separator ';' 'a;b;c' -Replace 'b', 'd' a;d;c # No-op, if entry already present PS> Update-ListString -Separator ';' 'a;b;c' -Append 'b' a;b;c # Enforce specified position PS> Update-ListString -Separator ';' 'a;b;c' -Append 'b' -Force a;c;b
Written as of:
PowerShell Core 6.1.0
You've given it a general name, but the behaviour seems very specific to "
You provisionally call it
ArrayList and Generic List have a
Then what happens if I have two entries for Java and I
ArrayLists and Generic Lists have
If I try to prepend, how could it ever fail such that I need to -Force it? If I try to insert at position 10 but there's only 3 things in the list, what does forcing it do? Make 7 blank entries in between? If I try to insert at position 2 and I don't force it, it has to move the following items +1 index in order to work normally, so in what situation might I need to -Force it, and what would be different?
If I'm giving instructions based on my assumption that something will exist, but it doesn't exist, should that really be "make the change somewhere else instead", or should it really be an error?
I'm not against the idea overall, but it feels tailored to the behaviours which makes sense for environment variables full of paths, and not "Lists" in string form. Escaping rules are likely to also be specific to environment variables. Unless there are other use cases, I think I'd prefer if that specific nature was reflected in whatever name it ends up with.
I think the 90%+ use case is to manage environment variables. Plus there is already a separate need to surface the
I can currently think of only
I was hoping it would generalize to other use cases where a list of items must be kept inside a string, but perhaps that's too rare a use case to worry about.
I can now see how one/multiple
It is now a moot point, but as for your concerns, @HumanEquivalentUnit:
I specifically chose the noun "ListString" to avoid confusion with .NET list types - comparing their methods with what I'm proposing should not enter the picture.
The guiding principle for the suggested operations was desired-state logic, which, for instance, means that something targeted is allowed not to exist, with reasonable fallback behavior.
To address one question of yours as an example:
It couldn't fail, but the suggested default behavior was to leave a preexisting item alone, irrespective of its current position;