libsim  Versione7.2.3
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/>.
18 PROGRAM optionparser_test
22 !USE err_handling
23 !USE char_utilities
25 !USE log4fortran
26 IMPLICIT NONE
27 
28 ! option parsing
29 TYPE(optionparser) :: opt
30 INTEGER :: optind, optstatus
31 INTEGER :: iargc
32 ! option variables
33 CHARACTER(len=80) :: name
34 INTEGER :: nx
35 REAL :: xval, yval
36 DOUBLE PRECISION :: dval
37 TYPE(arrayof_integer) :: count_list
38 TYPE(arrayof_real) :: value_list
39 LOGICAL :: force, version
40 INTEGER :: verbose
41 ! for checking
42 CHARACTER(len=1024) :: exp_res
43 TYPE(csv_record) :: csv_reader
44 CHARACTER(len=80) :: ccheck
45 INTEGER :: icheck, ier
46 REAL :: rcheck
47 DOUBLE PRECISION :: dcheck
48 
49 ! define the option parser, for help2man usage_msg should start with
50 ! "Usage:"
51 opt = 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
57 CALL optionparser_add(opt, 'n', 'name', name, 'defaultname', help= &
58  'short and long character option with default value')
59 CALL optionparser_add(opt, '', 'nx', nx, 100, help= &
60  'long integer option with default value')
61 CALL optionparser_add(opt, 'x', 'xval', xval, 712., help= &
62  'short and long real option with default value')
63 yval = rmiss ! preset yval to recognize whether it has been set
64 CALL optionparser_add(opt, '', 'yval', yval, help= &
65  'long real option without default value')
66 CALL 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')
69 CALL 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')
72 CALL 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')
75 CALL optionparser_add(opt, 'f', 'force', force, help= &
76  'logical option, it cannot have a default value because it is .FALSE. by design')
77 CALL 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')
80 verbose = 0
81 
82 ! help options, useful for help2man
83 CALL optionparser_add_help(opt, 'h', 'help', help='show an help message and exit')
84 CALL optionparser_add(opt, ' ', 'version', version, help='show version and exit')
85 
86 ! parse options and check for errors
87 CALL optionparser_parse(opt, optind, optstatus)
88 
89 IF (optstatus == optionparser_help) THEN ! for help2man
90  CALL exit(0)
91 ELSE IF (optstatus == optionparser_err) THEN
92  WRITE(*,'(A)')'Error in command-line arguments'
93  CALL exit(1)
94 ENDIF
95 IF (version) THEN ! for help2man
96  WRITE(*,'(A,1X,A)')'optionparser_test 1.0'
97  CALL exit(0)
98 ENDIF
99 IF (iargc() > optind) THEN
100  WRITE(*,'(A)')'Error, zero or one non-option argument required'
101  CALL exit(1)
102 ENDIF
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
112 CALL delete(opt)
113 
114 IF (iargc() < optind) THEN ! nothing to do
115  CALL exit(0)
116 ENDIF
117 ! now format the options for comparing with expected result
118 CALL getarg(optind, exp_res)
119 CALL init(csv_reader, exp_res)
120 
121 CALL csv_record_getfield(csv_reader, ccheck, icheck, ier)
122 IF (ier /= 0 .OR. trim(ccheck) /= trim(name)) THEN
123  WRITE(*,'(A)')'Error in command-line argument --name'
124  CALL exit(1)
125 ENDIF
126 
127 CALL csv_record_getfield(csv_reader, icheck, ier)
128 IF (ier /= 0 .OR. icheck /= nx) THEN
129  WRITE(*,'(A)')'Error in command-line argument --nx'
130  CALL exit(1)
131 ENDIF
132 
133 CALL csv_record_getfield(csv_reader, rcheck, ier)
134 IF (ier /= 0 .OR. rcheck /= xval) THEN
135  WRITE(*,'(A)')'Error in command-line argument --xval'
136  CALL exit(1)
137 ENDIF
138 
139 CALL csv_record_getfield(csv_reader, rcheck, ier)
140 IF (ier /= 0 .OR. rcheck /= yval) THEN
141  WRITE(*,'(A)')'Error in command-line argument --yval'
142  CALL exit(1)
143 ENDIF
144 
145 CALL csv_record_getfield(csv_reader, dcheck, ier)
146 IF (ier /= 0 .OR. dcheck /= dval) THEN
147  WRITE(*,'(A)')'Error in command-line argument --dval'
148  CALL exit(1)
149 ENDIF
150 
151 ! add list and logical check here
152 
153 CALL csv_record_getfield(csv_reader, icheck, ier)
154 IF (ier /= 0 .OR. icheck /= verbose) THEN
155  WRITE(*,'(A)')'Error in command-line argument --verbose'
156  CALL exit(1)
157 ENDIF
158 
159 CALL delete(csv_reader)
160 CALL delete(count_list)
161 CALL delete(value_list)
162 
163 END PROGRAM optionparser_test
Destructor for the optionparser class.
Constructor for the class csv_record.
Module for parsing command-line optons.
Utilities for managing files.
Methods for successively obtaining the fields of a csv_record object.
This module defines usefull general purpose function and subroutine.
Definitions of constants and functions for working with missing values.
Add a new option of a specific type.

Generated with Doxygen.