-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unquoted strings #146
Comments
PowerShell/PowerShell#6467 tries to give a comprehensive overview of the current behavior with respect to arguments in command parsing mode - however, it doesn't cover the hashtable-key issue (see below).
As for use of unquoted key names in hash tables: Without having looked at the source code, my understanding is as follows:
To put it in terms of guidance: Quote your hash-table keys, if:
|
@mklement0, thank you for the comprehensive information, it is very helpful. |
PowerShell allows for unquoted strings as shown in the Style-Guide/Naming-Conventions:
But there are limitations to using unquoted strings as e.g. spaces are not allowed in an unquoted value and will obviously fail if not quoted (with either single or double quotes, see the related issue: #63):
Besides using unquoted strings in argument values, unquoted strings are common (best practice?) in hash tables keys. Hash tables are major objects in PowerShell along with PSCustomObjects which are in fact constructed from a hash table. Hash tables are also used for things like splatting and PowerShell Data files.
As with argument values, the use of unquoted hash table keys is limited to certain characters, as e.g.:
Will cause an error:
In some cases there are no errors but pitfalls, like:
AFAIK, there is no clear definition of when I should use quoted keys (or argument values) or when I might not. In other words: which characters are allowed for unquoted strings (or: which characters are not allowed for unquoted strings)?
It is also not described in About Quoting Rules or in About Special Characters
(Assuming I am right, should I created a separate documentation issue at the PowerShell Team?)
So usually I use quotes on a trial-on-error bases or pro-actively on a better-safe-than-sorry bases. But that could cause some inconsistency in defining the best (readable) practice to resolve the above error:
Or:
I am especially concerned with this writing style / best practice issue because I am maintaining a ConvertTo-Expression script. Although the code might not follow the styling guidelines (yet), I am committed to follow them for the expression it outputs as much as possible.
For this project, I got the issue request: Remove quotes in HashTables Keys when possible and looking for some documented guidance.
The text was updated successfully, but these errors were encountered: