File: options.c

package info (click to toggle)
p4est 2.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,536 kB
  • sloc: ansic: 87,528; makefile: 855; sh: 635; perl: 272; python: 226; awk: 40; javascript: 23
file content (145 lines) | stat: -rw-r--r-- 4,982 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
  This file is part of the SC Library.
  The SC Library provides support for parallel scientific applications.

  Copyright (C) 2010 The University of Texas System
  Additional copyright (C) 2011 individual authors

  The SC Library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  The SC Library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with the SC Library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include <sc_options.h>

/* quick and dirty -- please use callback's data argument instead */
static int          w;

static int
callback (sc_options_t * opt, const char *theoptarg, void *data)
{
  if (theoptarg == NULL) {
    SC_GLOBAL_INFOF ("%s without argument\n", (const char *) data);
  }
  else {
    SC_GLOBAL_INFOF ("%s with %s\n", (const char *) data, theoptarg);
  }
  ++w;

  return 0;
}

int
main (int argc, char **argv)
{
  int                 mpiret, retval;
  int                 rank;
  int                 first_arg;
  int                 i1, i2, si1;
  int                 kvint;
  size_t              z;
  double              d, sd;
  const char         *s1, *s2, *ss1, *ss2;
  const char         *cd = "Callback example";
  sc_keyvalue_t      *keyvalue;
  sc_options_t       *opt, *subopt;

  mpiret = sc_MPI_Init (&argc, &argv);
  SC_CHECK_MPI (mpiret);

  mpiret = sc_MPI_Comm_rank (sc_MPI_COMM_WORLD, &rank);
  SC_CHECK_MPI (mpiret);

  sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_DEFAULT);

  keyvalue = sc_keyvalue_new ();
  sc_keyvalue_set_int (keyvalue, "one", 1);
  sc_keyvalue_set_int (keyvalue, "two", 2);

  opt = sc_options_new (argv[0]);
  sc_options_add_switch (opt, 'w', "switch", &w, "Switch");
  sc_options_add_int (opt, 'i', "integer1", &i1, 0, "Integer 1");
  sc_options_add_double (opt, 'd', "double", &d, 0., "Double");
  sc_options_add_string (opt, 's', "string", &s1, NULL, "String 1");
  sc_options_add_callback (opt, 'c', "call1", 1, callback, (void *) cd,
                           "Callback 1");
  sc_options_add_callback (opt, 'C', "call2", 0, callback, (void *) cd,
                           "Callback 2");
  sc_options_add_string (opt, 't', NULL, &s2, NULL, "String 2");
  sc_options_add_inifile (opt, 'f', "inifile", ".ini file");
  sc_options_add_int (opt, '\0', "integer2", &i2, 7, "Integer 2");
  sc_options_add_size_t (opt, 'z', "sizet", &z, (size_t) 7000000000ULL,
                         "Size_t");

  subopt = sc_options_new (argv[0]);
  sc_options_add_int (subopt, 'i', "integer", &si1, 0, "Subset integer");
  sc_options_add_double (subopt, 'd', "double", &sd, 0., "Subset double");
  sc_options_add_string (subopt, 's', NULL, &ss1, NULL, "Subset string 1");
  sc_options_add_string (subopt, '\0', "string2", &ss2, NULL,
                         "Subset string 1");
  sc_options_add_keyvalue (subopt, 'n', "number", &kvint, "one",
                           keyvalue, "Subset keyvalue number");

  sc_options_add_suboptions (opt, subopt, "Subset");

  /* this is just to show off the load function */
  if (!sc_options_load (sc_package_id, SC_LP_INFO, opt,
                        "sc_options_preload.ini")) {
    SC_GLOBAL_INFO ("Preload successful\n");
  }
  else {
    SC_GLOBAL_INFO ("Preload not found or failed\n");
  }

  first_arg = sc_options_parse (sc_package_id, SC_LP_INFO, opt, argc, argv);
  if (first_arg < 0) {
    sc_options_print_usage (sc_package_id, SC_LP_INFO, opt,
                            "Usage for arg 1\nand for arg 2");
    SC_GLOBAL_INFO ("Option parsing failed\n");
  }
  else {
    SC_GLOBAL_INFO ("Option parsing successful\n");
    sc_options_print_summary (sc_package_id, SC_LP_INFO, opt);
    SC_GLOBAL_INFOF ("Keyvalue number is now %d\n", kvint);

    if (rank == 0) {
      retval = sc_options_save (sc_package_id, SC_LP_INFO, opt, "output.ini");
      if (retval) {
        SC_GLOBAL_INFO ("Option file output failed\n");
      }
      else {
        retval = sc_options_load_args (sc_package_id, SC_LP_INFO, opt,
                                       "output.ini");
        if (retval) {
          SC_GLOBAL_INFO ("Argument file input failed\n");
        }
        else {
          sc_options_print_summary (sc_package_id, SC_LP_INFO, opt);
          SC_GLOBAL_INFO ("Argument save load successful\n");
        }
      }
    }
  }

  sc_options_destroy (opt);
  sc_options_destroy (subopt);
  sc_keyvalue_destroy (keyvalue);

  sc_finalize ();

  mpiret = sc_MPI_Finalize ();
  SC_CHECK_MPI (mpiret);

  return 0;
}