Skip to content

Commit

Permalink
Error for unknown subcommands overrides unknown option
Browse files Browse the repository at this point in the history
If one does `rpm-ostree foo --bar`, one would expect to see
`Unknown subcommand foo`, not `Unknown option --bar`.

Closes: #267

Closes: #270
Approved by: jlebon
  • Loading branch information
cgwalters authored and cgwalters-bot committed Apr 27, 2016
1 parent 41e3d12 commit d935767
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 78 deletions.
24 changes: 11 additions & 13 deletions src/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,18 @@ main (int argc,
context = option_context_new_with_commands ();

/* This will not return for some options (e.g. --version). */
if (rpmostree_option_context_parse (context, NULL, &argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
NULL, NULL, &local_error))
(void) rpmostree_option_context_parse (context, NULL, &argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
NULL, NULL, NULL);
if (command_name == NULL)
{
if (command_name == NULL)
{
local_error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED,
"No command specified");
}
else
{
local_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown command '%s'", command_name);
}
local_error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED,
"No command specified");
}
else
{
local_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown command '%s'", command_name);
}

help = g_option_context_get_help (context, FALSE, NULL);
Expand Down
30 changes: 14 additions & 16 deletions src/app/rpmostree-builtin-compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,21 @@ rpmostree_builtin_compose (int argc, char **argv, GCancellable *cancellable, GEr
context = compose_option_context_new_with_commands ();

/* This will not return for some options (e.g. --version). */
if (rpmostree_option_context_parse (context, NULL,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
cancellable,
NULL,
error))
(void) rpmostree_option_context_parse (context, NULL,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
cancellable,
NULL,
NULL);
if (subcommand_name == NULL)
{
if (subcommand_name == NULL)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No command specified");
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown compose command '%s'", subcommand_name);
}
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No command specified");
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown compose command '%s'", subcommand_name);
}

exit_status = EXIT_FAILURE;
Expand Down
30 changes: 14 additions & 16 deletions src/app/rpmostree-builtin-container.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,21 @@ rpmostree_builtin_container (int argc, char **argv, GCancellable *cancellable, G
context = container_option_context_new_with_commands ();

/* This will not return for some options (e.g. --version). */
if (rpmostree_option_context_parse (context, NULL,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
cancellable,
NULL,
error))
(void) rpmostree_option_context_parse (context, NULL,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
cancellable,
NULL,
NULL);
if (subcommand_name == NULL)
{
if (subcommand_name == NULL)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No \"container\" subcommand specified");
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown \"container\" subcommand '%s'", subcommand_name);
}
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No \"container\" subcommand specified");
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown \"container\" subcommand '%s'", subcommand_name);
}

exit_status = EXIT_FAILURE;
Expand Down
30 changes: 14 additions & 16 deletions src/app/rpmostree-builtin-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,21 @@ rpmostree_builtin_db (int argc, char **argv, GCancellable *cancellable, GError *
context = rpm_option_context_new_with_commands ();

/* This will not return for some options (e.g. --version). */
if (rpmostree_option_context_parse (context, NULL,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
cancellable,
NULL,
error))
(void) rpmostree_option_context_parse (context, NULL,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
cancellable,
NULL,
NULL);
if (subcommand_name == NULL)
{
if (subcommand_name == NULL)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No \"db\" subcommand specified");
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown \"db\" subcommand '%s'", subcommand_name);
}
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No \"db\" subcommand specified");
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown \"db\" subcommand '%s'", subcommand_name);
}

exit_status = EXIT_FAILURE;
Expand Down
30 changes: 14 additions & 16 deletions src/app/rpmostree-builtin-internals.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,21 @@ rpmostree_builtin_internals (int argc, char **argv, GCancellable *cancellable, G
context = internals_option_context_new_with_commands ();

/* This will not return for some options (e.g. --version). */
if (rpmostree_option_context_parse (context, NULL,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
cancellable,
NULL,
error))
(void) rpmostree_option_context_parse (context, NULL,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
cancellable,
NULL,
NULL);
if (subcommand_name == NULL)
{
if (subcommand_name == NULL)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No \"internals\" subcommand specified");
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown \"internals\" subcommand '%s'", subcommand_name);
}
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No \"internals\" subcommand specified");
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unknown \"internals\" subcommand '%s'", subcommand_name);
}

exit_status = EXIT_FAILURE;
Expand Down
7 changes: 6 additions & 1 deletion tests/test-basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ assert_file_has_content OUTPUT-status.txt '1\.0\.9'
# Ensure it returns an error when passing a wrong option.
rpm-ostree --help | awk '/^$/ {in_commands=0} {if(in_commands==1){print $0}} /^Builtin Commands:/ {in_commands=1}' > commands
while read command; do
if rpm-ostree $command --n0t-3xisting-0ption >/dev/null 2>&1; then
if rpm-ostree $command --n0t-3xisting-0ption &>/dev/null; then
assert_not_reached "command $command --n0t-3xisting-0ption was successful"
fi
done < commands

if rpm-ostree nosuchcommand --nosuchoption 2>err.txt; then
assert_not_reached "Expected an error for nosuchcommand"
fi
assert_file_has_content err.txt 'Unknown.*command'

0 comments on commit d935767

Please sign in to comment.