Skip to content

Commit

Permalink
LibCore: Show version and help before parsing positional arguments
Browse files Browse the repository at this point in the history
This allows `--version` and `--help` to work properly even if we do not
supply the required positional arguments to a command.
  • Loading branch information
SeekingBlues authored and awesomekling committed Aug 20, 2021
1 parent 6337eb5 commit 9721b7b
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions Userland/Libraries/LibCore/ArgsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,23 @@ bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_beha
}
}

// We're done processing options, now let's parse positional arguments.
// We're done processing options.
// Now let's show version or help if requested.

if (m_show_version) {
print_version(stdout);
if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
exit(0);
return false;
}
if (m_show_help) {
print_usage(stdout, argv[0]);
if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
exit(0);
return false;
}

// Now let's parse positional arguments.

int values_left = argc - optind;
Vector<int, 16> num_values_for_arg;
Expand Down Expand Up @@ -153,21 +169,6 @@ bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_beha
}
}

// We're done parsing! :)
// Now let's show version or help if requested.
if (m_show_version) {
print_version(stdout);
if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
exit(0);
return false;
}
if (m_show_help) {
print_usage(stdout, argv[0]);
if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
exit(0);
return false;
}

return true;
}

Expand Down

0 comments on commit 9721b7b

Please sign in to comment.