Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 4e8d942

Browse files
authored
chore: Unify getopt for some commands (#144)
1 parent c2f53ac commit 4e8d942

6 files changed

Lines changed: 152 additions & 124 deletions

File tree

bin/scoop.ps1

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Requires -Version 5
2-
param([string] $cmd)
2+
param([String] $Command)
33

44
Set-StrictMode -Off
55

@@ -17,32 +17,34 @@ Upgrade PowerShell: 'https://docs.microsoft.com/en-us/powershell/scripting/insta
1717

1818
Reset-Alias
1919

20-
$exitCode = 0
20+
$ExitCode = 0
2121

22-
# Powershell automatically bind bash like short parameters as $args, and does not put it in $cmd parameter
22+
# Powershell automatically bind bash like short parameters as $args, and does not put it in $Command parameter
2323
# ONLY if:
2424
# - No command passed
2525
# - -v or --version passed
26-
$version = ($cmd -eq '--version') -or (!$cmd -and ($args.Contains('-v')))
26+
$version = ($Command -eq '--version') -or (!$Command -and ($args.Contains('-v')))
2727

2828
# Scoop itself help should be shown only if explicitly asked:
2929
# - No version asked
3030
# - No command passed
3131
# - /?, /help,, /h, --help, -h passed
32-
$scoopHelp = !$version -and (!$cmd -or (($cmd -in @($null, '--help', '/?', '/help', '/h')) -or (!$cmd -and ($args.Contains('-h')))))
32+
$scoopHelp = !$version -and (!$Command -or (($Command -in @($null, '--help', '/?', '/help', '/h')) -or (!$Command -and ($args.Contains('-h')))))
3333

3434
# Valid command execution
35-
$validCommand = $cmd -and ($cmd -in (commands))
35+
$validCommand = $Command -and ($Command -in (commands))
3636

3737
# Command help should be shown only if:
3838
# - No help for scoop asked
39-
# - $cmd is passed
39+
# - $Command is passed
4040
# - --help, -h is in $args
4141
$commandHelp = !$scoopHelp -and $validCommand -and (($args.Contains('--help')) -or ($args.Contains('-h')))
4242

4343
if ($version) {
44-
Write-UserMessage -Message "PowerShell version: $($PSVersionTable.PSVersion)" -Output
45-
Write-UserMessage -Message 'Current Scoop (Shovel) version:' -Output
44+
Write-UserMessage -Output -Message @(
45+
"PowerShell version: $($PSVersionTable.PSVersion)"
46+
'Current Scoop (Shovel) version:'
47+
)
4648
Invoke-GitCmd -Command 'VersionLog' -Repository (versiondir 'scoop' 'current')
4749

4850
# TODO: Export to lib/buckets
@@ -57,18 +59,16 @@ if ($version) {
5759
}
5860
} elseif ($scoopHelp) {
5961
Invoke-ScoopCommand 'help'
60-
$exitCode = $LASTEXITCODE
62+
$ExitCode = $LASTEXITCODE
6163
} elseif ($commandHelp) {
62-
# TODO: getopt adoption
63-
# Invoke-ScoopCommand 'help' @($cmd)
64-
Invoke-ScoopCommand 'help' @{ 'cmd' = $cmd }
65-
$exitCode = $LASTEXITCODE
64+
Invoke-ScoopCommand 'help' @($Command)
65+
$ExitCode = $LASTEXITCODE
6666
} elseif ($validCommand) {
67-
Invoke-ScoopCommand $cmd $args
68-
$exitCode = $LASTEXITCODE
67+
Invoke-ScoopCommand $Command $args
68+
$ExitCode = $LASTEXITCODE
6969
} else {
70-
Write-UserMessage -Message "scoop: '$cmd' isn't a scoop command. See 'scoop help'." -Output
71-
$exitCode = 2
70+
Write-UserMessage -Message "scoop: '$Command' isn't a scoop command. See 'scoop help'." -Output
71+
$ExitCode = 2
7272
}
7373

74-
exit $exitCode
74+
exit $ExitCode

libexec/scoop-export.ps1

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,82 @@
11
# Usage: scoop export [<OPTIONS>]
2-
# Summary: Exports (an importable) list of installed applications.
2+
# Summary: Export (an importable) list of installed applications.
33
#
44
# Options:
55
# -h, --help Show help for this command.
66

7-
'core', 'Versions', 'manifest', 'buckets' | ForEach-Object {
7+
'core', 'manifest', 'buckets', 'getopt', 'Versions' | ForEach-Object {
88
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
99
}
1010

11-
# TODO: Export:
12-
# - [ ] all installed versions
13-
# - [ ] buckets
14-
# - [ ] config
15-
# - [ ] json
11+
<#
12+
TODO: Export:
13+
{
14+
"applications": [
15+
{
16+
"name": "xxx",
17+
"version": "xxxx",
18+
"bucket": "xxx",
19+
"url": "xxx",
20+
"architecture": "xxx",
21+
"global": false|true,
22+
"installedVersions": [
23+
"xxx",
24+
...
25+
]
26+
},
27+
{...},
28+
...
29+
],
30+
"buckets": [
31+
{
32+
"name": "xxx",
33+
"url": "xxx"
34+
}
35+
],
36+
"config": {
37+
...wholeConfig
38+
},
39+
environments??
40+
}
41+
#>
1642

1743
Reset-Alias
1844

19-
$def_arch = default_architecture
45+
$ExitCode = 0
46+
$Options, $null, $_err = getopt $args
47+
48+
if ($_err) { Stop-ScoopExecution -Message "scoop export: $_err" -ExitCode 2 }
2049

2150
$local = installed_apps $false | ForEach-Object { @{ 'name' = $_; 'global' = $false } }
2251
$global = installed_apps $true | ForEach-Object { @{ 'name' = $_; 'global' = $true } }
2352

24-
$apps = @($local) + @($global)
25-
26-
if ($apps) {
27-
$apps | Sort-Object -Property 'Name' | ForEach-Object {
28-
$app = $_.name
29-
$global = $_.global
30-
$ver = Select-CurrentVersion -AppName $app -Global:$global
31-
$global_display = $null; if ($global) { $global_display = ' *global*' }
32-
33-
$install_info = install_info $app $ver $global
34-
$bucket = ''
35-
if ($install_info.bucket) {
36-
$bucket = ' [' + $install_info.bucket + ']'
37-
} elseif ($install_info.url) {
38-
$bucket = ' [' + $install_info.url + ']'
39-
}
40-
if ($install_info.architecture -and $def_arch -ne $install_info.architecture) {
41-
$arch = ' {' + $install_info.architecture + '}'
42-
} else {
43-
$arch = ''
44-
}
53+
$Applications = @($local) + @($global)
4554

46-
# "$app (v:$ver) global:$($global.toString().tolower())"
47-
"$app (v:$ver)$global_display$bucket$arch"
55+
# TODO: What to do?
56+
if (!$Applications) { exit $ExitCode }
57+
58+
# Export applications
59+
$Applications | Sort-Object -Property 'Name' | ForEach-Object {
60+
$app = $_.name
61+
$global = $_.global
62+
$ver = Select-CurrentVersion -AppName $app -Global:$global
63+
$globalDisplay = if ($global) { ' *global*' } else { $null }
64+
$installInfo = install_info $app $ver $global
65+
$bucket = ''
66+
67+
if ($installInfo.bucket) {
68+
$bucket = ' [' + $installInfo.bucket + ']'
69+
} elseif ($installInfo.url) {
70+
$bucket = ' [' + $installInfo.url + ']'
4871
}
72+
73+
if ($installInfo.architecture -and ((default_architecture) -ne $installInfo.architecture)) {
74+
$arch = ' {' + $installInfo.architecture + '}'
75+
} else {
76+
$arch = ''
77+
}
78+
79+
Write-UserMessage -Message "$app (v:$ver)$globalDisplay$bucket$arch" -Output
4980
}
5081

51-
exit 0
82+
exit $ExitCode

libexec/scoop-help.ps1

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,41 @@
44
# Options:
55
# -h, --help Show help for this command.
66

7-
param($cmd)
8-
9-
# TODO: getopt adoption
10-
11-
'help', 'Helpers' | ForEach-Object {
7+
'help', 'Helpers', 'getopt' | ForEach-Object {
128
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
139
}
1410

1511
Reset-Alias
1612

17-
$exitCode = 0
18-
$commands = commands
13+
$ExitCode = 0
14+
$Options, $Command, $_err = getopt $args
1915

20-
if (!($cmd)) {
16+
if ($_err) { Stop-ScoopExecution -Message "scoop help: $_err" -ExitCode 2 }
17+
18+
if (!($Command)) {
2119
Write-UserMessage -Output -Message @(
22-
'Usage: scoop [<OPTIONS>] [<COMMAND>]'
20+
'Usage: scoop [<COMMAND>] [<OPTIONS>]'
2321
''
2422
'Windows command line installer'
2523
''
26-
'General exit codes'
24+
'General exit codes:'
2725
' 0 - Everything OK'
2826
' 1 - No parameter provided or usage shown'
2927
' 2 - Argument parsing error'
3028
' 3 - General execution error'
3129
' 4 - Permission/Privileges related issue'
32-
' 10 + - Number of failed actions (installations, updates, ...)'
30+
' 10+ - Number of failed actions (installations, updates, ...)'
3331
''
3432
'Type ''scoop help <COMMAND>'' to get help for a specific command.'
3533
''
3634
'Available commands are:'
3735
)
3836
print_summaries
39-
} elseif ($commands -contains $cmd) {
40-
print_help $cmd
37+
} elseif ((commands) -contains $Command) {
38+
print_help $Command
4139
} else {
42-
$exitCode = 3
43-
Write-UserMessage -Message "scoop help: no such command '$cmd'" -Output
40+
$ExitCode = 3
41+
Write-UserMessage -Message "scoop help: no such command '$Command'" -Output
4442
}
4543

46-
exit $exitCode
47-
44+
exit $ExitCode

libexec/scoop-home.ps1

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
# Usage: scoop home [<OPTIONS>] <APP>
2-
# Summary: Opens the application's homepage in default browser.
2+
# Summary: Open the application's homepage in default browser.
33
#
44
# Options:
55
# -h, --help Show help for this command.
66

7-
param($app)
8-
9-
# TODO: getopt adoption
10-
11-
'core', 'help', 'manifest', 'buckets' | ForEach-Object {
7+
'core', 'help', 'Helpers', 'getopt', 'manifest', 'buckets' | ForEach-Object {
128
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
139
}
1410

1511
Reset-Alias
1612

17-
$exitCode = 0
13+
$ExitCode = 0
14+
$Options, $Application, $_err = getopt $args
15+
16+
if ($_err) { Stop-ScoopExecution -Message "scoop home: $_err" -ExitCode 2 }
17+
if (!$Application) { Stop-ScoopExecution -Message 'Parameter <APP> missing' -Usage (my_usage) }
1818

19-
if ($app) {
20-
# TODO: Resolve-ManifestInformation
21-
$manifest, $bucket = find_manifest $app
22-
if ($manifest) {
23-
if ([String]::IsNullOrEmpty($manifest.homepage)) {
24-
$exitCode = 3
25-
Write-UserMessage -Message "Could not find homepage in manifest for '$app'." -Err
26-
} else {
27-
Start-Process $manifest.homepage
28-
}
19+
# TODO: Adopt Resolve-ManifestInformation
20+
$manifest, $null = find_manifest $Application
21+
if ($manifest) {
22+
if ([String]::IsNullOrEmpty($manifest.homepage)) {
23+
Write-UserMessage -Message 'Manifest does not contain homepage property' -Err
24+
$ExitCode = 3
2925
} else {
30-
$exitCode = 3
31-
Write-UserMessage -Message "Could not find manifest for '$app'." -Err
26+
Start-Process $manifest.homepage
3227
}
3328
} else {
34-
Stop-ScoopExecution -Message 'Parameter <APP> missing' -Usage (my_usage)
29+
Write-UserMessage -Message "Could not find manifest for '$Application'" -Err
30+
$ExitCode = 3
3531
}
3632

37-
exit $exitCode
33+
exit $ExitCode

libexec/scoop-prefix.ps1

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@
44
# Options:
55
# -h, --help Show help for this command.
66

7-
param($app)
8-
# TODO: getopt adoption
9-
# TODO: Add --global
10-
11-
'core', 'help', 'Helpers', 'manifest', 'buckets' | ForEach-Object {
7+
'core', 'help', 'Helpers', 'getopt', 'manifest', 'buckets' | ForEach-Object {
128
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
139
}
1410

1511
Reset-Alias
1612

17-
$exitCode = 0
13+
$ExitCode = 0
14+
$Options, $Application, $_err = getopt $args
1815

19-
if (!$app) { Stop-ScoopExecution -Message 'Parameter <APP> missing' -Usage (my_usage) }
16+
if ($_err) { Stop-ScoopExecution -Message "scoop prefix: $_err" -ExitCode 2 }
17+
if (!$Application) { Stop-ScoopExecution -Message 'Parameter <APP> missing' -Usage (my_usage) }
18+
19+
# TODO: Test if application is installed first
20+
# Same flow as for hold/unhold
21+
# TODO: Add --global
22+
# TODO: Respect NO_JUNCTION
23+
$Application = $Application[0]
24+
$ApplicationPath = versiondir $Application 'current' $false
2025

21-
# TODO: NO_JUNCTION
22-
$app_path = versiondir $app 'current' $false
23-
if (!(Test-Path $app_path)) { $app_path = versiondir $app 'current' $true }
26+
if (!(Test-Path $ApplicationPath)) { $ApplicationPath = versiondir $Application 'current' $true }
2427

25-
if (Test-Path $app_path) {
26-
Write-UserMessage -Message $app_path -Output
28+
if (Test-Path $ApplicationPath) {
29+
Write-UserMessage -Message $ApplicationPath -Output
2730
} else {
28-
$exitCode = 3
29-
Write-UserMessage -Message "Could not find app path for '$app'." -Err
31+
Write-UserMessage -Message "'$Application' is not installed" -Err
32+
$ExitCode = 3
3033
}
3134

32-
exit $exitCode
35+
exit $ExitCode

0 commit comments

Comments
 (0)