What is the naming convention for Powershell functions with regard to upper/lower case usage?

依然范特西╮ 提交于 2019-12-18 05:43:50

问题


While I did find a lot of information on how to name Cmdlets and functions in the Cmdlet Development Guidelines I did not find any information on whether functions should be named in upper or in lower case.

What is the convention here?

(I do understand that Cmdlets themselves are generally named in upper case, even though they are not case-sensitive when it comes to executing.)


回答1:


Naming convention can be tricky. While a fixed naming convention may provide aesthetics or simplified usage, it is not required to be followed. In general, a naming convention that I advocate for is the one that is used already in Powershell. As functions are created on verb-noun base, each word starts with a capital letter or if it is an abbreviation - all capitals, or if it is a proprietary - then as it is accordingly.

I have, for example, created some functions for myself:

Get-ServerDiag
Mount-TrueCryptVolumes
Start-RDP
Generate-RandomPassword
Nuke-Environment

You can imagine what these functions do, it is rather clear, straightforward and compliant with built-in Powershell functions. I do however have exceptions which come from "importing" a several Unix commands to Powershell (like killall, pidof etc...) You can always use a Set-Alias if you prefer to write something else.

This question, however important, is discussable as there does not seem to be the 'one, best way'. It is all, in the end, up to personal preferences.




回答2:


A function is mostly a script-based cmdlet. A cmdlet is written in ex. C#, while a function is written as a script. Because of this similarity, I recommend using the same style as cmdlets for my "standalone-functions" so that it blends in with the other PowerShell cmdlets. Ex. I had a filecount(per folder)-function that I used often. I called it Get-FileCount.

However, I usually name helper functions(functions you only use in other functions) using a simpler name like convertsidtousername etc.

You could use aliases to create short names for a function.



来源:https://stackoverflow.com/questions/24882537/what-is-the-naming-convention-for-powershell-functions-with-regard-to-upper-lowe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!