Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
* Rename pConfigJsonPath to pConfigJsonPaths
* Change commandline parsing to 2 initial passes for better readability
  • Loading branch information
angela28chen committed Aug 16, 2023
1 parent 5ae59a4 commit c4be397
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/ppx/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ struct StandardOptions
#endif

std::shared_ptr<KnobFlag<std::vector<std::string>>> pAssetsPaths;
std::shared_ptr<KnobFlag<std::vector<std::string>>> pConfigJsonPath;
std::shared_ptr<KnobFlag<std::vector<std::string>>> pConfigJsonPaths;
};

// -------------------------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/ppx/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,13 +807,13 @@ void Application::InitStandardKnobs()
"Add a path in front of the assets search path list.");
mStandardOpts.pAssetsPaths->SetFlagParameters("<path>");

mStandardOpts.pConfigJsonPath = mKnobManager.CreateKnob<KnobFlag<std::vector<std::string>>>(mCommandLineParser.GetJsonConfigFlagName(), defaultEmptyList);
mStandardOpts.pConfigJsonPath->SetFlagDescription(
mStandardOpts.pConfigJsonPaths = mKnobManager.CreateKnob<KnobFlag<std::vector<std::string>>>(mCommandLineParser.GetJsonConfigFlagName(), defaultEmptyList);
mStandardOpts.pConfigJsonPaths->SetFlagDescription(
"Additional commandline flags specified in a JSON file. Values specified in JSON files are "
"always lower priority than those specified on the command line. Between different files, the "
"later ones take priority. For lists, the JSON values will come earlier in the array than the "
"command-line values");
mStandardOpts.pConfigJsonPath->SetFlagParameters("<path>");
mStandardOpts.pConfigJsonPaths->SetFlagParameters("<path>");

mStandardOpts.pDeterministic =
mKnobManager.CreateKnob<KnobFlag<bool>>("deterministic", false);
Expand Down
30 changes: 15 additions & 15 deletions src/ppx/command_line_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,12 @@ std::optional<CommandLineParser::ParsingError> CommandLineParser::Parse(int argc
return std::nullopt;
}

// With one initial pass:
// - Split any flag and parameters that are connected with '='
// - Identify JSON config files, add that option, and remove it from the argument list
// Initial pass to trim the name of executable and to split any flag and parameters that are connected with '='
std::vector<std::string_view> args;
for (size_t i = 1; i < argc; ++i) {
std::string_view argString(argv[i]);
auto res = ppx::string_util::SplitInTwo(argString, '=');
if (res == std::nullopt) {
if (StartsWithDoubleDash(argString) &&
argString == "--" + mJsonConfigFlagName &&
i + 1 < argc &&
!StartsWithDoubleDash(argv[i + 1])) {
mOpts.AddOption(mJsonConfigFlagName, ppx::string_util::TrimBothEnds(argv[i + 1]));
++i;
continue;
}
args.emplace_back(argString);
continue;
}
Expand All @@ -131,14 +121,24 @@ std::optional<CommandLineParser::ParsingError> CommandLineParser::Parse(int argc
if (res->second.find('=') != std::string_view::npos) {
return "Unexpected number of '=' symbols in the following string: \"" + std::string(argString) + "\"";
}
if (StartsWithDoubleDash(res->first) && res->first == "--" + mJsonConfigFlagName) {
mOpts.AddOption(mJsonConfigFlagName, ppx::string_util::TrimBothEnds(res->second));
continue;
}
args.emplace_back(res->first);
args.emplace_back(res->second);
}

// Another pass to identify JSON config files, add that option, and remove it from the argument list
std::vector<std::string_view> argsFiltered;
for (size_t i = 0; i < args.size(); ++i) {
bool nextArgumentIsParameter = (i + 1 < args.size()) && !StartsWithDoubleDash(args[i + 1]);
if ((args[i] == "--" + mJsonConfigFlagName) && nextArgumentIsParameter) {
mOpts.AddOption(mJsonConfigFlagName, ppx::string_util::TrimBothEnds(args[i + 1]));
++i;
continue;
}
argsFiltered.emplace_back(args[i]);
}
args = argsFiltered;
argsFiltered.clear();

// Flags inside JSON files are processed first
// These are always lower priority than flags on the command-line
std::vector<std::string> configJsonPaths;
Expand Down

0 comments on commit c4be397

Please sign in to comment.