0

This is probably a dumb question so forgive me. I've used many cmdlets that have a parameter that is named as a singular object, yet it accepts multiple objects.

This makes sense to me for the parameter to be named singularly. For example:

Process-VM -VMName

However, I'm writing my own function that can do the same; take a singular object or a collection, but I'm having trouble with semantics of naming the parameter singularly.

For example, my function accepts one or more virtual machines. Normally with a collection like this I'd use a foreach loop to enumerate through them, and it would look like

foreach ($vm in $VMNames)

But if I write the parameter singularly it would look something like:

foreach ($vm in $VMName)

For the foreach to make sense to me, my parameter would have to be named plurally but then the function call looks silly to me when I'm passing a single object. Ofc this also deviates from those other well-known cmdlets.

Process-VM -VMNames "myvm"

I feel silly having this problem, but I'm curious how this is handled by someone like Microsoft and their cmdlets.

TIA

2
  • 1
    I think you have already spent too much time thinking about this. When a parameter can accept either 1 or many values, there will always be cases when it doesn't read grammatically. Commented Sep 10, 2024 at 3:37
  • I appreciate your opinion. I'm just trying to follow best practice and wasn't sure which way to go. Commented Sep 10, 2024 at 14:49

2 Answers 2

4

If you're asking for official guidelines then you can checkout Strongly Encouraged Development Guidelines. More specifically for the question being asked, Use Singular Parameter Names:

Avoid using plural names for parameters whose value is a single element. This includes parameters that take arrays or lists because the user might supply an array or list with only one element.

Plural parameter names should be used only in those cases where the value of the parameter is always a multiple-element value. In these cases, the cmdlet should verify that multiple elements are supplied, and the cmdlet should display a warning to the user if multiple elements are not supplied.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't know where my previous comment went but adding it again. Thank you for pointing me in the right direction. Did not know about these guidelines
Glad it was helpful @MatthewMcDonald
0

in your case using plural is logical, and a single entry in the list is a special case. IOW your vmnames array is still an array, just an array with just 1 element.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.