module getopt_m implicit none integer, parameter :: MAX_ARGLEN=256 interface integer function iargc () end function iargc end interface contains subroutine getopt(options,nopt,opt,optarg) character(len=*), intent(in) :: options ! nopt - Number of words of the option string that have been processed integer, intent(inout) :: nopt integer, intent(out) :: opt character(len=*), intent(out) :: optarg ! Current word in argument string argv[optind] character(len=MAX_ARGLEN), save :: word character(len=1) :: ch integer, save :: ipos integer :: cpos, i integer, save :: letp = 0 ! Pointer to current position in the current word logical, save :: start = .true. ! Initialisation if ( start ) then ipos = 1 nopt = 0 start = .false. end if if ( letp == 0 ) then nopt = nopt+1 if ( nopt > iargc() ) then opt = -1 return end if call getarg(nopt,word) if ( len_trim(word) >= MAX_ARGLEN ) then print*, " Warning: option string too long, may be truncated " end if letp = 1 ! Check if the next word is an option if( word(letp:letp) /= '-') then opt = -1 return end if letp = letp+1 end if ch = word(letp:letp) letp = letp+1 if ( ch == ' ') then opt = -1 return end if cpos = index(options,ch) if ( ch == ':' .or. cpos == 0 ) then print*, 'get command line option' opt = ichar('?') return end if if ( options(cpos+1:cpos+1) == ':' ) then ! Argument expected if ( word(letp:letp) == ' ' ) then ! Get the next word of the option string nopt = nopt+1 if ( nopt > iargc() ) then print*, 'Option requries an argument --',ch opt = ichar('?') return end if call getarg(nopt,word) if ( len_trim(word) >= MAX_ARGLEN ) then print*, " Warning: option string too long, may be truncated " end if letp = 1 end if ! Copy the word into optarg, up to its length optarg = '' ! do i=1,len(optarg) do i=1,len_trim(word(letp:len(word))) optarg(i:i) = word(letp:letp) letp = letp+1 end do letp = 0 else ! Ordinary option without an argument if ( word(letp:letp) == ' ' ) then letp = 0 end if optarg = '' end if opt = ichar(ch) return end subroutine getopt subroutine getcline ( cline ) ! Get the complete program command line character(len=*), intent(out) :: cline integer :: iarg character(len=MAX_ARGLEN) :: arg cline = '' do iarg=0,iargc() call getarg(iarg,arg) ! Use >= here to allow for the extra space if ( len_trim(cline) + len_trim(arg) >= len(cline) ) then print*, "Error, increase length of command line variable" stop end if cline = cline(1:len_trim(cline)) // " " // trim(arg) end do ! The loop above adds a leading blank so adjustl cline = adjustl(cline) end subroutine getcline end module getopt_m