use IO_error in CLI

This commit is contained in:
Philip Eisenlohr 2023-07-11 17:15:03 -04:00
parent 32810cbe8b
commit d452a9ce3f
2 changed files with 54 additions and 63 deletions

View File

@ -74,7 +74,7 @@ subroutine CLI_init()
print'(a)', achar(27)//'[31m'
print'(1x,a,/)', 'debug version - debug version - debug version - debug version - debug version'
#else
print'(a)', achar(27)//'[94m'
print'(a)', achar(27)//'[1;94m'
#endif
print'(1x,a)', ' _/_/_/ _/_/ _/ _/ _/_/ _/_/_/ _/ _/ _/_/_/'
print'(1x,a)', ' _/ _/ _/ _/ _/_/ _/_/ _/ _/ _/ _/ _/ _/'
@ -158,48 +158,34 @@ subroutine CLI_init()
print'(1x,a,/)',' Prints this message and exits'
call quit(0) ! normal Termination
case ('-g', '--geom', '--geometry')
if (.not. hasArg) print'(/,1x,a)', asError('missing argument for --geom')
if (.not. hasArg) call IO_error(610)
geomArg = getArg(i+1)
case ('-l', '--load', '--loadcase')
if (.not. hasArg) print'(/,1x,a)', asError('missing argument for --load')
if (.not. hasArg) call IO_error(611)
loadArg = getArg(i+1)
case ('-m', '--material', '--materialconfig')
if (.not. hasArg) print'(/,1x,a)', asError('missing argument for --material')
if (.not. hasArg) call IO_error(612)
materialArg = getArg(i+1)
case ('-n', '--numerics', '--numericsconfig')
if (.not. hasArg) print'(/,1x,a)', asError('missing argument for --numerics')
if (.not. hasArg) call IO_error(613)
numericsArg = getArg(i+1)
case ('-j', '--job', '--jobname')
if (.not. hasArg) print'(/,1x,a)', asError('missing argument for --jobname')
if (.not. hasArg) call IO_error(614)
solverJobname = getArg(i+1)
case ('-w', '--wd', '--workingdir', '--workingdirectory')
if (.not. hasArg) print'(/,1x,a)', asError('missing argument for --workingdirectory')
if (.not. hasArg) call IO_error(615)
workingDirArg = getArg(i+1)
case ('-r', '--rs', '--restart')
if (.not. hasArg) print'(/,1x,a)', asError('missing argument for --restart')
if (.not. hasArg) call IO_error(616)
arg = getArg(i+1)
read(arg,*,iostat=stat) CLI_restartInc
if (CLI_restartInc < 0 .or. stat /= 0) then
print'(/,1x,a)', asError('could not parse restart increment: '//trim(arg))
call quit(1)
end if
if (CLI_restartInc < 0 .or. stat /= 0) call IO_error(617,ext_msg=arg)
end select
end do
if (.not. allocated(loadArg)) then
print'(/,1x,a)', asError('no load case specified (-h for help)')
call quit(1)
end if
if (.not. allocated(geomArg)) then
print'(/,1x,a)', asError('no geometry specified (-h for help)')
call quit(1)
end if
if (.not. allocated(materialArg)) then
print'(/,1x,a)', asError('no material configuration specified (-h for help)')
call quit(1)
end if
if (.not. allocated(geomArg)) call IO_error(620,ext_msg='-h for help')
if (.not. allocated(loadArg)) call IO_error(621,ext_msg='-h for help')
if (.not. allocated(materialArg)) call IO_error(622,ext_msg='-h for help')
call setWorkingDirectory(trim(workingDirArg))
CLI_geomFile = getPathRelCWD(geomArg,'geometry')
@ -211,8 +197,7 @@ subroutine CLI_init()
if (.not. allocated(solverJobname)) then
solverJobname = jobname(CLI_geomFile,CLI_loadFile,CLI_materialFile,CLI_numericsFile)
elseif (scan(solverJobname,'/') > 0) then
print'(/,1x,a)', asError('JOBNAME must not contain any slashes')
call quit(1)
call IO_error(630)
endif
commandLine = getArg(-1)
@ -272,9 +257,6 @@ subroutine setWorkingDirectory(workingDirectoryArg)
character(len=*), intent(in) :: workingDirectoryArg !< working directory argument
character(len=:), allocatable :: workingDirectory
logical :: error
external :: quit
absolutePath: if (workingDirectoryArg(1:1) == '/') then
workingDirectory = workingDirectoryArg
@ -284,11 +266,7 @@ subroutine setWorkingDirectory(workingDirectoryArg)
end if absolutePath
workingDirectory = trim(normpath(workingDirectory))
error = setCWD(trim(workingDirectory))
if (error) then
print'(1x,a)', asError('invalid Working directory: '//trim(workingDirectory))
call quit(1)
end if
if (setCWD(trim(workingDirectory))) call IO_error(640,ext_msg=workingDirectory)
end subroutine setWorkingDirectory
@ -344,7 +322,6 @@ function getPathRelCWD(path,fileType)
character(len=*), intent(in) :: fileType
logical :: file_exists
external :: quit
getPathRelCWD = trim(path)
@ -352,10 +329,7 @@ function getPathRelCWD(path,fileType)
getPathRelCWD = trim(relpath(getPathRelCWD,getCWD()))
inquire(file=getPathRelCWD, exist=file_exists)
if (.not. file_exists) then
print'(/,1x,a)', asError(fileType//' file does not exist: '//trim(getPathRelCWD))
call quit(1)
end if
if (.not. file_exists) call IO_error(100,ext_msg=fileType//' "'//trim(getPathRelCWD)//'"')
end function getPathRelCWD
@ -438,18 +412,5 @@ function relpath(path,start)
end function relpath
!--------------------------------------------------------------------------------------------------
!> @brief Prefix string with 'ERROR: ' and color it red.
!--------------------------------------------------------------------------------------------------
function asError(str)
character(len=*), intent(in) :: str
character(len=:), allocatable :: asError
asError = 'ERROR: '//achar(27)//'[31m'//trim(str)//achar(27)//'[0m'
end function asError
end module CLI

View File

@ -555,6 +555,34 @@ subroutine IO_error(error_ID,ext_msg,label1,ID1,label2,ID2)
! user errors
case (603)
msg = 'invalid data for table'
case (610)
msg = 'missing argument for --geom'
case (611)
msg = 'missing argument for --load'
case (612)
msg = 'missing argument for --material'
case (613)
msg = 'missing argument for --numerics'
case (614)
msg = 'missing argument for --jobname'
case (615)
msg = 'missing argument for --workingdirectory'
case (616)
msg = 'missing argument for --restart'
case (617)
msg = 'could not parse restart increment'
case (620)
msg = 'no geometry specified'
case (621)
msg = 'no load case specified'
case (622)
msg = 'no material configuration specified'
case (630)
msg = 'JOBNAME must not contain any slashes'
case (640)
msg = 'invalid working directory'
!------------------------------------------------------------------------------------------------
! errors related to YAML data
@ -704,6 +732,7 @@ subroutine panel(paneltype,ID,msg,ext_msg,label1,ID1,label2,ID2)
character(len=pSTRLEN) :: formatString
integer, parameter :: panelwidth = 69
character(len=:), allocatable :: msg_,ID_
character(len=*), parameter :: DIVIDER = repeat('─',panelwidth)
@ -712,16 +741,17 @@ subroutine panel(paneltype,ID,msg,ext_msg,label1,ID1,label2,ID2)
if ( present(label1) .and. .not. present(ID1)) error stop 'missing value for label 1'
if ( present(label2) .and. .not. present(ID2)) error stop 'missing value for label 2'
if (paneltype == 'error') msg_ = achar(27)//'[31m'//trim(msg)//achar(27)//'[0m'
if (paneltype == 'warning') msg_ = achar(27)//'[33m'//trim(msg)//achar(27)//'[0m'
ID_ = IO_intAsStr(ID)
!$OMP CRITICAL (write2out)
write(IO_STDERR,'(/,a)') ' ┌'//DIVIDER//'┐'
write(formatString,'(a,i2,a)') '(a,24x,a,',max(1,panelwidth-24-len_trim(paneltype)),'x,a)'
write(IO_STDERR,formatString) ' │',trim(paneltype), '│'
write(formatString,'(a,i2,a)') '(a,24x,i3,',max(1,panelwidth-24-3),'x,a)'
write(IO_STDERR,formatString) ' │',ID, '│'
write(formatString,'(a,i2,a)') '(a,24x,a,1x,i0,',max(1,panelwidth-24-len_trim(paneltype)-1-len_trim(ID_)),'x,a)'
write(IO_STDERR,formatString) ' │',trim(paneltype),ID, '│'
write(IO_STDERR,'(a)') ' ├'//DIVIDER//'┤'
write(formatString,'(a,i3.3,a,i3.3,a)') '(1x,a4,a',max(1,len_trim(msg)),',',&
write(formatString,'(a,i3.3,a,i3.3,a)') '(1x,a4,a',max(1,len_trim(msg_)),',',&
max(1,panelwidth+3-len_trim(msg)-4),'x,a)'
write(IO_STDERR,formatString) '│ ',trim(msg), '│'
write(IO_STDERR,formatString) '│ ',trim(msg_), '│'
if (present(ext_msg)) then
write(formatString,'(a,i3.3,a,i3.3,a)') '(1x,a4,a',max(1,len_trim(ext_msg)),',',&
max(1,panelwidth+3-len_trim(ext_msg)-4),'x,a)'