calling opustags without arguments is now an error

Get rid of the exit_now status and simplify the help display code.
This commit is contained in:
Frédéric Mangano-Tarumi 2018-12-01 11:36:03 -05:00
parent 2b92ee0ce1
commit b60183c0ca
4 changed files with 34 additions and 46 deletions

View File

@ -17,15 +17,14 @@
#include <string.h>
#include <unistd.h>
static const char* version = PROJECT_NAME " version " PROJECT_VERSION "\n";
static const char help_message[] =
PROJECT_NAME " version " PROJECT_VERSION
R"raw(
static const char* usage = 1 + R"raw(
Usage: opustags --help
opustags [OPTIONS] FILE
opustags OPTIONS FILE -o FILE
)raw";
static const char* help = 1 + R"raw(
Options:
-h, --help print this help
-o, --output FILE set the output file
@ -36,6 +35,8 @@ Options:
-D, --delete-all delete all the previously existing comments
-s, --set FIELD=VALUE replace a comment (shorthand for --delete FIELD --add FIELD=VALUE)
-S, --set-all replace all the comments with the ones read from standard input
See the man page for extensive documentation.
)raw";
static struct option getopt_options[] = {
@ -53,11 +54,9 @@ static struct option getopt_options[] = {
ot::status ot::process_options(int argc, char** argv, ot::options& opt)
{
if (argc == 1) {
fputs(version, stdout);
fputs(usage, stdout);
return st::exit_now;
}
if (argc == 1)
return {st::bad_arguments, "No arguments specified. Use -h for help."};
int c;
while ((c = getopt_long(argc, argv, "ho:i::yd:a:s:DS", getopt_options, NULL)) != -1) {
switch (c) {
@ -109,13 +108,9 @@ ot::status ot::process_options(int argc, char** argv, ot::options& opt)
return st::bad_arguments;
}
}
if (opt.print_help) {
puts(version);
puts(usage);
puts(help);
puts("See the man page for extensive documentation.");
return st::exit_now;
}
if (opt.print_help)
return st::ok;
if (optind != argc - 1) {
fputs("exactly one input file must be specified\n", stderr);
return st::bad_arguments;
@ -276,6 +271,11 @@ static bool same_file(const std::string& path_in, const std::string& path_out)
ot::status ot::run(ot::options& opt)
{
if (opt.print_help) {
fputs(help_message, stdout);
return st::ok;
}
if (!opt.path_out.empty() && same_file(opt.path_in, opt.path_out))
return {ot::st::fatal_error, "Input and output files are the same"};

View File

@ -13,23 +13,16 @@
* Does practically nothing but call the cli module.
*/
int main(int argc, char** argv) {
ot::status rc;
ot::options opt;
rc = process_options(argc, argv, opt);
if (rc == ot::st::exit_now) {
ot::status rc = process_options(argc, argv, opt);
if (rc == ot::st::ok)
rc = run(opt);
if (rc != ot::st::ok) {
if (!rc.message.empty())
fprintf(stderr, "error: %s\n", rc.message.c_str());
return EXIT_FAILURE;
} else {
return EXIT_SUCCESS;
} else if (rc != ot::st::ok) {
if (!rc.message.empty())
fprintf(stderr, "error: %s\n", rc.message.c_str());
return EXIT_FAILURE;
}
rc = run(opt);
if (rc != ot::st::ok && rc != ot::st::exit_now) {
if (!rc.message.empty())
fprintf(stderr, "error: %s\n", rc.message.c_str());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -65,7 +65,6 @@ enum class st {
cut_comment_data,
/* CLI */
bad_arguments,
exit_now, /**< The program should terminate successfully. */
fatal_error,
};
@ -431,7 +430,6 @@ struct options {
*
* It returns one of :
* - #ot::st::ok, meaning the process may continue normally.
* - #ot::st::exit_now, meaning there is nothing to do and process should exit successfully.
* 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.

View File

@ -36,19 +36,16 @@ sub opustags {
# Tests related to the overall opustags executable, like the help message.
# No Opus file is manipulated here.
my $usage = opustags();
$usage->[0] =~ /^([^\n]*+)/;
is_deeply(opustags(), ['', <<EOF, 256], 'no options is a failure');
error: No arguments specified. Use -h for help.
EOF
my $help = opustags('--help');
$help->[0] =~ /^([^\n]*+)/;
my $version = $1;
like($version, qr/^opustags version (\d+\.\d+\.\d+)/, 'get the version string');
is_deeply($usage, [<<"EOF", "", 0], 'no options show the usage');
$version
Usage: opustags --help
opustags [OPTIONS] FILE
opustags OPTIONS FILE -o FILE
EOF
my $help = <<"EOF";
my $expected_help = <<"EOF";
$version
Usage: opustags --help
@ -69,8 +66,8 @@ Options:
See the man page for extensive documentation.
EOF
is_deeply(opustags('--help'), [$help, '', 0], '--help displays the help message');
is_deeply(opustags('-h'), [$help, '', 0], '-h displays the help message too');
is_deeply(opustags('--help'), [$expected_help, '', 0], '--help displays the help message');
is_deeply(opustags('-h'), [$expected_help, '', 0], '-h displays the help message too');
is_deeply(opustags('--derp'), ['', <<"EOF", 256], 'unrecognized option shows an error');
$opustags: unrecognized option '--derp'