Date: 2026-03-31T05:46:32+00:00

Actual source code: ex20.c

      1: static char help[] = "Demonstrates PetscOptionsPush()/PetscOptionsPop().\n\n";

      3: #include <petscsys.h>
      4: #include <petscoptions.h>
      5: int main(int argc, char **argv)
      6: {
      7:   PetscOptions opt1, opt2;
      8:   PetscInt     int1, int2;
      9:   PetscBool    flg1, flg2, flga, match;
     10:   char         str[16];

     12:   PetscFunctionBeginUser;
     13:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

     15:   PetscCall(PetscOptionsCreate(&opt1));
     16:   PetscCall(PetscOptionsInsertString(opt1, "-testa a"));
     17:   PetscCall(PetscOptionsPush(opt1));
     18:   PetscCall(PetscOptionsSetValue(NULL, "-test1", "1"));
     19:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test1", &int1, &flg1));
     20:   PetscCheck(flg1 && int1 == 1, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option test1 or it has the wrong value");
     21:   PetscCall(PetscOptionsGetString(NULL, NULL, "-testa", str, sizeof(str), &flga));
     22:   PetscCall(PetscStrcmp(str, "a", &match));
     23:   PetscCheck(flga && match, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option testa or it has the wrong value");
     24:   PetscCall(PetscOptionsCreate(&opt2));
     25:   PetscCall(PetscOptionsPush(opt2));
     26:   PetscCall(PetscOptionsSetValue(NULL, "-test2", "2"));
     27:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test2", &int2, &flg2));
     28:   PetscCheck(flg2 && int2 == 2, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option test2 or it has the wrong value");
     29:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test1", &int1, &flg1));
     30:   PetscCheck(!flg1, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Able to access test1 from a different options database");

     32:   PetscCall(PetscOptionsPop());
     33:   PetscCall(PetscOptionsPop());
     34:   PetscCall(PetscOptionsDestroy(&opt2));
     35:   PetscCall(PetscOptionsDestroy(&opt1));
     36:   PetscCall(PetscFinalize());
     37:   return 0;
     38: }

     40: /*TEST

     42:    test:
     43:      output_file: output/empty.out

     45: TEST*/