activescott / lessmsi

A tool to view and extract the contents of an Windows Installer (.msi) file.
https://lessmsi.activescott.com
MIT License
1.29k stars 150 forks source link

Return non-zero exit code on error #177

Closed homes32 closed 5 months ago

homes32 commented 1 year ago

I would love for the cli version to return a non-zero exit code when an error is encountered. Currently if I am extracting a file via a script there is no way my script can tell if the operation failed and make a decision on what to do next.

Example:

0 - Success 1 - MSI file not found 2 - Extraction failed 3 - File not found in MSI ...

activescott commented 1 year ago

Great suggestion. I'd welcome a PR and be happy to support you if you're interested in adding it. I'll put this high on the list though as I can see how it'd be super useful. Thank you for taking the time to leave the suggestion!

activescott commented 5 months ago

This seems to mostly work - albeit not with the specific error codes you wanted. Here is what I tested:

@setlocal

set "errorlevel=1"
set "errorlevel="

rmdir /s /q .\temp-out
mkdir .\temp-out

REM "C:\Users\User\Desktop\lessmsi-v1.6.201\lessmsi.exe" x "TestFiles\MsiInput\NUnit-2.5.2.9222.msi" temp-out
"C:\Users\User\Desktop\lessmsi-v1.6.201\lessmsi.exe" x "TestFiles\MsiInput\VBRuntime.msi" temp-out

if %errorlevel% neq 0 (echo error level is non-zero. It was %errorlevel%) else (echo error level is zero.)

Note the somewhat special way of checking errorlevel because it returns negative errorlevels (hat tip to https://stackoverflow.com/questions/10935693/foolproof-way-to-check-for-nonzero-error-return-code-in-windows-batch-file).

If I test with VBRuntime.msi (which I know to always fail) the script writes: error level is non-zero. It was -1073741819

If I use the Nunit-2.....msi the script writes: error level is zero.

So I think the basic case works here. However, I did some quick looking and getting tot he point of detecting each scenario you mentioned is a fair bit of work (mostly testing).

I'm going to close this one, but if you want feel free to tag me on it and suggest re-opening if I'm missing something.