Comments (3)
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).
about_Quoting_Rules
is probably the best place to document this, though it is currently being discussed in the larger context of parsing modes in MicrosoftDocs/PowerShell-Docs#3440
As for use of unquoted key names in hash tables:
Without having looked at the source code, my understanding is as follows:
-
If a key is a syntactically valid number literal, it is used as a number - including with suffixes such as
u
(U
) for[uint32]
and binary multiplier suffixes such askb
, as well as hex. numbers (e.g.,0x10
) or numbers in exponential notation (e.g.,1e2
- , it is used as a number (this explains the2U
key turning into2
of typeSystem.UInt32
in your example). -
If a key starts with a digit, but is otherwise not a syntactically valid number literal, the hashtable definition fails.
- Note: digit refers not just to the Latin decimal digits, but to all digits in the
DecimalDigitNumber
(Nd
) Unicode category, which includes characters such asเน
(THAI DIGIT ZERO,U+0E50
)
- Note: digit refers not just to the Latin decimal digits, but to all digits in the
-
Otherwise, it is interpreted as a string, but parsing only succeeds if the token is limited to a sequence of the following characters (these seem to be the same that can be used in variable names without needing to enclose the name in
{...}
- see theabout_Variables
help topic's Variable Names that Include Special Characters section):-
Characters from the following Unicode categories (defined in .NET as enumeration
System.Globalization.UnicodeCategory
the two-letter shorthands listed in parentheses can be used with\p{<shortCategoryName>}
in regular expressions):UppercaseLetter
(Lu
)LowercaseLetter
(Ll
)TitlecaseLetter
(Lt
)ModifierLetter
(Lm
)OtherLetter
(Lo
)DecimalDigitNumber
(Nd
) - except as the first character (variable names don't have this restriction)
-
_
(underscore, the only allowed symbol)
-
To put it in terms of guidance:
Quote your hash-table keys, if:
-
they should be strings but happen to start with a digit.
-
they are strings that contain whitespace or a symbol other than
_
from powershellpracticeandstyle.
@mklement0, thank you for the comprehensive information, it is very helpful.
from powershellpracticeandstyle.
It's worth pointing out that this same advice applies whenever you're interacting with a hashtable -- even when you're calling $hashtable.Add(
or getting or setting via dot notation or indexing. I.e.:
$hashtable[2u] = "UInt"
$hashtable.2u
Also worth noting is that good syntax highlighting (such as at the prompt in PSReadline) will make the distinction really obvious:
from powershellpracticeandstyle.
Related Issues (20)
- Question: Best Practice fot HOT 3
- Indentation advice is bad/wrong HOT 9
- Constant variables placement with in a script. HOT 1
- Is it better to use $PSItem vs $error[0] or $_ HOT 2
- Add Set-Variable vs. $foo = "bar" best practice HOT 2
- Correct formatting HOT 5
- Indentation on Comment-Based help HOT 5
- Consider revising PERF-02 HOT 1
- Naming-Conventions refers to a broken link
- Guidance on External Module Dependencies HOT 3
- Suggestion - explain why the second command in PERF-01 is faster
- Capitalization of function parameter type HOT 2
- Broken link and sentence fragment on naming conventions page
- Module structure HOT 4
- Avoid Using Semicolons (`;`). HOT 3
- Parameter Comments and Get-Help HOT 2
- Begin/Process/End or Process only. No Process/End, Begin/End, Begin/Process. HOT 5
- Indentation for lines after a pipe character HOT 3
- How to deal with a module that contains multiple interdependent classes? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershellpracticeandstyle.