Skip to content

Conversation

@Karan-Palan
Copy link
Contributor

What kind of change does this PR introduce?

  • Fix: Make unknown commands other than "help" return exit code 1

Issue Number:

Screenshots/videos:

image

src/main.cc Outdated
)EOF"};

auto jsonschema_main(const std::string &program, const std::string &command,
auto jsonschema_main([[maybe_unused]] const std::string &program,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this annotation. program is always used in the help handler!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright!

<< " <command> [arguments...]\n";
std::cout << USAGE_DETAILS;
return EXIT_SUCCESS;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add one test covering this case and the output? See how we test help for an example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

@Karan-Palan Karan-Palan force-pushed the fix/unknown-commands-exitcode branch from a7e8134 to 4f8e8a2 Compare February 13, 2025 18:14
@Karan-Palan
Copy link
Contributor Author

Did the changes. PTAL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you assert the actual error output the CLI prints?

clean() { rm -rf "$TMP"; }
trap clean EXIT

"$1" unknown_command 1> "$TMP/stdout" 2> "$TMP/stderr" || exit_code=$?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please see how we do it in other files for consistency. i.e. we use uppercase for variables: EXIT_CODE

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also see the && EXIT_CODE="$?" || EXIT_CODE="$?" pattern we use to avoid the -z cases you have below


"$1" unknown_command 1> "$TMP/stdout" 2> "$TMP/stderr" || exit_code=$?

if [ -z "${exit_code:-}" ] || [ "$exit_code" -eq 0 ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at other files, we put then in its own line. In general, please try to copy the current style as much as possible, otherwise we end up with files that have their own style

src/main.cc Outdated
return EXIT_SUCCESS;
} else {
std::cerr << "error: Unknown command '" << command << "'.\n";
std::cerr << "Use '--help' for usage information.\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::cerr << "Use '--help' for usage information.\n";
std::cerr << "Use --help for usage information\n";

For consistency

src/main.cc Outdated
std::cout << USAGE_DETAILS;
return EXIT_SUCCESS;
} else {
std::cerr << "error: Unknown command '" << command << "'.\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::cerr << "error: Unknown command '" << command << "'.\n";
std::cerr << "error: Unknown command '" << command << "'\n";

For consistency with all other error messages

Signed-off-by: karan-palan <[email protected]>
@Karan-Palan
Copy link
Contributor Author

Done the required changes, ptal

@jviotti jviotti merged commit 9df8bad into sourcemeta:main Feb 27, 2025
7 checks passed
@jviotti
Copy link
Member

jviotti commented Feb 27, 2025

Awesome, thanks a lot!

@Karan-Palan Karan-Palan deleted the fix/unknown-commands-exitcode branch February 28, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make unknown commands other than "help" return exit code 1

2 participants