!= 繧ｳ繝槭Φ繝峨Λ繧､繝ｳ蠑墓焚隗｣譫仙�逅�
!
!= 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