xref: /petsc/src/sys/tutorials/ex20.c (revision f97672e55eacc8688507b9471cd7ec2664d7f203)
1 
2 static char help[] = "Demonstrates PetscOptionsPush()/PetscOptionsPop().\n\n";
3 
4 #include <petscsys.h>
5 #include <petscoptions.h>
6 int main(int argc,char **argv)
7 {
8   PetscOptions   opt1,opt2;
9   PetscInt       int1,int2;
10   PetscBool      flg1,flg2,flga,match;
11   char           str[16];
12 
13   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
14 
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");
31 
32   PetscCall(PetscOptionsPop());
33   PetscCall(PetscOptionsPop());
34   PetscCall(PetscOptionsDestroy(&opt2));
35   PetscCall(PetscOptionsDestroy(&opt1));
36   PetscCall(PetscFinalize());
37   return 0;
38 }
39 
40 /*TEST
41 
42    test:
43 
44 TEST*/
45