From 067c9240c3ff5a4e1727ddba015c7b5b19fbae70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano-Tarumi?= Date: Sat, 1 Dec 2018 12:02:19 -0500 Subject: [PATCH] proces_options -> parse_options The function is not supposed to have side effects anymore. --- src/cli.cc | 7 +++--- src/opustags.cc | 4 ++-- src/opustags.h | 61 +++++++++++++++++++++++-------------------------- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/cli.cc b/src/cli.cc index ba201d1..e4e2972 100644 --- a/src/cli.cc +++ b/src/cli.cc @@ -52,8 +52,9 @@ static struct option getopt_options[] = { {NULL, 0, 0, 0} }; -ot::status ot::process_options(int argc, char** argv, ot::options& opt) +ot::status ot::parse_options(int argc, char** argv, ot::options& opt) { + opt = {}; if (argc == 1) return {st::bad_arguments, "No arguments specified. Use -h for help."}; @@ -99,7 +100,7 @@ ot::status ot::process_options(int argc, char** argv, ot::options& opt) break; case 'S': opt.set_all = true; - /* fall through */ + break; case 'D': opt.delete_all = true; break; @@ -266,7 +267,7 @@ static bool same_file(const std::string& path_in, const std::string& path_out) return false; } -ot::status ot::run(ot::options& opt) +ot::status ot::run(const ot::options& opt) { if (opt.print_help) { fputs(help_message, stdout); diff --git a/src/opustags.cc b/src/opustags.cc index 581326c..c00f3d4 100644 --- a/src/opustags.cc +++ b/src/opustags.cc @@ -14,9 +14,9 @@ */ int main(int argc, char** argv) { ot::options opt; - ot::status rc = process_options(argc, argv, opt); + ot::status rc = ot::parse_options(argc, argv, opt); if (rc == ot::st::ok) - rc = run(opt); + rc = ot::run(opt); if (rc != ot::st::ok) { if (!rc.message.empty()) diff --git a/src/opustags.h b/src/opustags.h index 5f92eaa..b11a7e5 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -350,9 +350,16 @@ void delete_comments(opus_tags& tags, const char* field_name); */ /** - * Structured representation of the arguments to opustags. + * Structured representation of the command-line arguments to opustags. It must faithfully represent + * what the user asked for, with as little interpretation or deduction as possible. */ struct options { + /** + * When true, opustags prints a detailed help and exits. All the other options are ignored. + * + * Option: --help + */ + bool print_help = false; /** * Path to the input file. It cannot be empty. The special "-" string means stdin. * @@ -373,6 +380,12 @@ struct options { * Option: --in-place */ const char* inplace = nullptr; + /** + * By default, opustags won't overwrite the output file if it already exists. + * + * Option: --overwrite + */ + bool overwrite = false; /** * List of field names to delete. `{"ARTIST"}` will delete *all* the comments `ARTIST=*`. It * is currently case-sensitive. When #delete_all is true, it becomes meaningless. @@ -386,6 +399,12 @@ struct options { * Option: --delete, --set */ std::vector to_delete; + /** + * Delete all the existing comments. + * + * Option: --delete-all + */ + bool delete_all = false; /** * List of comments to add, in the current system encoding. For exemple `TITLE=a b c`. They * must be valid. @@ -393,50 +412,26 @@ struct options { * Options: --add, --set, --set-all */ std::vector to_add; - /** - * Delete all the existing comments. - * - * Option: --delete-all - */ - bool delete_all = false; /** * Replace the previous comments by the ones supplied by the user. * - * Read a list of comments from stdin and populate #to_add. Implies #delete_all. Further - * comments may be added with the --add option. + * Read a list of comments from stdin and populate #to_add. Further comments may be added + * with the --add option. * * Option: --set-all */ bool set_all = false; - /** - * By default, opustags won't overwrite the output file if it already exists. - * - * Option: --overwrite - */ - bool overwrite = false; - /** - * When true, opustags prints a detailed help and exits. All the other options are ignored. - * - * Option: --help - */ - bool print_help = false; }; /** - * Process the command-line arguments. + * Parse the command-line arguments. Does not perform I/O related validations, but checks the + * consistency of its arguments. * - * This function does not perform I/O related validations, but checks the consistency of its - * arguments. + * On error, the state of the options structure is unspecified. * - * It returns one of : - * - #ot::st::ok, meaning the process may continue normally. - * This happens when all the user wants is see the help or usage. - * - #ot::st::bad_arguments, meaning the arguments were invalid and the process should exit with - * an error. - * - * Help messages are written on standard output, and error messages on standard error. + * \todo Return error messages in the status instead of writing them to stderr. */ -status process_options(int argc, char** argv, options& opt); +status parse_options(int argc, char** argv, options& opt); /** * Print all the comments, separated by line breaks. Since a comment may @@ -466,7 +461,7 @@ status process(ogg_reader& reader, ogg_writer* writer, const options &opt); * This is the main entry point to the opustags program, and pretty much the same as calling * opustags from the command-line. */ -status run(options& opt); +status run(const options& opt); /** \} */