Skip to content

Commit

Permalink
feat: (#1299) Add CLI option for converting files to TOML (#1741)
Browse files Browse the repository at this point in the history
Co-authored-by: Tristan Youngs <[email protected]>
  • Loading branch information
rprospero and trisyoungs committed Apr 9, 2024
1 parent 3e56c74 commit 8cfdae1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,37 @@ int main(int args, char **argv)
}

// Save input file to new output filename and quit?
if (options.writeInputFilename())
if (options.writeInputFilename() || options.toTomlFile())
{
Messenger::print("Saving input file to '{}'...\n", options.writeInputFilename().value());
// This should be options.writeInputFilename().or_else(options.totomlFile.value())
// but that will require C++23
std::string filename =
options.writeInputFilename() ? options.writeInputFilename().value() : options.toTomlFile().value();
Messenger::print("Saving input file to '{}'...\n", filename);
bool result;
if (dissolve.worldPool().isMaster())
{
result = dissolve.saveInput(options.writeInputFilename().value());
if (result)
dissolve.worldPool().decideTrue();
if (options.writeInputFilename())
{
result = dissolve.saveInput(options.writeInputFilename().value());
if (result)
dissolve.worldPool().decideTrue();
else
dissolve.worldPool().decideFalse();
}
else
dissolve.worldPool().decideFalse();
{
auto toml = dissolve.serialise();
std::ofstream outfile(options.toTomlFile().value());
outfile << toml;
outfile.close();
result = true;
}
}
else
result = dissolve.worldPool().decision();
if (!result)
Messenger::error("Failed to save input file to '{}'.\n", options.writeInputFilename().value());
Messenger::error("Failed to save input file to '{}'.\n", filename);

// Reload the written file and continue?
if (options.writeInputAndReload())
Expand Down
4 changes: 4 additions & 0 deletions src/main/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ int CLIOptions::parse(const int args, char **argv, bool isGUI, bool isParallel)
app.add_option("-w,--write-input", writeInputFilename_,
"Write out the current simulation input to the file specified and then quit")
->group("Input Files");
app.add_option("--to-toml", toTomlFile_, "Convert input file into TOML format")->group("Output Files");
app.add_flag("--reload-after-write", writeInputAndReload_,
"Reload the input generated by --write-input and continue instead of quitting")
->group("Input Files");
Expand Down Expand Up @@ -108,3 +109,6 @@ bool CLIOptions::ignoreRestartFile() const { return ignoreRestartFile_; }

// Return whether to prevent writing of the restart file
bool CLIOptions::noRestartFile() const { return noRestartFile_; };

// Return output destination for TOML conversion
std::optional<std::string> CLIOptions::toTomlFile() const { return toTomlFile_; }
4 changes: 4 additions & 0 deletions src/main/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class CLIOptions
bool ignoreRestartFile_{false};
// Whether to prevent writing of the restart file
bool noRestartFile_{false};
// File for TOML conversion
std::optional<std::string> toTomlFile_;

public:
// Parse Result enum
Expand Down Expand Up @@ -66,4 +68,6 @@ class CLIOptions
bool ignoreRestartFile() const;
// Return whether to prevent writing of the restart file
bool noRestartFile() const;
// Return output destination for TOML conversion
std::optional<std::string> toTomlFile() const;
};

0 comments on commit 8cfdae1

Please sign in to comment.