Giter Club home page Giter Club logo

Comments (3)

mklement0 avatar mklement0 commented on June 11, 2024 4

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 as kb, as well as hex. numbers (e.g., 0x10) or numbers in exponential notation (e.g., 1e2 - , it is used as a number (this explains the 2U key turning into 2 of type System.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)
  • 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 the about_Variables help topic's Variable Names that Include Special Characters section):


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.

iRon7 avatar iRon7 commented on June 11, 2024 1

@mklement0, thank you for the comprehensive information, it is very helpful.

from powershellpracticeandstyle.

Jaykul avatar Jaykul commented on June 11, 2024 1

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:
image

from powershellpracticeandstyle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.