Skip to content

Commit

Permalink
FEXpidof: Move over to MicroArgParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonicadvance1 committed Sep 18, 2024
1 parent ab561d3 commit 91c1b80
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
6 changes: 1 addition & 5 deletions Source/Tools/pidof/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
add_executable(FEXpidof pidof.cpp)
target_include_directories(FEXpidof
PRIVATE
${PROJECT_SOURCE_DIR}/Source/Common/cpp-optparse/)

target_link_libraries(FEXpidof
PRIVATE
cpp-optparse
Common
fmt::fmt
)

Expand Down
56 changes: 28 additions & 28 deletions Source/Tools/pidof/pidof.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
#include "OptionParser.h"
#include <FEXCore/fextl/string.h>
#include "Common/MicroArgParser.h"

#include <charconv>
#include <cstring>
Expand All @@ -14,35 +15,34 @@ namespace Config {
bool SingleShot {};
bool SkipZombie {true};
bool DoNotDisplay {};
std::string Separator {" "};
std::string_view Separator {" "};
std::unordered_set<int64_t> OmitPids;
std::unordered_set<std::string> Programs;

void LoadOptions(int argc, char** argv) {
optparse::OptionParser Parser {};

Parser.add_option("-s").help("Single shot - Only returns one pid").action("store_true").set_default(SingleShot);

Parser.add_option("-q")
.help("Do not display matched PIDs to stdout. Simply exit with status of true or false if a PID was found")
.action("store_true")
.set_default(DoNotDisplay);

Parser.add_option("-z").help("Try to detect zombie processes - Usually zombie processes are skipped").action("store_false").set_default(SkipZombie);

Parser.add_option("-d").help("Use a different separator if more than one pid is show - Default is space").set_default(Separator);

Parser.add_option("-o").help("Ignore processes with matched pids").action("append");

optparse::Values Options = Parser.parse_args(argc, argv);

SingleShot = Options.get("s");
DoNotDisplay = Options.get("q");
SkipZombie = Options.get("z");
Separator = Options["d"];

for (const auto& Omit : Options.all("o")) {
std::istringstream ss {Omit};
static std::array<FEX::MicroArgParser::ParseMember, 7> Args = {{
{FEX::MicroArgParser::ParseMember::Type::Bool, "-s", {}, "Single shot - Only returns one pid", "0"},
{FEX::MicroArgParser::ParseMember::Type::Bool, "-q", {}, "Do not display matched PIDs to stdout. Simply exit with status of true or false if a PID was found", "0"},
{FEX::MicroArgParser::ParseMember::Type::BoolInvert, "-z", {}, "Try to detect zombie processes - Usually zombie processes are skipped", "1"},
{FEX::MicroArgParser::ParseMember::Type::String, "-d", {}, "Use a different separator if more than one pid is show - Default is space", " "},
{FEX::MicroArgParser::ParseMember::Type::StringArray, "-o", {}, "Ignore processes with matched pids", {}},

// Defaults
{FEX::MicroArgParser::ParseMember::Type::Bool, "-v", "--version", "show program's verison number and exit", "0"},
{FEX::MicroArgParser::ParseMember::Type::Bool, "-h", "--help", "show this help message and exit", "0"},
}};

// Load the arguments
FEX::MicroArgParser Parser(Args);
Parser.Parse(argc, argv);

SingleShot = Parser.Get<bool>("-s");
DoNotDisplay = Parser.Get<bool>("-q");
SkipZombie = Parser.Get<bool>("-z");
Separator = Parser.Get<std::string_view>("-d");

for (const auto& Omit : Parser.GetVector("-o")) {
std::istringstream ss {fextl::string(Omit)};
std::string sub;
while (std::getline(ss, sub, ',')) {
int64_t pid;
Expand All @@ -57,8 +57,8 @@ void LoadOptions(int argc, char** argv) {
}
}

for (const auto& Program : Parser.args()) {
Programs.emplace(Program);
for (size_t i = Parser.GetRemainingArgumentsIndex(); i < argc; ++i) {
Programs.emplace(argv[i]);
}
}
} // namespace Config
Expand Down

0 comments on commit 91c1b80

Please sign in to comment.