Actual source code: ex55.c

  1: static char help[] = "Tests options database monitoring and precedence.\n\n";

  3: #include <petscsys.h>
  4: #include <petscviewer.h>

  6: PetscErrorCode PetscOptionsMonitorCustom(const char name[], const char value[], PetscOptionSource source, void *ctx)
  7: {
  8:   PetscViewer viewer = (PetscViewer)ctx;

 10:   PetscFunctionBegin;
 11:   if (!value) {
 12:     PetscCall(PetscViewerASCIIPrintf(viewer, "* Removing option: %s\n", name));
 13:   } else if (!value[0]) {
 14:     PetscCall(PetscViewerASCIIPrintf(viewer, "* Setting option: %s (no value)\n", name));
 15:   } else {
 16:     PetscCall(PetscViewerASCIIPrintf(viewer, "* Setting option: %s = %s\n", name, value));
 17:   }
 18:   PetscFunctionReturn(PETSC_SUCCESS);
 19: }

 21: int main(int argc, char **argv)
 22: {
 23:   PetscViewer       viewer = NULL;
 24:   PetscViewerFormat format;

 26:   PetscFunctionBeginUser;
 27:   PetscCall(PetscInitialize(&argc, &argv, "ex55options", help));
 28:   PetscCall(PetscOptionsInsertString(NULL, "-option1 1 -option2 -option3 value3"));
 29:   PetscCall(PetscOptionsGetViewer(PETSC_COMM_WORLD, NULL, NULL, "-options_monitor_viewer", &viewer, &format, NULL));
 30:   if (viewer) {
 31:     PetscCall(PetscViewerPushFormat(viewer, format));
 32:     PetscCall(PetscOptionsMonitorSet(PetscOptionsMonitorCustom, viewer, NULL));
 33:     PetscCall(PetscViewerPopFormat(viewer));
 34:     PetscCall(PetscOptionsRestoreViewer(&viewer));
 35:   }
 36:   PetscCall(PetscOptionsInsertString(NULL, "-option4 value4 -option5"));
 37:   PetscCall(PetscFinalize());
 38:   return 0;
 39: }

 41: /*TEST

 43:    testset:
 44:       localrunfiles: ex55options .petscrc petscrc
 45:       args: -options_left 0 -options_view -options_monitor_viewer ascii
 46:       args: -skip_petscrc {{0 1}separate output} -options_monitor_cancel {{0 1}separate output}
 47:       test:
 48:         suffix: 1
 49:       test:
 50:         suffix: 2
 51:         args: -options_monitor
 52:       test:
 53:         suffix: 3
 54:         args: -options_monitor -option_cmd_1 option_cmd_1_val -option_cmd_2
 55:    test:
 56:       # test effect of -skip_petscrc in ex55options file
 57:       suffix: 4
 58:       localrunfiles: ex55options .petscrc petscrc
 59:       args: -options_left 0 -options_view -options_monitor
 60:    testset:
 61:       # test -help / -help intro / -version from command line
 62:       localrunfiles: ex55options .petscrc petscrc
 63:       filter: grep -E -e "(version|help|^See)"
 64:       args: -options_left -options_view -options_monitor
 65:       test:
 66:         suffix: 5a
 67:         args: -help
 68:       test:
 69:         suffix: 5b
 70:         args: -help intro
 71:       test:
 72:         suffix: 5c
 73:         args: -version
 74:    testset:
 75:       # test -help / -help intro / -version from file
 76:       localrunfiles: ex55options rc_help rc_help_intro rc_version
 77:       filter: grep -E -e "(version|help|^See)"
 78:       args: -skip_petscrc
 79:       args: -options_left -options_view -options_monitor
 80:       test:
 81:         suffix: 6a
 82:         args: -options_file rc_help
 83:       test:
 84:         suffix: 6b
 85:         args: -options_file rc_help_intro
 86:       test:
 87:         suffix: 6c
 88:         args: -options_file rc_version

 90:    test:
 91:      localrunfiles: ex55options .petscrc petscrc
 92:      suffix: 7
 93:      args: -options_monitor -options_left 0

 95: TEST*/