140 lines
4.5 KiB
C++
140 lines
4.5 KiB
C++
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2025 Vanessa T. <nessa@neko-tools.de>
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
#include <stdexcept>
|
|
#include "CommandLine.h"
|
|
#include "Utility.h"
|
|
|
|
enum class Option {
|
|
None,
|
|
ProcessId,
|
|
ImagePath,
|
|
WindowTitle,
|
|
Text,
|
|
Order
|
|
};
|
|
|
|
void CommandLine::parse(const std::vector<std::string>& args) {
|
|
auto option = Option::None;
|
|
|
|
for (auto& arg : args) {
|
|
switch (option) {
|
|
case Option::None:
|
|
if (arg == "-h" || arg == "--help") {
|
|
showHelp = true;
|
|
} else if (arg == "-f" || arg == "--foreground") {
|
|
bringToForeground = true;
|
|
} else if (arg == "-l" || arg == "--list-processes") {
|
|
listProcesses = true;
|
|
} else if (arg == "-r" || arg == "--reverse") {
|
|
reverseOrder = true;
|
|
} else if (arg == "-I" || arg == "--image-path") {
|
|
option = Option::ImagePath;
|
|
} else if (arg == "-W" || arg == "--window-title") {
|
|
option = Option::WindowTitle;
|
|
} else if (arg == "-T" || arg == "--text") {
|
|
option = Option::Text;
|
|
} else if (arg == "-p" || arg == "--process-id") {
|
|
option = Option::ProcessId;
|
|
} else if (arg == "-o" || arg == "--order") {
|
|
option = Option::Order;
|
|
} else {
|
|
throw std::runtime_error("Unknown option: " + arg);
|
|
}
|
|
break;
|
|
case Option::ImagePath:
|
|
imagePath = arg;
|
|
option = Option::None;
|
|
break;
|
|
case Option::WindowTitle:
|
|
windowTitle = arg;
|
|
option = Option::None;
|
|
break;
|
|
case Option::Text:
|
|
text = arg;
|
|
option = Option::None;
|
|
break;
|
|
case Option::ProcessId:
|
|
processId = std::stoi(arg);
|
|
option = Option::None;
|
|
break;
|
|
case Option::Order:
|
|
auto lowerArg = toLowerCase(arg);
|
|
if (arg == "pid") {
|
|
sortOrder = CommandLine::SortOrder::ProcessId;
|
|
} else if (arg == "path") {
|
|
sortOrder = CommandLine::SortOrder::ImagePath;
|
|
} else if (arg == "ram") {
|
|
sortOrder = CommandLine::SortOrder::RamUsage;
|
|
} else {
|
|
throw std::runtime_error("Unknown sort order: " + arg);
|
|
}
|
|
option = Option::None;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!valid()) {
|
|
throw std::runtime_error("Invalid command line");
|
|
}
|
|
}
|
|
|
|
bool CommandLine::valid() const {
|
|
if (showHelp) return true;
|
|
|
|
if (listProcesses) {
|
|
if (processId > 0) return false;
|
|
if (!imagePath.empty()) return false;
|
|
if (!windowTitle.empty()) return false;
|
|
if (!text.empty()) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
if (processId > 0) {
|
|
if (!imagePath.empty()) return false;
|
|
if (!windowTitle.empty()) return false;
|
|
if (!text.empty()) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
if (!imagePath.empty()) {
|
|
if (!windowTitle.empty()) return false;
|
|
if (!text.empty()) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
if (!windowTitle.empty()) {
|
|
if (!text.empty()) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
if (text.empty()) return false;
|
|
|
|
return true;
|
|
}
|