Skip to content

Commit

Permalink
simplify the command line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed May 9, 2020
1 parent 529c1aa commit 72737db
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 87 deletions.
138 changes: 53 additions & 85 deletions src/cli.cc
Expand Up @@ -36,33 +36,24 @@ void printError(const ReturnCode& rc) {
int main(int argc, char** argv) {
FlagList flags;

std::string flag_in;
flags_add_string(&flags, 'i', "in", &flag_in);
std::string flag_export;
flags_add_string(&flags, 'e', "export", &flag_export);

std::string flag_out;
flags_add_string(&flags, 'e', "out", &flag_out);

bool flag_stdin = false;
flags_add_switch(&flags, 0, "stdin", &flag_stdin);
std::string flag_format;
flags_add_string(&flags, 'f', "export-format", &flag_format);

bool flag_stdout = false;
flags_add_switch(&flags, 0, "stdout", &flag_stdout);
std::vector<std::string> flag_font_load;
flags_add_stringv(&flags, 0, "font-load", &flag_font_load);

std::string flag_format;
flags_add_string(&flags, 'f', "format", &flag_format);
bool flag_debug = true;
flags_add_switch(&flags, 'd', "debug", &flag_debug);

bool flag_help = false;
flags_add_switch(&flags, 'h', "help", &flag_help);

bool flag_version = false;
flags_add_switch(&flags, 'v', "version", &flag_version);

bool flag_debug = true;
flags_add_switch(&flags, 'd', "debug", &flag_debug);

std::vector<std::string> flag_font_load;
flags_add_stringv(&flags, 0, "font-load", &flag_font_load);

std::vector<std::string> args;
if (auto rc = flags_parse(flags, argc, argv, &args); !rc) {
std::cerr << "ERROR: " << rc.message << std::endl;
Expand All @@ -81,61 +72,35 @@ int main(int argc, char** argv) {
return 0;
}

if (flag_help) {
if (flag_help || args.size() != 1) {
std::cerr <<
"Usage: $ clip [OPTIONS]\n"
" --in <path> Path to the input file\n"
" --out <path> Path to the output file\n"
" --stdin Read the input file from stdin\n"
" --stdout Write the output file from sdout\n"
" --format <format> Output format. If no format is given, it is inferred from the\n"
" filename. Valid values: 'png', 'svg'\n"
" --font-defaults <bool> Enable or disable default font loading. Default is enabled.\n"
" Valid values: 'on' and 'off'\n"
" --font-load <path> Add a font file to the default font list\n"
" --debug Run in debug mode\n"
" --help Display this help text and exit\n"
" --version Display the version of this binary and exit\n"
"Usage: clip [OPTIONS] <file>\n"
"\n"
"Export:\n"
" -e, --export <path> Export the image\n"
" -f, --export-format <fmt> Export format. If no format is given, it is\n"
" inferred from the export filename.\n"
"\n"
"Fonts:\n"
" --font-load <path> Add a font file to the default font list\n"
"\n"
"Other:\n"
" -d, --debug Run in debug mode\n"
" -h, --help Display this help text and exit\n"
" -v, --version Display the version of this binary and exit\n"
"\n"
"Examples:\n"
" $ clip --in my_chart.clp --out my_chart.svg\n";
" $ clip -e my_chart.svg my_chart.clp\n";

return 0;
}

/* check if the input flags are valid */
if (flag_in.empty() && !flag_stdin) {
std::cerr << "Need an input file (--in)\n";
return 1;
}

if (!flag_in.empty() && flag_stdin) {
std::cerr
<< "Can't read from an input file (--in) and stdin (--stdin) at the "
<< "same time\n";

return 1;
}

if (flag_out.empty() && !flag_stdout) {
std::cerr << "Need an output file (--out)\n";
return 1;
}

if (!flag_out.empty() && flag_stdout) {
std::cerr
<< "Can't write to an output file (--out) and stdout (--stdout) at the "
<< "same time\n";

return 1;
}

/* figure out which output format the user wants */
auto output_format = clip::OutputFormat::SVG;
if (flag_format.empty()) {
if (StringUtil::endsWith(flag_out, ".svg"))
if (StringUtil::endsWith(flag_export, ".svg"))
output_format = OutputFormat::SVG;
if (StringUtil::endsWith(flag_out, ".png"))
if (StringUtil::endsWith(flag_export, ".png"))
output_format = OutputFormat::PNG;
} else if (flag_format == "svg") {
output_format = OutputFormat::SVG;
Expand All @@ -161,15 +126,16 @@ int main(int argc, char** argv) {

/* read the input file */
std::string input;
if (flag_stdin) {
const auto& input_path = args[0];
if (input_path == "-") {
std::istreambuf_iterator<char> begin(std::cin), end;
input = std::string(begin, end);
} else {
if (auto rc = read_file(flag_in, &input); !rc) {
if (auto rc = read_file(input_path, &input); !rc) {
fmt::print(
stderr,
"ERROR: unable to read input file ({}): {}\n",
flag_in,
input_path,
rc.message);

return EXIT_FAILURE;
Expand All @@ -182,29 +148,31 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

/* write the output file */
std::string output_buffer;
ReturnCode export_rc;
switch (output_format) {
case OutputFormat::SVG:
export_rc = export_svg(ctx.layer.get(), &output_buffer);
break;
//case OutputFormat::PNG:
// export_rc = page_export_png(page, drawlist, output_buffer);
// break;
}
/* export the context's layers if requested */
if (!flag_export.empty()) {
std::string output_buffer;
ReturnCode export_rc;
switch (output_format) {
case OutputFormat::SVG:
export_rc = export_svg(ctx.layer.get(), &output_buffer);
break;
//case OutputFormat::PNG:
// export_rc = page_export_png(page, drawlist, output_buffer);
// break;
}

if (!export_rc) {
error_print(export_rc, std::cerr);
return EXIT_FAILURE;
}
if (!export_rc) {
error_print(export_rc, std::cerr);
return EXIT_FAILURE;
}

if (flag_stdout) {
std::cout << output_buffer;
} else {
FileUtil::write(
flag_out,
Buffer(output_buffer.data(), output_buffer.size()));
if (flag_export == "-") {
std::cout << output_buffer;
} else {
FileUtil::write(
flag_export,
Buffer(output_buffer.data(), output_buffer.size()));
}
}

return EXIT_SUCCESS;
Expand Down
4 changes: 2 additions & 2 deletions test/test_runner.sh
Expand Up @@ -74,8 +74,8 @@ run_test() {
result=""
if (cd ${source_path} && "${proc_path}" \
--font-load "test/testdata/fonts/LiberationSans-Regular.ttf" \
--in "${infile}" \
--out "${outfile}" \
--export "${outfile}" \
"${infile}" \
2> "${logfile}"); then
result="ok"
else
Expand Down

0 comments on commit 72737db

Please sign in to comment.