Skip to content
Snippets Groups Projects
Commit 3cad2fb7 authored by kamischi's avatar kamischi
Browse files

add examples

parent da237d2e
No related branches found
No related tags found
No related merge requests found
!***********************************************************************************************************************************
program getopt_example
use sufr_getopt, only: getopt_t, getopt, optarg, getopt_help
implicit none
character :: option, optStr*(99)
! Set the option string for short options. Only specify the character after the dash (e.g. 'a' for -a). Characters followed
! by a colon (:) have a required argument:
optstr = 'af:hx'
do ! scan all the command-line parameters
! getopt() returns a single character" ">","!",".", or the short-option character (e.g. "a" for -a).
! It also sets this 'global' variable through the SUFR_getopt module:
! - optArg: the argument following the option (if required and present)
option = getopt(trim(optstr))
! Do different things depending on the option returned:
select case(option)
case('>') ! Last parameter
if(command_argument_count().eq.0) call getopt_help(trim(optstr)) ! No parameters found - print help
exit
case('!') ! Unknown option (starting with "-" or "--")
write(*,'(A)') 'WARNING: unknown option: '//trim(optarg)//' Use -h for a list of valid options'
case('a')
write(*,'(A)') 'Found option: -'//option
case('f')
write(*,'(A)') 'Found option: -'//option//' '//trim(optarg)
case('h')
call getopt_help(trim(optstr))
case('.') ! Parameter is not an option (i.e., it doesn't start with "-" or "--")
write(*,'(A)') 'Found non-option element: '//trim(optarg)
case default
write(*,'(A)') 'Option ignored: -'//option
end select
end do
end program getopt_example
!***********************************************************************************************************************************
!***********************************************************************************************************************************
program getopt_long_example
use sufr_getopt, only: getopt_t, getopt_long, longoption, optarg, getopt_long_help
implicit none
integer :: Np
character :: option
! Set up the longopts struct to define the valid options: short option, long option, argument (0/1), short description:
type(getopt_t) :: longopts(4) = [ &
getopt_t('a', 'all', 0, 'Select all'), &
getopt_t('f', 'file', 1, 'Specify input file'), &
getopt_t('h', 'help', 0, 'Print help'), &
getopt_t('', 'ignore', 0, '') ]
np = 0
do ! scan all the command-line parameters
! getopt_long() returns a single character" ">","!",".", or the short-option character (e.g. "a" for -a).
! It also sets two 'global' variables through the SUFR_getopt module:
! - longOption: the full option (e.g. "-a" or "--all") including the dashes
! - optArg: the argument following the option (if required and present)
option = getopt_long(longopts)
! Do different things depending on the option returned:
select case(option)
case('>') ! Last parameter
if(command_argument_count().eq.0) call getopt_long_help(longopts) ! No parameters found - print help
exit
case('!') ! Unknown option (starting with "-" or "--")
write(*,'(A)') 'WARNING: unknown option: '//trim(optarg)//' Use --help for a list of valid options'
case('a')
write(*,'(A)') 'Found option: '//trim(longoption)
case('f')
write(*,'(A)') 'Found option: '//trim(longoption)//' '//trim(optarg)
case('h')
call getopt_long_help(longopts)
case('.') ! Parameter is not an option (i.e., it doesn't start with "-" or "--")
write(*,'(A)') 'Found parameter: '//trim(optarg)
np = np + 1
case default
select case(longoption)
case('--ignore') ! Note that --ignore was not given a short equivalent
write(*,'(A)') 'Found option: '//trim(longoption)
case default
write(*,'(A)') 'Valid option unhandled: '//trim(longoption)
end select
end select
end do
if(np.eq.0) then
write(*,'(A)') 'No parameters found'
else
write(*,'(I0,A)') np, ' parameters found'
end if
end program getopt_long_example
!***********************************************************************************************************************************
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment