#!/bin/sh ########################################### # Aug 29, 2009, John S. Urban # Tested with: # gfortran: GNU Fortran (GCC) 4.3.2 20080827 (beta) # g95: G95 (GCC 4.0.3 (g95 0.92!) May 7 2009) ########################################### #------------------------------------------ # CREATE FORTRAN SOURCE: #------------------------------------------ cat >cmdline_tiny.f90 <<\EOF program cmdline !=============================================================================== ! fortran 2003: namelist read from an internal file and ! reading the command line arguments combined ! to read simple command-line options ! John S. Urban -- Aug 29, 2009 !=============================================================================== CHARACTER(LEN=1000):: string(3) ! storage for command line arguments character(LEN=255) :: message ! storage for I/O messages ! ----------------------------------------------------- ! make a namelist group with various data types TYPE person INTEGER p_age CHARACTER(20) p_name REAL p_height END TYPE person INTEGER :: i=1,j=2,k=3 ! simple integers INTEGER :: s=1,t=2,r=3 ! simple reals INTEGER :: l(3)=[10,20,30] ! numeric array LOGICAL :: quit=.false. ! logical CHARACTER(len=5) :: c='Bacon' ! character variable COMPLEX :: x=(-1.0,-1.0) ! complex TYPE(person) smith ! user-defined type namelist /cmd/ i,j,c,k,l,smith,quit,s,t,r,x ! ----------------------------------------------------- smith=person(20,'john smith',5.90) ! ----------------------------------------------------- WRITE(*,'(80(''=''))') CALL GET_COMMAND_ARGUMENTS(string(2),icmd_len,ier) if(string(2)(:icmd_len) .eq. '?' )then WRITE(*,nml=cmd) write(*,*)'Enter values:(&CMD name=value name=value .../)' read(*,nml=cmd) else string(1)=" &cmd " string(3)=" /" WRITE(*,*)'command line as received by program:' WRITE(*,*)string(2)(:LEN_TRIM(string(2))) WRITE(*,'(80(''-''))') WRITE(*,*)'resulting namelist group values' !!!!READ(string,NML=cmd,IOSTAT=ios,iomsg=message) READ(string,NML=cmd,IOSTAT=ios) IF(ios.NE.0)THEN WRITE(*,*)'e-r-r-o-r: processing command line, iostat=',ios !!!! WRITE(*,*)'---------: ',message(:len_trim(message)) ENDIF endif WRITE(*,nml=cmd) ! ----------------------------------------------------- END PROGRAM cmdline !=============================================================================== subroutine get_command_arguments(string,istring_len,istatus) ! @(#)get_command_arguments: return all command arguments as a string character(len=*),intent(out) :: string ! string of all arguments integer,intent(out) :: istring_len ! last character position set integer,intent(out) :: istatus ! status (non-zero means error) integer :: ilength ! length of individual arguments integer :: i ! loop count integer :: icount ! count of number of arguments available character(len=255) :: value ! store individual arguments one at a time string="" ! initialize returned output string istring_len=0 ! initialize returned output string length istatus=0 ! initialize returned error code icount=command_argument_count() ! intrinsic gets number of arguments if(icount>0)then ! if there are arguments load them into string ! start with first argument call get_command_argument(1,string,istring_len,istatus) if(istatus == 0)then do i=2,icount ! append any additional arguments to first call get_command_argument(i,value,ilength,istatus) if(istatus /= 0)then exit ! stop on error endif string=string(:istring_len)//" "//value(:ilength) istring_len=istring_len+ilength+1 enddo endif ! keep track of length and so do not need to use len_trim istring_len=len_trim(string) endif return end subroutine get_command_arguments !=============================================================================== EOF ########################################### TESTIT(){ # TEST different ways of putting in input echo DEFAULT VALUES: ./cmdline_tiny ./cmdline_tiny I=1111 j=22222 C="'Eggs'" quit=t SMITH%P_HEIGHT=5.10 ./cmdline_tiny L=1111,2222,3333 L=,,4444 'L(2)=5555' ! array syntax tests } ########################################### ISSUES(){ # this seems to make gfortran fail, but not g95: ./cmdline_tiny ! Just a comment and nothing else # OOPS: quotes do not make it in most shells -- but not handled as expected ./cmdline_tiny I=1111 j=22222 C='A and B',quit=t } ########################################### #------------------------------------------ # COMPILE, LOAD, GO #------------------------------------------ ( date uname -a exec 2>&1 set -x ########################################### rm -f cmdline_tiny gfortran -dumpversion gfortran cmdline_tiny.f90 -o cmdline_tiny TESTIT ########################################### rm -f cmdline_tiny g95 -dumpversion g95 cmdline_tiny.f90 -o cmdline_tiny TESTIT ########################################### # CLEAN UP rm -f cmdline_tiny.f90 cmdline_tiny ########################################### ) |tee -a log.$$ ########################################### exit