Skip to content

Add redirect for stdout to a file #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <cstdint>
#include <cstdio>
#if defined(WIN32)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
Expand All @@ -40,6 +41,7 @@ void PrintUsage() {
<< std::endl
<< "Options:" << std::endl
<< " --help Display this message" << std::endl
<< " -o,--output Print output to file. [default: stdout]" << std::endl
<< " -y,--yaml Format output as YAML. [default: disabled]" << std::endl
<< " -v VERBOSITY Specify output verbosity (YAML output "
"only):"
Expand Down Expand Up @@ -80,6 +82,7 @@ void PrintUsage() {
int main(int argn, char** argv) {
ArgParser arg_parser;
arg_parser.AddFlag("h", "help", "");
arg_parser.AddOptionString("o", "output", "");
arg_parser.AddFlag("y", "yaml", "");
arg_parser.AddOptionInt("v", "verbosity", "", 0);
arg_parser.AddFlag("e", "entrypoint", "");
Expand All @@ -97,6 +100,10 @@ int main(int argn, char** argv) {
return EXIT_SUCCESS;
}

std::string output_file;
arg_parser.GetString("o", "output", &output_file);
FILE* output_fp = output_file.empty() ? NULL : freopen(output_file.c_str(), "w", stdout);

bool output_as_yaml = arg_parser.GetFlag("y", "yaml");

int yaml_verbosity = 0;
Expand Down Expand Up @@ -189,5 +196,8 @@ int main(int argn, char** argv) {
}
}

if (output_fp) {
fclose(output_fp);
}
return EXIT_SUCCESS;
}
Loading