!= ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³å¼•æ•°è§£æžå‡¦ç†
!
!= Command line option parser
!
! Authors::   Yoshiyuki O. Takahashi, Yasuhiro MORIKAWA
! Version::   $Id: option_parser.f90,v 1.3 2010/08/30 01:54:45 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module option_parser
  !
  != ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³å¼•æ•°è§£æžå‡¦ç†
  !
  != Command line option parser
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³å¼•æ•°ã®è§£æžå‡¦ç†ã‚’è¡Œã„ã¾ã™.
  !
  ! Command line options are parsed
  !
  !== Procedures List
  !
  ! OptParseInit  :: ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³å¼•æ•°ã®è§£æžå‡¦ç†
  ! ------------  :: ------------
  ! OptParseInit  :: Parse of command line options

  ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
  !

  ! ç¨®åˆ¥åž‹ãƒ‘ãƒ©ãƒ¡ã‚¿
  ! Kind type parameter
  !
  use dc_types, only: STRING  ! æ–‡å—åˆ—. Strings.

  ! å®£è¨€æ–‡ ; Declaration statements
  !
  implicit none
  private

  ! å…¬é–‹æ‰‹ç¶šã
  ! Public procedure
  !
  public:: OptParseInit

  ! å…¬é–‹å¤‰æ•°
  ! Public variables
  !
  logical, save, public:: option_parser_inited = .false.
                              ! åˆæœŸè¨å®šãƒ•ãƒ©ã‚°.
                              ! Initialization flag
  logical, save:: namelist_flag = .false.
                              ! NAMELIST ãƒ•ã‚¡ã‚¤ãƒ«æŒ‡å®šã®æœ‰ç„¡ã‚’ç¤ºã™ãƒ•ãƒ©ã‚°.
                              ! Flag of NAMELIST file specification

  ! éžå…¬é–‹å¤‰æ•°
  ! Private variables
  !
  character(*), parameter:: module_name = 'option_parser'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åç§°.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: option_parser.f90,v 1.3 2010/08/30 01:54:45 yot Exp $'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³
                              ! Module version

contains

  subroutine OptParseInit( &
    & namelist_filename,   & ! (out)
    & exec_name, brief     & ! (in ) optional
    & )
    !
    ! option_parser ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åˆæœŸè¨å®šã‚’è¡Œã„ã¾ã™.
    !
    ! Initialize "option_parser" module.
    !

    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
    !

    ! æ–‡å—åˆ—æ“ä½œ
    ! Character handling
    !
    use dc_string, only: StoA

    ! ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³å¼•æ•°å‡¦ç†ä¸‹è«‹ã‘
    ! Command line option parser subcontractor
    !
    use dc_args, only: ARGS, &
      & DCArgsOpen,  DCArgsHelpMsg, DCArgsOption, &
      & DCArgsDebug, DCArgsHelp,    DCArgsStrict, DCArgsClose

    ! çµ„ã¿è¾¼ã¿é–¢æ•° PRESENT ã®æ‹¡å¼µç‰ˆé–¢æ•°
    ! Extended functions of intrinsic function "PRESENT"
    !
    use dc_present, only: present_and_not_empty

    ! å®£è¨€æ–‡ ; Declaration statements
    !
    implicit none
    character(*), intent(out)          :: namelist_filename
                              ! NAMELIST ãƒ•ã‚¡ã‚¤ãƒ«ã®åç§°.
                              ! NAMELIST file name
    character(*), intent(in ), optional:: exec_name
                              ! å®Ÿè¡Œãƒ•ã‚¡ã‚¤ãƒ«å.
                              ! Executable file name
    character(*), intent(in ), optional:: brief
                              ! å®Ÿè¡Œãƒ•ã‚¡ã‚¤ãƒ«ã®ç°¡æ½”ãªèª¬æ˜Ž
                              ! Brief account of executable file

    character(STRING):: ename
                              ! å®Ÿè¡Œãƒ•ã‚¡ã‚¤ãƒ«å.
                              ! Executable file name
    character(STRING):: brief_msg
                              ! å®Ÿè¡Œãƒ•ã‚¡ã‚¤ãƒ«ã®ç°¡æ½”ãªèª¬æ˜Ž
                              ! Brief account of executable file

    type(ARGS):: arg          ! ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³å¼•æ•°.
                              ! Command line options

    ! å®Ÿè¡Œæ–‡ ; Executable statement
    !

    if ( option_parser_inited ) return
    call InitCheck

    ! ã‚ªãƒ—ã‚·ãƒ§ãƒŠãƒ«å¼•æ•°ã®å‡¦ç†.
    ! Handling of optional arguments
    !
    if ( present_and_not_empty(exec_name) ) then
      ename = exec_name
    else
      ename = 'dcpam'
    end if

    if ( present_and_not_empty(brief) ) then
      brief_msg = brief
    else
      brief_msg = 'dcpam main program'
    end if

    ! ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³å¼•æ•°ã®è§£æžå‡¦ç†
    ! Parse command line arguments
    !
    call DCArgsOpen( arg )               ! (out)

    call DCArgsHelpMsg( arg, &                          ! (inout)
      & category = 'Title', &                           ! (in)
      & msg = trim(ename) // ': ' // trim(brief_msg) )  ! (in)
    call DCArgsHelpMsg( arg, &                      ! (inout)
      & category = 'Usage', &                       ! (in)
      & msg = './' // trim(ename) // ' [Options]' ) ! (in)

    call DCArgsOption( arg, &                 ! (inout)
      & options = StoA('-N', '--namelist'), & ! (in)
      & flag = namelist_flag, &               ! (out)
      & value = namelist_filename, &          ! (out)
      & help = "Namelist filename")           ! (in)

    call DCArgsDebug( arg )  ! (inout)
    call DCArgsHelp( arg )   ! (inout)
    call DCArgsStrict( arg ) ! (inout)

    call DCArgsClose( arg )  ! (inout)

    option_parser_inited = .true.
  end subroutine OptParseInit

  subroutine InitCheck
    !
    ! ä¾å˜ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åˆæœŸåŒ–ãƒã‚§ãƒƒã‚¯
    !
    ! Check initialization of dependency modules

    ! å®Ÿè¡Œæ–‡ ; Executable statement
    !

  end subroutine InitCheck

end module option_parser