-
Notifications
You must be signed in to change notification settings - Fork 0
/
whence.ps1
93 lines (83 loc) · 1.99 KB
/
whence.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# Find out where/what kind of command "cmdname" is...
#
$local:whence_usage =
"usage: which [-v] [-a] [-e extension] cmdname
-v - verbose - show search progress in window title
-a - search for all possible matches, not just the first
-h - show this help text
-e - look for a particular extension
"
$local:basename = ""
$local:all = 0
$local:verbose = 0
$local:name = ""
$local:result = @()
$local:extensions = @()
#
# Crack the args...
#
if ($args.length -lt 1) { return ($whence_usage) }
foreach ($arg in $args) {
switch -regex ($arg) {
{ $doExt } { $extensions += ".$_",""; $doExt=0; continue }
"^-a" { $all = 1 ; continue }
"^[-/](h|help)" { return ($whence_usage) }
"^-v" { $verbose = 1 ; continue }
"^-e" { $doExt=1 ; continue }
"^-e" {
if ($foreach.movenext()) {
$extensions += ("."+$foreach.current),""
} else {
return "error: -e requires an argument"
}
continue }
"^-." { return "bad argument: $arg`n$whence_usage" }
"^[^-]" { $basename = $arg ; continue}
}
}
# make sure a name was actually requested...
if (! $basename ) {
return ("You must specify a name to search for`n$whence_usage")
}
# Check to see if it's an alias
if (test-path "alias:$basename") {
"alias $basename " + $(get-alias $basename).Definition
if (! $all) {
return
}
}
# now see if it's a function
if (test-path "function:$basename") {
"function $basename"
if (! $all) {
return
}
}
# now see if it's a cmdlet
if ( get-command -type cmdlet $basename -ea silentlycontinue )
{
"Cmdlet $basename"
}
# finally search the file system...
# if an extensions wasn't specified, search the path using all possible
# extensions...
if (! $extensions) {
$extensions = @(".ps1");
$extensions += $ENV:PATHEXT.Split(";");
}
foreach ($ext in $extensions) {
foreach ($path in $ENV:PATH.Split(";") ) {
$name = "${path}\${basename}${ext}"
$name = $name.Replace("`"", "")
if ($verbose) {
write-host "Checking $name"
}
if (test-path $name) {
$name
if (! $all) {
return
}
}
}
}