Skip to content

Commit

Permalink
Merge pull request #2694 from tclose/quit-after-usage
Browse files Browse the repository at this point in the history
Quit after command-line parses env var
  • Loading branch information
Lestropie authored Aug 25, 2023
2 parents 5b0ec65 + 93d17e2 commit d7fedca
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ testing/src/project_version.h
.__tmp.log
.check_syntax.tmp
.check_syntax2.tmp
*.venv
__pycache__
.DS_Store
13 changes: 13 additions & 0 deletions core/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "app.h"
#include "exec_version.h"
#include "mrtrix.h"
#ifdef MRTRIX_PROJECT
namespace MR {
namespace App {
Expand Down Expand Up @@ -92,6 +93,18 @@ int main(int cmdline_argc, char **cmdline_argv) {
::MR::GUI::App app(cmdline_argc, cmdline_argv);
#endif
::MR::App::parse();

// ENVVAR name: MRTRIX_CLI_PARSE_ONLY
// ENVVAR Set the command to parse the provided inputs and then quit
// ENVVAR if it is set. This can be used in the CI of wrapping code,
// ENVVAR such as the automatically generated Pydra interfaces.
// ENVVAR Note that it will have no effect for R interfaces
char *parse_only = std::getenv("MRTRIX_CLI_PARSE_ONLY");
if (parse_only && ::MR::to<bool>(parse_only)) {
CONSOLE("Quitting after parsing command-line arguments successfully due to environment variable "
"'MRTRIX_CLI_PARSE_ONLY'");
return 0;
}
run();
} catch (::MR::Exception &E) {
E.display();
Expand Down
10 changes: 5 additions & 5 deletions core/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ extern void (*report_to_user_func)(const std::string &msg, int type);

#define CONSOLE(msg) \
if (MR::App::log_level >= 1) \
report_to_user_func(msg, -1)
::MR::report_to_user_func(msg, -1)
#define FAIL(msg) \
if (MR::App::log_level >= 0) \
report_to_user_func(msg, 0)
::MR::report_to_user_func(msg, 0)
#define WARN(msg) \
if (MR::App::log_level >= 1) \
report_to_user_func(msg, 1)
::MR::report_to_user_func(msg, 1)
#define INFO(msg) \
if (MR::App::log_level >= 2) \
report_to_user_func(msg, 2)
::MR::report_to_user_func(msg, 2)
#define DEBUG(msg) \
if (MR::App::log_level >= 3) \
report_to_user_func(msg, 3)
::MR::report_to_user_func(msg, 3)

class Exception {
public:
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/environment_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ List of MRtrix3 environment variables
when reading DICOM data, match the StudyName entry against
the string provided

.. envvar:: MRTRIX_CLI_PARSE_ONLY

Set the command to parse the provided inputs and then quit
if it is set. This can be used in the CI of wrapping code,
such as the automatically generated Pydra interfaces.
Note that it will have no effect for R interfaces

.. envvar:: MRTRIX_CONFIGFILE

This can be used to set the location of the system-wide
Expand Down
15 changes: 15 additions & 0 deletions lib/mrtrix3/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ def _execute(module): #pylint: disable=unused-variable
CMDLINE.print_citation_warning()

return_code = 0

cli_parse_only = os.getenv('MRTRIX_CLI_PARSE_ONLY')
if cli_parse_only:
try:
if cli_parse_only.lower() in ['yes', 'true'] or int(cli_parse_only):
console(
'Quitting after parsing command-line arguments successfully due to '
'environment variable "MRTRIX_CLI_PARSE_ONLY"'
)
sys.exit(return_code)
except ValueError:
warn('Potentially corrupt environment variable "MRTRIX_CLI_PARSE_ONLY" '
'= "' + cli_parse_only + '"; ignoring')
sys.exit(return_code)

try:
module.execute()
except (run.MRtrixCmdError, run.MRtrixFnError) as exception:
Expand Down

0 comments on commit d7fedca

Please sign in to comment.