Kelsidavis-WoWee/tools/editor/cli_arg_parse.hpp
Kelsi ec569768fe refactor(editor): hoist consumeJsonFlag into cli_arg_parse.hpp
The pattern

  bool jsonOut = (i + 1 < argc &&
                  std::strcmp(argv[i + 1], "--json") == 0);
  if (jsonOut) i++;

is repeated ~50 times across the editor — every --info-*
and --validate-* handler writes the same three lines to
detect and consume an optional --json follower. Extract
to consumeJsonFlag(int& i, int argc, char** argv) in
cli_arg_parse.hpp (the same header that already hosts
parseOptInt / parseOptFloat / parseOptUint / parseOptArg
for similar repeated patterns).

Adopted in the recently-added files:
  • cli_world_map.cpp     — both --info-womx and --validate-womx
  • cli_sound_catalog.cpp — both --info-wsnd and --validate-wsnd
  • cli_wom_info.cpp      — all 7 --info-* / --validate-wom
                            handlers (replace_all)

Also adopted the shared stripExt() helper from
cli_box_emitter.hpp instead of the new files' rolled-
own stripWomxExt / stripWsndExt — same observable behavior,
no more local copies of the extension-strip logic.

Future --info-/-validate handlers added to other formats
(WSP, WTC, etc.) get the same one-line jsonOut detection
without reinventing the peek-and-advance dance. Any later
adoption of consumeJsonFlag in the older 50+ sites is now
a mechanical replace_all edit per file.

Behavior preserved: --validate-wom and --info-womx and
--info-wsnd round-trip exactly as before, both text and
--json output unchanged.
2026-05-09 14:50:27 -07:00

58 lines
1.8 KiB
C++

#pragma once
#include <cstdint>
#include <cstring>
#include <string>
namespace wowee {
namespace editor {
namespace cli {
// Common pattern across cli_gen_texture and cli_gen_mesh handlers:
// "if there's another arg AND it doesn't look like a switch, parse
// it into <var>; otherwise leave <var> at its default". 465+ copies
// across the two files were each writing this 3-line block manually.
//
// Each helper silently no-ops on parse failure so the caller's
// default value is preserved — matches the prior try/catch
// behavior exactly.
inline bool parseOptArg(int& i, int argc, char** argv) {
return i + 1 < argc && argv[i + 1][0] != '-';
}
inline void parseOptInt(int& i, int argc, char** argv, int& value) {
if (parseOptArg(i, argc, argv)) {
try { value = std::stoi(argv[++i]); } catch (...) {}
}
}
inline void parseOptFloat(int& i, int argc, char** argv, float& value) {
if (parseOptArg(i, argc, argv)) {
try { value = std::stof(argv[++i]); } catch (...) {}
}
}
inline void parseOptUint(int& i, int argc, char** argv, uint32_t& value) {
if (parseOptArg(i, argc, argv)) {
try { value = static_cast<uint32_t>(std::stoul(argv[++i])); }
catch (...) {}
}
}
// Common --json-output flag pattern: every --info-* / --validate-*
// handler (~50 sites across the editor) writes the same three lines
// to detect and consume an optional `--json` follower. Hoisted here
// so future handlers can do `bool jsonOut = consumeJsonFlag(i, argc, argv);`
// instead of the open-coded peek-and-advance.
inline bool consumeJsonFlag(int& i, int argc, char** argv) {
if (i + 1 < argc && std::strcmp(argv[i + 1], "--json") == 0) {
++i;
return true;
}
return false;
}
} // namespace cli
} // namespace editor
} // namespace wowee