teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

8

コード修正

2017/09/20 11:44

投稿

mitarai
mitarai

スコア223

answer CHANGED
@@ -13,7 +13,8 @@
13
13
  [string]$Title = $null,
14
14
  [string]$Message = "",
15
15
  [string]$Default = $null,
16
- [parameter(Mandatory = $true, Position = 0)][System.Collections.IDictionary]$Selections
16
+ [parameter(Mandatory = $true, Position = 0)]
17
+ [System.Collections.IDictionary]$Selections
17
18
  )
18
19
 
19
20
  if ($Message -eq "" -and (-not $Default)) { $Message = " " }

7

コード修正

2017/09/20 11:44

投稿

mitarai
mitarai

スコア223

answer CHANGED
@@ -8,26 +8,26 @@
8
8
  ですがこれだとご希望の動作にならないでしょう。
9
9
  なので自分で作るしかないと思います。以下のような関数でどうでしょうか。
10
10
  ```PowerShell
11
- function Choice
11
+ function Choice {
12
- {
13
12
  param (
14
13
  [string]$Title = $null,
15
- [string]$Message = "選択してください",
14
+ [string]$Message = "",
16
15
  [string]$Default = $null,
17
- [parameter(Mandatory=$true, Position=0)][System.Collections.IDictionary]$Selections
16
+ [parameter(Mandatory = $true, Position = 0)][System.Collections.IDictionary]$Selections
18
17
  )
19
18
 
19
+ if ($Message -eq "" -and (-not $Default)) { $Message = " " }
20
- if($Title) { Write-Host $Title }
20
+ if ($Title) { Write-Host $Title }
21
21
  do {
22
22
  $Selections.GetEnumerator() |
23
- ForEach-Object { Write-Host ("`[" + $_.Key + "`] " + $_.Value) `
23
+ ForEach-Object { Write-Host ("[{0}] {1}" -f $_.key, $_.Value) `
24
- -ForegroundColor $(if($_.Key -eq $Default) {'Yellow'} else {'White'}) }
24
+ -ForegroundColor $(if ($_.Key -eq $Default) {"Yellow"} else {"White"}) }
25
-
25
+
26
- $result = Read-Host $Message $(if($Default) {"(規定値は'$Default')"})
26
+ $result = Read-Host ("{0}{1}" -f $Message, $(if ($Default) {"(規定値は'$Default')"}))
27
- if($result -eq '') { $result = $Default }
27
+ if ($result -eq "") { $result = $Default }
28
- } while($result -notin $Selections.Keys)
28
+ } while ($result -notin $Selections.Keys)
29
-
29
+
30
- return $result
30
+ $result
31
31
  }
32
32
  ```
33
33
 

6

コード修正

2017/09/11 03:23

投稿

mitarai
mitarai

スコア223

answer CHANGED
@@ -14,7 +14,7 @@
14
14
  [string]$Title = $null,
15
15
  [string]$Message = "選択してください",
16
16
  [string]$Default = $null,
17
- [parameter(Mandatory=$true, Position = 0)][System.Collections.IDictionary]$Selections
17
+ [parameter(Mandatory=$true, Position=0)][System.Collections.IDictionary]$Selections
18
18
  )
19
19
 
20
20
  if($Title) { Write-Host $Title }

5

コード修正

2017/09/09 22:25

投稿

mitarai
mitarai

スコア223

answer CHANGED
@@ -14,7 +14,7 @@
14
14
  [string]$Title = $null,
15
15
  [string]$Message = "選択してください",
16
16
  [string]$Default = $null,
17
- [parameter(Mandatory=$true)][System.Collections.IDictionary]$Selections
17
+ [parameter(Mandatory=$true, Position = 0)][System.Collections.IDictionary]$Selections
18
18
  )
19
19
 
20
20
  if($Title) { Write-Host $Title }
@@ -34,7 +34,7 @@
34
34
  使い方は以下のようになります。
35
35
  ```PowerShell
36
36
  $selections = [Ordered]@{ 1 = "通常処理を行う。"; 2 = "処理1を挟む"; 3 = "処理1のみで終了させる。"; 4 = "終了する。" }
37
- $ans = Choice -Title "-Menu-" -Message "番号を選択してください" -Selections $selections -Default 4
37
+ $ans = Choice -Title "-Menu-" -Message "番号を選択してください" $selections -Default 4
38
38
  ```
39
39
  出力は以下の通りです。
40
40
  ```PowerShell

4

コード修正

2017/09/09 22:24

投稿

mitarai
mitarai

スコア223

answer CHANGED
@@ -8,7 +8,7 @@
8
8
  ですがこれだとご希望の動作にならないでしょう。
9
9
  なので自分で作るしかないと思います。以下のような関数でどうでしょうか。
10
10
  ```PowerShell
11
- Function Choice
11
+ function Choice
12
12
  {
13
13
  param (
14
14
  [string]$Title = $null,
@@ -18,14 +18,14 @@
18
18
  )
19
19
 
20
20
  if($Title) { Write-Host $Title }
21
- Do {
21
+ do {
22
22
  $Selections.GetEnumerator() |
23
23
  ForEach-Object { Write-Host ("`[" + $_.Key + "`] " + $_.Value) `
24
24
  -ForegroundColor $(if($_.Key -eq $Default) {'Yellow'} else {'White'}) }
25
25
 
26
26
  $result = Read-Host $Message $(if($Default) {"(規定値は'$Default')"})
27
27
  if($result -eq '') { $result = $Default }
28
- } While($result -notin $Selections.Keys)
28
+ } while($result -notin $Selections.Keys)
29
29
 
30
30
  return $result
31
31
  }

3

コード修正

2017/09/09 06:42

投稿

mitarai
mitarai

スコア223

answer CHANGED
@@ -23,7 +23,7 @@
23
23
  ForEach-Object { Write-Host ("`[" + $_.Key + "`] " + $_.Value) `
24
24
  -ForegroundColor $(if($_.Key -eq $Default) {'Yellow'} else {'White'}) }
25
25
 
26
- $result = Read-Host $Message $(if($Default) { "(規定値は'$Default')" })
26
+ $result = Read-Host $Message $(if($Default) {"(規定値は'$Default')"})
27
27
  if($result -eq '') { $result = $Default }
28
28
  } While($result -notin $Selections.Keys)
29
29
 

2

コード修正

2017/09/09 02:36

投稿

mitarai
mitarai

スコア223

answer CHANGED
@@ -17,15 +17,13 @@
17
17
  [parameter(Mandatory=$true)][System.Collections.IDictionary]$Selections
18
18
  )
19
19
 
20
+ if($Title) { Write-Host $Title }
21
+ Do {
20
- $menu = ""
22
+ $Selections.GetEnumerator() |
21
- $Selections.GetEnumerator() | ForEach-Object { $menu += "`[" + $_.Key + "`] " + $_.Value + "`n" }
23
+ ForEach-Object { Write-Host ("`[" + $_.Key + "`] " + $_.Value) `
22
- $menu = $menu.TrimEnd()
24
+ -ForegroundColor $(if($_.Key -eq $Default) {'Yellow'} else {'White'}) }
23
25
 
24
- if($Default) { $Message += " (規定値は'$Default')" }
26
+ $result = Read-Host $Message $(if($Default) { "(規定値は'$Default')" })
25
- Do {
26
- if($Title) { Write-Host $Title }
27
- Write-Host $menu
28
- $result = Read-Host $Message
29
27
  if($result -eq '') { $result = $Default }
30
28
  } While($result -notin $Selections.Keys)
31
29
 

1

コード修正

2017/09/09 02:29

投稿

mitarai
mitarai

スコア223

answer CHANGED
@@ -26,7 +26,7 @@
26
26
  if($Title) { Write-Host $Title }
27
27
  Write-Host $menu
28
28
  $result = Read-Host $Message
29
- if(($result -eq '') -and $Default) { $result = $Default }
29
+ if($result -eq '') { $result = $Default }
30
30
  } While($result -notin $Selections.Keys)
31
31
 
32
32
  return $result