PoshCode Archive  Artifact Content

Artifact 1a5683dc45d375016d00a9a2bf1fb37a5589ea314ef9a45744b956dcf12e1222:

  • File Test-CommandValidation.ps1 — part of check-in [a306414d20] at 2018-06-10 13:29:11 on branch trunk — validate cmdlet and function (user: Walid Toumi size: 1902)

# encoding: ascii
# api: powershell
# title: Test-CommandValidation
# description: validate cmdlet and function
# version: 0.1
# type: function
# author: Walid Toumi
# license: CC0
# function: Test-CommandValidation
# x-poshcode-id: 3675
# x-archived: 2012-10-21T16:42:15
# x-published: 2012-10-01T23:06:00
#
#
#############################################################
#
#  PS II> Test-CommandValidation -command get-process | fl
#            VerbNounConvention : True
#            ReservedKeyWords   : True
#            VerbConvention     : True
#
# author: Walid Toumi
#############################################################

function Test-CommandValidation {

 param($Command,[switch]$SimpleForm)

 $keys = man key |

           Select-String "(\S+)(?=\s{5,}about_*)" |

                select -expand Matches |

                      select -expand value

 $verbNounConvention = $verbconvention = $reservedkeywords = $false

 $verb,$noun = $Command.Split('-')

 if($noun) {

    $verbNounConvention = $true

    if( (get-verb $verb) ) { $verbconvention = $true }

    if($keys -contains $noun) { $reservedkeywords = $true }

 }

 else {

     $reservedkeywords = $verbconvention = $null

 }

  $objPS=new-object PSObject -prop @{

     VerbNounConvention = $verbNounConvention

     VerbConvention = $verbconvention

     ReservedKeyWords = $reservedkeywords

  }

 if($SimpleForm) {

     switch($objPS) {

       {!$_.ReservedKeyWords -and $_.VerbConvention -and $_.VerbNounConvention}

         {Write-Host "PASS !!" -ForegroundColor green}

       {!$_.VerbConvention -and !$_.VerbConvention -and !$_.VerbNounConvention}

         {Write-Host "FAIL !!" -ForegroundColor red}

       default

        { Write-Host "MAYBE !!" -ForegroundColor DarkYellow }

     }

  } else {

    $objPS

 }

}