libsim Versione 7.2.0
optionparser_test.f90
1! Copyright (C) 2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
2! authors:
3! Davide Cesari <dcesari@arpa.emr.it>
4! Paolo Patruno <ppatruno@arpa.emr.it>
5
6! This program is free software; you can redistribute it and/or
7! modify it under the terms of the GNU General Public License as
8! published by the Free Software Foundation; either version 2 of
9! the License, or (at your option) any later version.
10
11! This program is distributed in the hope that it will be useful,
12! but WITHOUT ANY WARRANTY; without even the implied warranty of
13! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14! GNU General Public License for more details.
15
16! You should have received a copy of the GNU General Public License
17! along with this program. If not, see <http://www.gnu.org/licenses/>.
18PROGRAM optionparser_test
22!USE err_handling
23!USE char_utilities
25!USE log4fortran
26IMPLICIT NONE
27
28! option parsing
29TYPE(optionparser) :: opt
30INTEGER :: optind, optstatus
31INTEGER :: iargc
32! option variables
33CHARACTER(len=80) :: name
34INTEGER :: nx
35REAL :: xval, yval
36DOUBLE PRECISION :: dval
37TYPE(arrayof_integer) :: count_list
38TYPE(arrayof_real) :: value_list
39LOGICAL :: force, version
40INTEGER :: verbose
41! for checking
42CHARACTER(len=1024) :: exp_res
43TYPE(csv_record) :: csv_reader
44CHARACTER(len=80) :: ccheck
45INTEGER :: icheck, ier
46REAL :: rcheck
47DOUBLE PRECISION :: dcheck
48
49! define the option parser, for help2man usage_msg should start with
50! "Usage:"
51opt = optionparser_new(description_msg= &
52 'Test program for the optionparser class, &
53 &it just tests the arguments.', &
54 usage_msg='Usage: optionparser_test [options] expected_result')
55
56! add various options
57CALL optionparser_add(opt, 'n', 'name', name, 'defaultname', help= &
58 'short and long character option with default value')
59CALL optionparser_add(opt, '', 'nx', nx, 100, help= &
60 'long integer option with default value')
61CALL optionparser_add(opt, 'x', 'xval', xval, 712., help= &
62 'short and long real option with default value')
63yval = rmiss ! preset yval to recognize whether it has been set
64CALL optionparser_add(opt, '', 'yval', yval, help= &
65 'long real option without default value')
66CALL optionparser_add(opt, 'd', 'dval', dval, 489.0d0, help=&
67 'short and long double precision option with default value, &
68 &this should be a positive number')
69CALL optionparser_add(opt, '', 'count-list', count_list, (/1,2,3/), help= &
70 'integer array option, a comma-separated list of values can be provided, &
71 &the default is partially displayed, in this case it is 1,2,3')
72CALL optionparser_add(opt, '', 'value-list', value_list, help= &
73 'real array option, a comma-separated list of values can be provided, &
74 &the default in this case does not exist')
75CALL optionparser_add(opt, 'f', 'force', force, help= &
76 'logical option, it cannot have a default value because it is .FALSE. by design')
77CALL optionparser_add_count(opt, 'v', 'verbose', verbose, help= &
78 'count option without start value, it will be incremented at every appearence &
79 &of the option')
80verbose = 0
81
82! help options, useful for help2man
83CALL optionparser_add_help(opt, 'h', 'help', help='show an help message and exit')
84CALL optionparser_add(opt, ' ', 'version', version, help='show version and exit')
85
86! parse options and check for errors
87CALL optionparser_parse(opt, optind, optstatus)
88
89IF (optstatus == optionparser_help) THEN ! for help2man
90 CALL exit(0)
91ELSE IF (optstatus == optionparser_err) THEN
92 WRITE(*,'(A)')'Error in command-line arguments'
93 CALL exit(1)
94ENDIF
95IF (version) THEN ! for help2man
96 WRITE(*,'(A,1X,A)')'optionparser_test 1.0'
97 CALL exit(0)
98ENDIF
99IF (iargc() > optind) THEN
100 WRITE(*,'(A)')'Error, zero or one non-option argument required'
101 CALL exit(1)
102ENDIF
103
104! check for specific errors in the options
105!IF (dval <= 0.0D0) THEN
106! CALL optionparser_printhelp(opt)
107! CALL l4f_log(L4F_ERROR,'dval must be positive!')
108! CALL raise_fatal_error()
109!ENDIF
110
111! release all the option data structure, the variables set by options will remain
112CALL delete(opt)
113
114IF (iargc() < optind) THEN ! nothing to do
115 CALL exit(0)
116ENDIF
117! now format the options for comparing with expected result
118CALL getarg(optind, exp_res)
119CALL init(csv_reader, exp_res)
120
121CALL csv_record_getfield(csv_reader, ccheck, icheck, ier)
122IF (ier /= 0 .OR. trim(ccheck) /= trim(name)) THEN
123 WRITE(*,'(A)')'Error in command-line argument --name'
124 CALL exit(1)
125ENDIF
126
127CALL csv_record_getfield(csv_reader, icheck, ier)
128IF (ier /= 0 .OR. icheck /= nx) THEN
129 WRITE(*,'(A)')'Error in command-line argument --nx'
130 CALL exit(1)
131ENDIF
132
133CALL csv_record_getfield(csv_reader, rcheck, ier)
134IF (ier /= 0 .OR. rcheck /= xval) THEN
135 WRITE(*,'(A)')'Error in command-line argument --xval'
136 CALL exit(1)
137ENDIF
138
139CALL csv_record_getfield(csv_reader, rcheck, ier)
140IF (ier /= 0 .OR. rcheck /= yval) THEN
141 WRITE(*,'(A)')'Error in command-line argument --yval'
142 CALL exit(1)
143ENDIF
144
145CALL csv_record_getfield(csv_reader, dcheck, ier)
146IF (ier /= 0 .OR. dcheck /= dval) THEN
147 WRITE(*,'(A)')'Error in command-line argument --dval'
148 CALL exit(1)
149ENDIF
150
151! add list and logical check here
152
153CALL csv_record_getfield(csv_reader, icheck, ier)
154IF (ier /= 0 .OR. icheck /= verbose) THEN
155 WRITE(*,'(A)')'Error in command-line argument --verbose'
156 CALL exit(1)
157ENDIF
158
159CALL delete(csv_reader)
160CALL delete(count_list)
161CALL delete(value_list)
162
163END PROGRAM optionparser_test
Destructor for finalizing an array object.
Methods for successively obtaining the fields of a csv_record object.
Constructor for the class csv_record.
Add a new option of a specific type.
This module defines usefull general purpose function and subroutine.
Utilities for managing files.
Definitions of constants and functions for working with missing values.
Module for parsing command-line optons.

Generated with Doxygen.