Class dc_args
In: dc_args.f90

Overview

コマンドライン引数の解析を行います.

加えて, ヘルプメッセージの表示に関して便利なサブルーチンも 用意しています.

List

DCArgsOpen :構造型 ARGS 変数の初期化
DCArgsClose :構造型 ARGS 変数の終了処理
DCArgsGet :コマンドライン引数の取得
DCArgsNumber :コマンドライン引数の数を返す
DCArgsOption :コマンドライン引数オプションを取得するための設定
DCArgsDebug :デバッグオプションの自動設定
DCArgsHelp :ヘルプオプションの自動設定
DCArgsHelpMsg :ヘルプメッセージの設定
DCArgsStrict :無効なオプションが指定された時に警告を表示するよう設定
DCArgsPutLine :構造型 ARGS 変数の内容を印字

Usage

構造型 ARGS の変数を定義し, Open, Get を利用することで コマンドライン引数を取得することができます.

    program dc_args_sample1
      use dc_types
      use dc_string, only: StoA
      use dc_args
      implicit none
      type(ARGS) :: arg
      character(STRING), pointer :: argv(:) => null()
      integer :: i

      call DCArgsOpen( arg = arg )   ! (out)
      call DCArgsDebug( arg = arg )  ! (inout)
      call DCArgsHelp( arg = arg )   ! (inout)
      call DCArgsStrict( arg = arg ) ! (inout)
      call DCArgsGet( arg = arg, &   ! (inout)
        & argv = argv )              ! (out)
      do i = 1, size( argv )
        write(*,*) argv(i)
      end do
      deallocate( argv )
      call DCArgsClose( arg = arg )  ! (inout)
    end program dc_args_sample1

引数にオプションを指定したい場合には, DCArgsOption サブルーチンを 利用してください. オプションの書式に関しては DCArgsOption の 「オプションの書式」を参照してください.

    program dc_args_sample2
      use dc_types
      use dc_string, only: StoA
      use dc_args
      implicit none
      type(ARGS) :: arg
      logical :: OPT_size
      logical :: OPT_namelist
      character(STRING) :: VAL_namelist

      call DCArgsOpen( arg = arg )              ! (out)
      call DCArgsOption( arg = arg, &           ! (inout)
        & options = StoA('-s', '--size'), &     ! (in)
        & flag = OPT_size, &                    ! (out)
        & help = "Return number of arguments")  ! (in)
      call DCArgsOption( arg = arg, &           ! (inout)
        & options = StoA('-N', '--namelist'), & ! (in)
        & flag = OPT_namelist, &                ! (out)
        & value = VAL_namelist, &               ! (out)
        & help = "Namelist filename")           ! (in)

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

      if (OPT_size) then
        write(*,*) 'number of arguments :: ', DCArgsNumber(arg)
      end if
      if (OPT_namelist) then
        write(*,*) '--namelist=', trim(VAL_namelist)
      else
        write(*,*) '--namelist is not found'
      end if
      call DCArgsClose( arg = arg ) ! (inout)
    end program dc_args_sample2

コマンドライン引数に ’-h’, ’-H’, ’—help’ のいづれかのオプションを 指定することで, オプションの一覧が標準出力に表示されます.

ヘルプメッセージの内容を充実させたい場合には DCArgsHelpMsg を 参照してください.

Note

後方互換

バージョン 20071009 以前に利用可能だった以下の手続きは, 後方互換のため, しばらくは利用可能です.

dc_args モジュールを作成した理由について

Fortran コンパイラのほとんどには IARGC, GETARG といった コマンドライン引数取得のための副プログラムが用意されている. これらの副プログラムの利用によって, コマンドラインの引数を 単に取得することは簡単である.

しかしこの IARGC, GETARG の使用に際し, 2 つほど面倒な点がある.

1 つはコンパイラ依存による IARGC, GETARG の仕様の違いである. これらの副プログラムは Fortran 規格に含まれないサービスルーチン であるため, たいていのコンパイラにはこの副プログラムは 存在するものの, 仕様が微妙に異なる場合がある. (大抵のコンパイラは GETARG の第一引数を 1 にすると一つ目の引数を取得するが, 古い HITACHI のコンパイラは第一引数を 2 にしないと一つ目の 引数を取得できない, など). そこで gt4f90io ライブラリでは これらのコンパイラ依存性を吸収する設計を行っている. dc_args モジュールを使用する際には, これらのコンパイラ依存は 気にしなくてよい. (なお, コンパイラ依存性を実際に 吸収しているのは sysdep モジュールである).

2 つ目は, コマンドライン引数におけるオプション引数 (-h や —version など) の取り扱いの不便さである. IARGC や GETARG は単に引数を取得するための副プログラムであり, Perl や Ruby などのインタプリタ言語のように, コマンドライン引数にオプション引数を処理するための ライブラリ (Getopt や OptionParser など) が用意されていない. dc_args モジュールは, Fortran プログラムでもオプション引数を 手軽に扱えるよう, オプション引数処理の ためのコーディングをできるだけ簡素にするべく整備したプログラムである.

設計思想は, オブジェクト指向スクリプト言語 RubyOptionParser を真似ており, OptionParser クラスのオブジェクトを 構造型 ARGS に, new (initialize) メソッドを DCArgsOpen サブルーチンに, on メソッドを DCArgsOption サブルーチンに, parse メソッドを DCArgsGet サブルーチンに見立てている. 言語仕様の違いにより実装や仕様は それなりに異なるが, 構造型 ARGS の変数をオブジェクトに見立て, この変数に対してサブルーチンを作用させることによって オブジェクトへの操作やオブジェクトからの引数情報の取得を行うという点では OptionParser と同様である.

おまけ的機能であるが, dc_trace モジュールとの連携も図られており, Debug サブルーチンを使用することにより (使用法は上記参照), 再コン パイルすることなく, プログラムの実行の際に "-D" オプションをつける ことでデバッグメッセージを表示するモードに変更することもできる.

Methods

Included Modules

dc_types dc_hash dc_message dc_string dc_trace dc_present sysdep

Public Instance methods

ARGS
Derived Type :

コマンドライン引数解析用の構造体です. 初期化には DCArgsOpen を, 終了処理には DCArgsClose を用います. コマンドライン引数に与えられる引数や, プログラム内で DCArgsOption, DCArgsHelpMsg サブルーチンによって与えられた情報を 格納します.

詳しい使い方は dc_args の Usage を参照ください.

Subroutine :
arg :type(ARGS), intent(inout)

ARGS 型の変数の終了処理を行います.

[Source]

  subroutine DCArgsClose0(arg)
    !
    ! ARGS 型の変数の終了処理を行います. 
    !
    use dc_hash, only: DCHashDelete
    implicit none
    type(ARGS), intent(inout) :: arg
    integer :: i
  continue
    if (arg % initialized) then
       do i = 1, size(arg % opt_table)
        deallocate(arg % opt_table(i) % options)
      end do

      deallocate(arg % opt_table)
      deallocate(arg % cmd_opts_list)
      call DCHashDelete(arg % helpmsg)
    end if
  end subroutine DCArgsClose0
Subroutine :
arg :type(ARGS), intent(inout)

ARGS 型の変数の終了処理を行います.

[Source]

  subroutine DCArgsClose0(arg)
    !
    ! ARGS 型の変数の終了処理を行います. 
    !
    use dc_hash, only: DCHashDelete
    implicit none
    type(ARGS), intent(inout) :: arg
    integer :: i
  continue
    if (arg % initialized) then
       do i = 1, size(arg % opt_table)
        deallocate(arg % opt_table(i) % options)
      end do

      deallocate(arg % opt_table)
      deallocate(arg % cmd_opts_list)
      call DCHashDelete(arg % helpmsg)
    end if
  end subroutine DCArgsClose0
Subroutine :
arg :type(ARGS), intent(inout)

デバッグオプションの自動設定を行います.

-D もしくは —debug が指定された際, 自動的に dc_trace#SetDebug を呼び出すよう arg を設定します.

[Source]

  subroutine DCArgsDebug0(arg)
    !
    ! デバッグオプションの自動設定を行います. 
    !
    ! -D もしくは --debug が指定された際, 自動的に
    ! dc_trace#SetDebug を呼び出すよう *arg* を設定します.
    !
    use dc_types, only: STRING
    use dc_string, only: StoA, StoI
    use dc_trace, only: SetDebug
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    logical :: OPT_debug
    character(STRING) :: VAL_debug
    character(len = *), parameter  :: subname = 'DCArgsDebug'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Debug in dc_args.')
      call DCArgsOpen(arg)
    end if
    call Option(arg, StoA('-D', '--debug'), OPT_debug, VAL_debug, help="call dc_trace#SetDebug (display a lot of messages for debug). " // "VAL is unit number (default is standard output)")
    if (OPT_debug) then
      if (trim(VAL_debug) == '') then
        call SetDebug
      else
        call SetDebug(StoI(VAL_debug))
      end if
    end if
    return
  end subroutine DCArgsDebug0
Subroutine :
arg :type(ARGS), intent(inout)
argv(:) :character(*), pointer
: (out)

コマンドライン引数のうち, オプションではないものを argv に返します.

argv は文字型配列のポインタです. 引数として与える場合には必ず空状態して与えてください.

[Source]

  subroutine DCArgsGet0(arg, argv)
    !
    ! コマンドライン引数のうち, オプションではないものを
    ! *argv* に返します.
    !
    ! *argv* は文字型配列のポインタです.
    ! 引数として与える場合には必ず空状態して与えてください.
    !
    use dc_types, only: STRING
    use dc_string, only: StoA, StoI, Printf, Concat, JoinChar
    use dc_present, only: present_and_true
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    character(*), pointer :: argv(:) !(out)
    integer :: i, cmd_argv_max
    character(len = *), parameter  :: subname = 'DCArgsGet'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    cmd_argv_max = size(cmd_argv_list)
    allocate(argv(cmd_argv_max))
    do i = 1, cmd_argv_max
      argv(i) = cmd_argv_list(i)
    end do
  end subroutine DCArgsGet0
Subroutine :
arg :type(ARGS), intent(inout)
force :logical, intent(in), optional

ヘルプオプションの自動設定を行います.

-h, -H, —help のいづれかが指定された際, 自動的に arg 内に設定された 情報をヘルプメッセージとして表示した後, プログラムを終了させます. 原則的に, このサブルーチンよりも前に DCArgsOption, DCArgsDebug のサブルーチンを呼んで下さい.

force に .true. が指定される場合, -H, —help オプションが与え られない場合でもヘルプメッセージを表示した後, プログラムを終了さ せます.

ヘルプメッセージに表示される情報は, DCArgsOption, DCArgsHelpMsg サブルーチンによって付加することが可能です.

[Source]

  subroutine DCArgsHelp0(arg, force)
    !
    ! ヘルプオプションの自動設定を行います. 
    !
    ! -h, -H, --help のいづれかが指定された際, 自動的に *arg* 内に設定された
    ! 情報をヘルプメッセージとして表示した後, プログラムを終了させます.
    ! 原則的に, このサブルーチンよりも前に DCArgsOption, DCArgsDebug 
    ! のサブルーチンを呼んで下さい.
    !
    ! *force* に .true. が指定される場合, -H, --help オプションが与え
    ! られない場合でもヘルプメッセージを表示した後, プログラムを終了さ
    ! せます.
    !
    ! ヘルプメッセージに表示される情報は, DCArgsOption, DCArgsHelpMsg
    ! サブルーチンによって付加することが可能です.
    !
    use dc_types, only: STRING, STDOUT
    use dc_string, only: StoA, StoI, Printf, Concat, JoinChar, UChar, LChar
    use dc_present, only: present_and_true
    use dc_message, only: MessageNotify
    use dc_hash, only: DCHashGet, DCHashDelete, DCHashRewind, DCHashNext
    implicit none
    type(ARGS), intent(inout) :: arg
    logical, intent(in), optional :: force
    logical :: OPT_help, found, end
    character(STRING) :: VAL_help, options_msg, help_msg, category
    character(STRING), pointer :: localopts(:) => null()
    integer :: unit, i
    character(len = *), parameter  :: subname = 'DCArgsHelp'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    call DCArgsOption(arg, StoA('-h', '-H', '--help'), OPT_help, VAL_help, help="display this help and exit. " // "VAL is unit number (default is standard output)")
    if (.not. OPT_help .and. .not. present_and_true(force)) then
      return
    end if
    if (trim(VAL_help) == '') then
      unit = STDOUT
    else
      unit = StoI(VAL_help)
    end if

    call Printf(unit, '')

    call DCHashGet(arg % helpmsg, 'TITLE', help_msg, found)
    if (found) then
      call Printf(unit, '%c', c1=trim(help_msg))
      call Printf(unit, '')
      call DCHashDelete(arg % helpmsg, 'TITLE')
    end if

    call DCHashGet(arg % helpmsg, 'OVERVIEW', help_msg, found)
    if (found) then
      call Printf(unit, 'Overview::')
      call PrintAutoLinefeed(unit, help_msg, indent='     ')
      call Printf(unit, '')
      call DCHashDelete(arg % helpmsg, 'OVERVIEW')
    end if

    call DCHashGet(arg % helpmsg, 'USAGE', help_msg, found)
    if (found) then
      call Printf(unit, 'Usage::')
      call PrintAutoLinefeed(unit, help_msg, indent='     ')
      call Printf(unit, '')
      call DCHashDelete(arg % helpmsg, 'USAGE')
    end if

    call Printf(unit, 'Options::')
    do i = 1, size(arg % opt_table)
      options_msg = ' '
      if (arg % opt_table(i) % optvalue_flag) then
        call Concat(arg % opt_table(i) % options, '=VAL', localopts)
      else
        allocate(localopts(size(arg % opt_table(i) % options)))
        localopts = arg % opt_table(i) % options
      end if
      options_msg = trim(options_msg) // trim(JoinChar(localopts))
      deallocate(localopts)
      call Printf(unit, ' %c', c1=trim(options_msg))
      call PrintAutoLinefeed(unit, arg % opt_table(i) % help_message, indent='     ')
      call Printf(unit, '')
    end do

    call DCHashRewind(arg % helpmsg)
    do
      call DCHashNext(arg % helpmsg, category, help_msg, end)
      if (end) exit

      call Printf(unit, '%c%c::', c1=trim(UChar(category(1:1))), c2=trim(LChar(category(2:))))
      call PrintAutoLinefeed(unit, help_msg, indent='     ')
      call Printf(unit, '')

    enddo

    call DCArgsClose(arg)

    stop
  end subroutine DCArgsHelp0
Subroutine :
arg :type(ARGS), intent(inout)
category :character(*), intent(in)
msg :character(*), intent(in)

ヘルプメッセージを追加します.

サブルーチン DCArgsHelp を使用した際に出力されるメッセージを 付加します. categoryTitle, Overview, Usage が 指定されたものは Options よりも上部に, それ以外のものは下部に表示されます. msg にはメッセージを与えてください.

    program dc_args_sample3
      use dc_types
      use dc_string, only: StoA
      use dc_args
      implicit none
      type(ARGS) :: arg
      logical :: OPT_namelist
      character(STRING) :: VAL_namelist
      character(STRING), pointer :: argv(:) => null()
      integer :: i

      call DCArgsOpen( arg = arg )   ! (out)
      call DCArgsHelpMsg( arg = arg, &              ! (inout)
        & category = 'Title', &                     ! (in)
        & msg = 'dcargs $Revision: 1.15 $ ' // &
        &       ':: Test program of dc_args' )      ! (in)
      call DCArgsHelpMsg( arg = arg, &              ! (inout)
        & category = 'Usage', &                     ! (in)
        & msg = 'dcargs [Options] arg1, arg2, ...') ! (in)
      call DCArgsOption( arg = arg, &           ! (inout)
        & options = StoA('-N', '--namelist'), & ! (in)
        & flag = OPT_namelist, &                ! (out)
        & value = VAL_namelist, &               ! (out)
        & help = "Namelist filename")           ! (in)
      call DCArgsHelpMsg( arg = arg, &          ! (inout)
        & category = 'DESCRIPTION', &           ! (in)
        & msg = '(1) Define type "HASH". ' // &
        &       '(2) Open the variable. ' // &
        &       '(3) set HelpMsg. ' // &
        &       '(4) set Options. ' // &
        &       '(5) call Debug. ' // &
        &       '(6) call Help. ' // &
        &       '(7) call Strict.')             ! (in)
      call DCArgsHelpMsg( arg = arg, &                         ! (inout)
        & category = 'Copyright', &                            ! (in)
        & msg = 'Copyright (C) ' // &
        &       'GFD Dennou Club, 2008. All rights reserved.') ! (in)
      call DCArgsDebug( arg = arg )  ! (inout)
      call DCArgsHelp( arg = arg )   ! (inout)
      call DCArgsStrict( arg = arg ) ! (inout)
      call DCArgsGet( arg = arg, &   ! (inout)
        & argv = argv )              ! (out)
      write(*,*) '--namelist=', trim( VAL_namelist )
      do i = 1, size(argv)
        write(*,*) argv(i)
      end do
      deallocate( argv )
      call DCArgsClose( arg = arg )  ! (inout)
    program dc_args_sample3

コマンドライン引数に ’-h’, ’-H’, ’—help’ のいづれかのオプション を指定することで, HelpMsg で与えたメッセージと, オプションの一覧 が標準出力に表示されます.

[Source]

  subroutine DCArgsHelpMsg0(arg, category, msg)
    !
    ! ヘルプメッセージを追加します. 
    !
    ! サブルーチン DCArgsHelp を使用した際に出力されるメッセージを
    ! 付加します. *category* に +Title+, +Overview+, +Usage+ が
    ! 指定されたものは +Options+ よりも上部に,
    ! それ以外のものは下部に表示されます.
    ! *msg* にはメッセージを与えてください.
    !
    !=== 例
    !
    !     program dc_args_sample3
    !       use dc_types
    !       use dc_string, only: StoA
    !       use dc_args
    !       implicit none
    !       type(ARGS) :: arg
    !       logical :: OPT_namelist
    !       character(STRING) :: VAL_namelist
    !       character(STRING), pointer :: argv(:) => null()
    !       integer :: i
    !
    !       call DCArgsOpen( arg = arg )   ! (out)
    !       call DCArgsHelpMsg( arg = arg, &              ! (inout)
    !         & category = 'Title', &                     ! (in)
    !         & msg = 'dcargs $Revision: 1.15 $ ' // &
    !         &       ':: Test program of dc_args' )      ! (in)
    !       call DCArgsHelpMsg( arg = arg, &              ! (inout)
    !         & category = 'Usage', &                     ! (in)
    !         & msg = 'dcargs [Options] arg1, arg2, ...') ! (in)
    !       call DCArgsOption( arg = arg, &           ! (inout)
    !         & options = StoA('-N', '--namelist'), & ! (in)
    !         & flag = OPT_namelist, &                ! (out)
    !         & value = VAL_namelist, &               ! (out)
    !         & help = "Namelist filename")           ! (in)
    !       call DCArgsHelpMsg( arg = arg, &          ! (inout)
    !         & category = 'DESCRIPTION', &           ! (in)
    !         & msg = '(1) Define type "HASH". ' // &
    !         &       '(2) Open the variable. ' // &
    !         &       '(3) set HelpMsg. ' // &
    !         &       '(4) set Options. ' // &
    !         &       '(5) call Debug. ' // &
    !         &       '(6) call Help. ' // &
    !         &       '(7) call Strict.')             ! (in)
    !       call DCArgsHelpMsg( arg = arg, &                         ! (inout)
    !         & category = 'Copyright', &                            ! (in)
    !         & msg = 'Copyright (C) ' // &
    !         &       'GFD Dennou Club, 2008. All rights reserved.') ! (in)
    !       call DCArgsDebug( arg = arg )  ! (inout)
    !       call DCArgsHelp( arg = arg )   ! (inout)
    !       call DCArgsStrict( arg = arg ) ! (inout)
    !       call DCArgsGet( arg = arg, &   ! (inout)
    !         & argv = argv )              ! (out)
    !       write(*,*) '--namelist=', trim( VAL_namelist )
    !       do i = 1, size(argv)
    !         write(*,*) argv(i)
    !       end do
    !       deallocate( argv )
    !       call DCArgsClose( arg = arg )  ! (inout)
    !     program dc_args_sample3
    !
    ! コマンドライン引数に '-h', '-H', '--help' のいづれかのオプション
    ! を指定することで, HelpMsg で与えたメッセージと, オプションの一覧
    ! が標準出力に表示されます.
    !
    use dc_hash, only: DCHashPut
    use dc_string, only: UChar
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    character(*), intent(in) :: category
    character(*), intent(in) :: msg
    character(len = *), parameter  :: subname = 'DCArgsHelpMsg'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    call DCHashPut(arg % helpmsg, key=UChar(category), value=msg)
  end subroutine DCArgsHelpMsg0
Function :
result :integer
arg :type(ARGS), intent(inout)

コマンドライン引数として与えられた引数の数を返します.

[Source]

  function DCArgsNumber0(arg) result(result)
    !
    ! コマンドライン引数として与えられた引数の数を返します.
    !
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    integer :: result
    character(len = *), parameter  :: subname = 'DCArgsNumber'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    result = size(cmd_argv_list)
  end function DCArgsNumber0
Subroutine :
arg :type(ARGS), intent(out)

ARGS 型の変数を初期設定します.

ARGS 型の変数を利用する際にはまずこのサブルーチンによって 初期設定を行ってください.

このサブルーチンは, より下層のサブルーチン内で IARGC や GETARG を用いて得られたコマンドライン引数の情報を引数 arg へと格納します.

[Source]

  subroutine DCArgsOpen0(arg)
    !
    ! ARGS 型の変数を初期設定します. 
    !
    ! ARGS 型の変数を利用する際にはまずこのサブルーチンによって
    ! 初期設定を行ってください.
    !
    ! このサブルーチンは, より下層のサブルーチン内で IARGC や GETARG
    ! を用いて得られたコマンドライン引数の情報を引数 *arg*
    ! へと格納します.
    !
    use dc_message, only: MessageNotify
    use dc_types, only: STRING
    implicit none
    type(ARGS), intent(out) :: arg
    integer:: cmd_opts_max
    character(len = *), parameter :: subname = 'DCArgsOpen'
  continue
    if (arg % initialized) then
      call MessageNotify('W', subname, 'This argument (type ARGS) is already opend.')
      return
    end if
    call BuildArgTable
    call SortArgTable
    cmd_opts_max = size(cmd_opts_list)
    allocate(arg % cmd_opts_list(cmd_opts_max))
    arg % cmd_opts_list = cmd_opts_list
    allocate(arg % opt_table(0))
    arg % initialized = .true.
  end subroutine DCArgsOpen0
Subroutine :
arg :type(ARGS), intent(inout)
options(:) :character(len = *), intent(in)
flag :logical, intent(out)
value :character(len = *), intent(out), optional
help :character(len = *), intent(in), optional

オプション情報の登録と取得を行います.

コマンドライン引数のうち, options に与えるオプションに関する情 報を flagvalue に取得します. options がコマンドライン 引数に与えられていれば flag に .true. が, そうでない場合は .false. が返ります. オプションに値が指定される場合は value に その値が返ります. オプション自体が与えられていない場合には value には空文字が返ります.

help には options に関するヘルプメッセージを arg に 登録します. サブルーチン DCArgsHelp を 用いた際に, このメッセージが出力されます. value を与えているかどうかでこのメッセージは変化します.

オプションの書式

コマンドライン引数のうち, オプションと判定されるのは以下の場合です.

  • 1 文字目が ’-’ の場合. この場合は短いオプションとなり, ’-’ の次の一文字のみがオプションとして有効になります.
  • 1-2文字目が ’—’ (ハイフン 2 文字) の場合. この場合は長いオプションとなり, ’—’ 以降の文字列がオプションとして有効になります.

オプションの値は, "=" よりも後ろの文字列になります.

コマンドライン引数 :オプション名, 値
-h :-h, 無し
help :help, 無し
-D=6 :-D, 6
-debug= :-d, 無し
—include=/usr :—include, /usr

[Source]

  subroutine DCArgsOption0(arg, options, flag, value, help)
    !
    ! オプション情報の登録と取得を行います. 
    !
    ! コマンドライン引数のうち, *options* に与えるオプションに関する情
    ! 報を *flag* と *value* に取得します. *options* がコマンドライン
    ! 引数に与えられていれば *flag* に .true. が, そうでない場合は 
    ! .false. が返ります. オプションに値が指定される場合は *value* に
    ! その値が返ります. オプション自体が与えられていない場合には
    ! *value* には空文字が返ります.
    !
    ! *help* には *options* に関するヘルプメッセージを *arg* に
    ! 登録します. サブルーチン DCArgsHelp を
    ! 用いた際に, このメッセージが出力されます.
    ! *value* を与えているかどうかでこのメッセージは変化します.
    !
    !=== オプションの書式
    !
    ! コマンドライン引数のうち, オプションと判定されるのは以下の場合です.
    !
    ! * 1 文字目が '-' の場合. この場合は短いオプションとなり, '-'
    !   の次の一文字のみがオプションとして有効になります.
    !
    ! * 1-2文字目が '--' (ハイフン 2 文字) の場合.
    !   この場合は長いオプションとなり,
    !   '--' 以降の文字列がオプションとして有効になります.
    !
    ! オプションの値は, "=" よりも後ろの文字列になります.
    !
    ! 例
    !
    ! <b>コマンドライン引数</b>  :: <b>オプション名, 値      </b>
    ! -h                         ::    -h,           無し
    ! --help                     ::    --help,       無し
    ! -D=6                       ::    -D,            6
    ! -debug=                    ::    -d,           無し
    ! --include=/usr             ::    --include,    /usr
    !

    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    character(len = *), intent(in) :: options(:)
    logical, intent(out) :: flag
    character(len = *), intent(out), optional :: value
    character(len = *), intent(in), optional :: help
    integer :: i, j, options_size, table_size
    type(OPT_ENTRY), allocatable :: local_tables(:)
    character(len = STRING) :: opt_name, opt_value, opt_full
    character(len = *), parameter  :: subname = 'DCArgsOption'
  continue
    flag = .false.
    if (present(value)) value = ''
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Option in dc_args.')
      call DCArgsOpen(arg)
    end if
    options_size = size(options)
    if (options_size < 1) then
      return
    end if

    !-----------------------------------
    ! 構造体 ARGS へのヘルプメッセージ用の情報登録
    ! * まずはテーブル arg % opt_table を一つ広げる.
    !-----------------------------------
    table_size = size(arg % opt_table)
    allocate(local_tables(table_size))
    local_tables(1:table_size) = arg % opt_table(1:table_size)
    deallocate(arg % opt_table)
    allocate(arg % opt_table(table_size + 1))
    arg % opt_table(1:table_size) = local_tables(1:table_size)
    deallocate(local_tables)
    !----- 値の代入 -----
    allocate(arg % opt_table(table_size + 1) % options(options_size))
    arg % opt_table(table_size + 1) % options = options
    arg % opt_table(table_size + 1) % help_message = ''
    if (present(help)) then
      arg % opt_table(table_size + 1) % help_message = help
    end if
    arg % opt_table(table_size + 1) % optvalue_flag = present(value)

    !----- options の正規化 -----
    do i = 1, options_size
      opt_full = arg % opt_table(table_size + 1) % options(i)
      if (DCOptionFormC(opt_full, opt_name, opt_value)) then
        arg % opt_table(table_size + 1) % options(i) = opt_name
      else
        if (len(trim(adjustl(opt_full))) < 2) then
          arg % opt_table(table_size + 1) % options(i) = '-' // trim(adjustl(opt_full))
        else
          arg % opt_table(table_size + 1) % options(i) = '--' // trim(adjustl(opt_full))
        end if
      end if
    end do

    ! arg % cmd_opts_list 内の探査と flag, value への代入
    ! 呼ばれたものに関しては arg % cmd_opts_list % flag_called を
    ! .true. に
    do i = 1, options_size
      do j = 1, size(arg % cmd_opts_list)
        if (trim(arg % opt_table(table_size + 1) % options(i)) == trim(arg % cmd_opts_list(j) % name)) then
          flag = .true.
          if (present(value)) then
            value = arg % cmd_opts_list(j) % value
          end if
          arg % cmd_opts_list(j) % flag_called = .true.
        end if
      end do
    end do
  end subroutine DCArgsOption0
Subroutine :
arg :type(ARGS), intent(in)

arg に関する情報を標準出力に表示します.

[Source]

  subroutine DCArgsPutLine0(arg)
    !
    ! *arg* に関する情報を標準出力に表示します.
    !
    use dc_types, only: STDOUT
    use dc_string, only: Printf, JoinChar
    implicit none
    type(ARGS), intent(in) :: arg
    integer :: i
  continue
    if (.not. arg % initialized) then
      call Printf(STDOUT, '#<ARGS:: @initialized=%y>', l=(/arg % initialized/))
      return
    end if
    call Printf(STDOUT, '#<ARGS:: @initialized=%y,', l=(/arg % initialized/))
    call Printf(STDOUT, '  @opt_table(:)=')
    do i = 1, size(arg % opt_table)
      call Printf(STDOUT, '    #<OPT_ENTRY:: ')
      call Printf(STDOUT, '      @options=%c, @help_message=%c, @optvalue_flag=%y', c1=trim(JoinChar(arg % opt_table(i) % options)), c2=trim(arg % opt_table(i) % help_message), l=(/arg % opt_table(i) % optvalue_flag/))
      call Printf(STDOUT, '    >')
    end do
    call Printf(STDOUT, '  ,')
    call Printf(STDOUT, '  @cmd_opts_list(:)=')
    do i = 1, size(arg % cmd_opts_list)
      call Printf(STDOUT, '    #<CMD_OPTS_INTERNAL:: ')
      call Printf(STDOUT, '      @name=%c, @value=%c, @flag_called=%y', c1=trim(arg % cmd_opts_list(i) % name), c2=trim(arg % cmd_opts_list(i) % value), l=(/arg % cmd_opts_list(i) % flag_called/))
      call Printf(STDOUT, '    >')
    end do
    call Printf(STDOUT, '  ,')
    call Printf(STDOUT, '  @cmd_argv_list(:)=%c', c1=trim(JoinChar(cmd_argv_list)))
    call Printf(STDOUT, '>')

  end subroutine DCArgsPutLine0
Subroutine :
arg :type(ARGS), intent(inout)
severe :logical, intent(in), optional

オプションチェックを行います.

コマンドライン引数のオプションとして指定されたものの内, DCArgsOption サブルーチンで設定されていないものが存在する 場合には警告を返します. severe に .true. を指定すると エラーを返して終了します. このサブルーチンを呼ぶ前に, DCArgsOption, DCArgsDebug, DCArgsHelp サブルーチンを呼んでください.

構造体 ARGS の変数に対してこのサブルーチンを適用しておく ことで, コマンドライン引数として与えたオプションが正しく プログラムが認識しているかどうかをチェックすることができます.

[Source]

  subroutine DCArgsStrict0(arg, severe)
    !
    ! オプションチェックを行います. 
    !
    ! コマンドライン引数のオプションとして指定されたものの内,
    ! DCArgsOption サブルーチンで設定されていないものが存在する
    ! 場合には警告を返します. *severe* に .true. を指定すると
    ! エラーを返して終了します.
    ! このサブルーチンを呼ぶ前に, DCArgsOption, DCArgsDebug, 
    ! DCArgsHelp サブルーチンを呼んでください.
    !
    ! 構造体 ARGS の変数に対してこのサブルーチンを適用しておく
    ! ことで, コマンドライン引数として与えたオプションが正しく
    ! プログラムが認識しているかどうかをチェックすることができます.
    !
    !
    use dc_types, only: STRING
    use dc_present, only: present_and_true
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    logical, intent(in), optional :: severe
    character(STRING) :: err_mess
    integer :: i
    character(len = *), parameter  :: subname = 'DCArgsStrict'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    do i = 1, size(arg % cmd_opts_list)
      err_mess = trim(arg % cmd_opts_list(i) % name) // ' is invalid option.'
      if (.not. arg % cmd_opts_list(i) % flag_called) then
        if (present_and_true(severe)) then
          call MessageNotify('E', subname, err_mess)
        else
          call MessageNotify('W', subname, err_mess)
        end if
      end if
    end do
  end subroutine DCArgsStrict0
Subroutine :
arg :type(ARGS), intent(inout)

デバッグオプションの自動設定を行います.

-D もしくは —debug が指定された際, 自動的に dc_trace#SetDebug を呼び出すよう arg を設定します.

[Source]

  subroutine DCArgsDebug0(arg)
    !
    ! デバッグオプションの自動設定を行います. 
    !
    ! -D もしくは --debug が指定された際, 自動的に
    ! dc_trace#SetDebug を呼び出すよう *arg* を設定します.
    !
    use dc_types, only: STRING
    use dc_string, only: StoA, StoI
    use dc_trace, only: SetDebug
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    logical :: OPT_debug
    character(STRING) :: VAL_debug
    character(len = *), parameter  :: subname = 'DCArgsDebug'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Debug in dc_args.')
      call DCArgsOpen(arg)
    end if
    call Option(arg, StoA('-D', '--debug'), OPT_debug, VAL_debug, help="call dc_trace#SetDebug (display a lot of messages for debug). " // "VAL is unit number (default is standard output)")
    if (OPT_debug) then
      if (trim(VAL_debug) == '') then
        call SetDebug
      else
        call SetDebug(StoI(VAL_debug))
      end if
    end if
    return
  end subroutine DCArgsDebug0
Subroutine :
arg :type(ARGS), intent(inout)
argv(:) :character(*), pointer
: (out)

コマンドライン引数のうち, オプションではないものを argv に返します.

argv は文字型配列のポインタです. 引数として与える場合には必ず空状態して与えてください.

[Source]

  subroutine DCArgsGet0(arg, argv)
    !
    ! コマンドライン引数のうち, オプションではないものを
    ! *argv* に返します.
    !
    ! *argv* は文字型配列のポインタです.
    ! 引数として与える場合には必ず空状態して与えてください.
    !
    use dc_types, only: STRING
    use dc_string, only: StoA, StoI, Printf, Concat, JoinChar
    use dc_present, only: present_and_true
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    character(*), pointer :: argv(:) !(out)
    integer :: i, cmd_argv_max
    character(len = *), parameter  :: subname = 'DCArgsGet'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    cmd_argv_max = size(cmd_argv_list)
    allocate(argv(cmd_argv_max))
    do i = 1, cmd_argv_max
      argv(i) = cmd_argv_list(i)
    end do
  end subroutine DCArgsGet0
Subroutine :
arg :type(ARGS), intent(inout)
force :logical, intent(in), optional

ヘルプオプションの自動設定を行います.

-h, -H, —help のいづれかが指定された際, 自動的に arg 内に設定された 情報をヘルプメッセージとして表示した後, プログラムを終了させます. 原則的に, このサブルーチンよりも前に DCArgsOption, DCArgsDebug のサブルーチンを呼んで下さい.

force に .true. が指定される場合, -H, —help オプションが与え られない場合でもヘルプメッセージを表示した後, プログラムを終了さ せます.

ヘルプメッセージに表示される情報は, DCArgsOption, DCArgsHelpMsg サブルーチンによって付加することが可能です.

[Source]

  subroutine DCArgsHelp0(arg, force)
    !
    ! ヘルプオプションの自動設定を行います. 
    !
    ! -h, -H, --help のいづれかが指定された際, 自動的に *arg* 内に設定された
    ! 情報をヘルプメッセージとして表示した後, プログラムを終了させます.
    ! 原則的に, このサブルーチンよりも前に DCArgsOption, DCArgsDebug 
    ! のサブルーチンを呼んで下さい.
    !
    ! *force* に .true. が指定される場合, -H, --help オプションが与え
    ! られない場合でもヘルプメッセージを表示した後, プログラムを終了さ
    ! せます.
    !
    ! ヘルプメッセージに表示される情報は, DCArgsOption, DCArgsHelpMsg
    ! サブルーチンによって付加することが可能です.
    !
    use dc_types, only: STRING, STDOUT
    use dc_string, only: StoA, StoI, Printf, Concat, JoinChar, UChar, LChar
    use dc_present, only: present_and_true
    use dc_message, only: MessageNotify
    use dc_hash, only: DCHashGet, DCHashDelete, DCHashRewind, DCHashNext
    implicit none
    type(ARGS), intent(inout) :: arg
    logical, intent(in), optional :: force
    logical :: OPT_help, found, end
    character(STRING) :: VAL_help, options_msg, help_msg, category
    character(STRING), pointer :: localopts(:) => null()
    integer :: unit, i
    character(len = *), parameter  :: subname = 'DCArgsHelp'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    call DCArgsOption(arg, StoA('-h', '-H', '--help'), OPT_help, VAL_help, help="display this help and exit. " // "VAL is unit number (default is standard output)")
    if (.not. OPT_help .and. .not. present_and_true(force)) then
      return
    end if
    if (trim(VAL_help) == '') then
      unit = STDOUT
    else
      unit = StoI(VAL_help)
    end if

    call Printf(unit, '')

    call DCHashGet(arg % helpmsg, 'TITLE', help_msg, found)
    if (found) then
      call Printf(unit, '%c', c1=trim(help_msg))
      call Printf(unit, '')
      call DCHashDelete(arg % helpmsg, 'TITLE')
    end if

    call DCHashGet(arg % helpmsg, 'OVERVIEW', help_msg, found)
    if (found) then
      call Printf(unit, 'Overview::')
      call PrintAutoLinefeed(unit, help_msg, indent='     ')
      call Printf(unit, '')
      call DCHashDelete(arg % helpmsg, 'OVERVIEW')
    end if

    call DCHashGet(arg % helpmsg, 'USAGE', help_msg, found)
    if (found) then
      call Printf(unit, 'Usage::')
      call PrintAutoLinefeed(unit, help_msg, indent='     ')
      call Printf(unit, '')
      call DCHashDelete(arg % helpmsg, 'USAGE')
    end if

    call Printf(unit, 'Options::')
    do i = 1, size(arg % opt_table)
      options_msg = ' '
      if (arg % opt_table(i) % optvalue_flag) then
        call Concat(arg % opt_table(i) % options, '=VAL', localopts)
      else
        allocate(localopts(size(arg % opt_table(i) % options)))
        localopts = arg % opt_table(i) % options
      end if
      options_msg = trim(options_msg) // trim(JoinChar(localopts))
      deallocate(localopts)
      call Printf(unit, ' %c', c1=trim(options_msg))
      call PrintAutoLinefeed(unit, arg % opt_table(i) % help_message, indent='     ')
      call Printf(unit, '')
    end do

    call DCHashRewind(arg % helpmsg)
    do
      call DCHashNext(arg % helpmsg, category, help_msg, end)
      if (end) exit

      call Printf(unit, '%c%c::', c1=trim(UChar(category(1:1))), c2=trim(LChar(category(2:))))
      call PrintAutoLinefeed(unit, help_msg, indent='     ')
      call Printf(unit, '')

    enddo

    call DCArgsClose(arg)

    stop
  end subroutine DCArgsHelp0
Subroutine :
arg :type(ARGS), intent(inout)
category :character(*), intent(in)
msg :character(*), intent(in)

ヘルプメッセージを追加します.

サブルーチン DCArgsHelp を使用した際に出力されるメッセージを 付加します. categoryTitle, Overview, Usage が 指定されたものは Options よりも上部に, それ以外のものは下部に表示されます. msg にはメッセージを与えてください.

    program dc_args_sample3
      use dc_types
      use dc_string, only: StoA
      use dc_args
      implicit none
      type(ARGS) :: arg
      logical :: OPT_namelist
      character(STRING) :: VAL_namelist
      character(STRING), pointer :: argv(:) => null()
      integer :: i

      call DCArgsOpen( arg = arg )   ! (out)
      call DCArgsHelpMsg( arg = arg, &              ! (inout)
        & category = 'Title', &                     ! (in)
        & msg = 'dcargs $Revision: 1.15 $ ' // &
        &       ':: Test program of dc_args' )      ! (in)
      call DCArgsHelpMsg( arg = arg, &              ! (inout)
        & category = 'Usage', &                     ! (in)
        & msg = 'dcargs [Options] arg1, arg2, ...') ! (in)
      call DCArgsOption( arg = arg, &           ! (inout)
        & options = StoA('-N', '--namelist'), & ! (in)
        & flag = OPT_namelist, &                ! (out)
        & value = VAL_namelist, &               ! (out)
        & help = "Namelist filename")           ! (in)
      call DCArgsHelpMsg( arg = arg, &          ! (inout)
        & category = 'DESCRIPTION', &           ! (in)
        & msg = '(1) Define type "HASH". ' // &
        &       '(2) Open the variable. ' // &
        &       '(3) set HelpMsg. ' // &
        &       '(4) set Options. ' // &
        &       '(5) call Debug. ' // &
        &       '(6) call Help. ' // &
        &       '(7) call Strict.')             ! (in)
      call DCArgsHelpMsg( arg = arg, &                         ! (inout)
        & category = 'Copyright', &                            ! (in)
        & msg = 'Copyright (C) ' // &
        &       'GFD Dennou Club, 2008. All rights reserved.') ! (in)
      call DCArgsDebug( arg = arg )  ! (inout)
      call DCArgsHelp( arg = arg )   ! (inout)
      call DCArgsStrict( arg = arg ) ! (inout)
      call DCArgsGet( arg = arg, &   ! (inout)
        & argv = argv )              ! (out)
      write(*,*) '--namelist=', trim( VAL_namelist )
      do i = 1, size(argv)
        write(*,*) argv(i)
      end do
      deallocate( argv )
      call DCArgsClose( arg = arg )  ! (inout)
    program dc_args_sample3

コマンドライン引数に ’-h’, ’-H’, ’—help’ のいづれかのオプション を指定することで, HelpMsg で与えたメッセージと, オプションの一覧 が標準出力に表示されます.

[Source]

  subroutine DCArgsHelpMsg0(arg, category, msg)
    !
    ! ヘルプメッセージを追加します. 
    !
    ! サブルーチン DCArgsHelp を使用した際に出力されるメッセージを
    ! 付加します. *category* に +Title+, +Overview+, +Usage+ が
    ! 指定されたものは +Options+ よりも上部に,
    ! それ以外のものは下部に表示されます.
    ! *msg* にはメッセージを与えてください.
    !
    !=== 例
    !
    !     program dc_args_sample3
    !       use dc_types
    !       use dc_string, only: StoA
    !       use dc_args
    !       implicit none
    !       type(ARGS) :: arg
    !       logical :: OPT_namelist
    !       character(STRING) :: VAL_namelist
    !       character(STRING), pointer :: argv(:) => null()
    !       integer :: i
    !
    !       call DCArgsOpen( arg = arg )   ! (out)
    !       call DCArgsHelpMsg( arg = arg, &              ! (inout)
    !         & category = 'Title', &                     ! (in)
    !         & msg = 'dcargs $Revision: 1.15 $ ' // &
    !         &       ':: Test program of dc_args' )      ! (in)
    !       call DCArgsHelpMsg( arg = arg, &              ! (inout)
    !         & category = 'Usage', &                     ! (in)
    !         & msg = 'dcargs [Options] arg1, arg2, ...') ! (in)
    !       call DCArgsOption( arg = arg, &           ! (inout)
    !         & options = StoA('-N', '--namelist'), & ! (in)
    !         & flag = OPT_namelist, &                ! (out)
    !         & value = VAL_namelist, &               ! (out)
    !         & help = "Namelist filename")           ! (in)
    !       call DCArgsHelpMsg( arg = arg, &          ! (inout)
    !         & category = 'DESCRIPTION', &           ! (in)
    !         & msg = '(1) Define type "HASH". ' // &
    !         &       '(2) Open the variable. ' // &
    !         &       '(3) set HelpMsg. ' // &
    !         &       '(4) set Options. ' // &
    !         &       '(5) call Debug. ' // &
    !         &       '(6) call Help. ' // &
    !         &       '(7) call Strict.')             ! (in)
    !       call DCArgsHelpMsg( arg = arg, &                         ! (inout)
    !         & category = 'Copyright', &                            ! (in)
    !         & msg = 'Copyright (C) ' // &
    !         &       'GFD Dennou Club, 2008. All rights reserved.') ! (in)
    !       call DCArgsDebug( arg = arg )  ! (inout)
    !       call DCArgsHelp( arg = arg )   ! (inout)
    !       call DCArgsStrict( arg = arg ) ! (inout)
    !       call DCArgsGet( arg = arg, &   ! (inout)
    !         & argv = argv )              ! (out)
    !       write(*,*) '--namelist=', trim( VAL_namelist )
    !       do i = 1, size(argv)
    !         write(*,*) argv(i)
    !       end do
    !       deallocate( argv )
    !       call DCArgsClose( arg = arg )  ! (inout)
    !     program dc_args_sample3
    !
    ! コマンドライン引数に '-h', '-H', '--help' のいづれかのオプション
    ! を指定することで, HelpMsg で与えたメッセージと, オプションの一覧
    ! が標準出力に表示されます.
    !
    use dc_hash, only: DCHashPut
    use dc_string, only: UChar
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    character(*), intent(in) :: category
    character(*), intent(in) :: msg
    character(len = *), parameter  :: subname = 'DCArgsHelpMsg'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    call DCHashPut(arg % helpmsg, key=UChar(category), value=msg)
  end subroutine DCArgsHelpMsg0
Function :
result :integer
arg :type(ARGS), intent(inout)

コマンドライン引数として与えられた引数の数を返します.

[Source]

  function DCArgsNumber0(arg) result(result)
    !
    ! コマンドライン引数として与えられた引数の数を返します.
    !
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    integer :: result
    character(len = *), parameter  :: subname = 'DCArgsNumber'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    result = size(cmd_argv_list)
  end function DCArgsNumber0
Subroutine :
arg :type(ARGS), intent(out)

ARGS 型の変数を初期設定します.

ARGS 型の変数を利用する際にはまずこのサブルーチンによって 初期設定を行ってください.

このサブルーチンは, より下層のサブルーチン内で IARGC や GETARG を用いて得られたコマンドライン引数の情報を引数 arg へと格納します.

[Source]

  subroutine DCArgsOpen0(arg)
    !
    ! ARGS 型の変数を初期設定します. 
    !
    ! ARGS 型の変数を利用する際にはまずこのサブルーチンによって
    ! 初期設定を行ってください.
    !
    ! このサブルーチンは, より下層のサブルーチン内で IARGC や GETARG
    ! を用いて得られたコマンドライン引数の情報を引数 *arg*
    ! へと格納します.
    !
    use dc_message, only: MessageNotify
    use dc_types, only: STRING
    implicit none
    type(ARGS), intent(out) :: arg
    integer:: cmd_opts_max
    character(len = *), parameter :: subname = 'DCArgsOpen'
  continue
    if (arg % initialized) then
      call MessageNotify('W', subname, 'This argument (type ARGS) is already opend.')
      return
    end if
    call BuildArgTable
    call SortArgTable
    cmd_opts_max = size(cmd_opts_list)
    allocate(arg % cmd_opts_list(cmd_opts_max))
    arg % cmd_opts_list = cmd_opts_list
    allocate(arg % opt_table(0))
    arg % initialized = .true.
  end subroutine DCArgsOpen0
Subroutine :
arg :type(ARGS), intent(inout)
options(:) :character(len = *), intent(in)
flag :logical, intent(out)
value :character(len = *), intent(out), optional
help :character(len = *), intent(in), optional

オプション情報の登録と取得を行います.

コマンドライン引数のうち, options に与えるオプションに関する情 報を flagvalue に取得します. options がコマンドライン 引数に与えられていれば flag に .true. が, そうでない場合は .false. が返ります. オプションに値が指定される場合は value に その値が返ります. オプション自体が与えられていない場合には value には空文字が返ります.

help には options に関するヘルプメッセージを arg に 登録します. サブルーチン DCArgsHelp を 用いた際に, このメッセージが出力されます. value を与えているかどうかでこのメッセージは変化します.

オプションの書式

コマンドライン引数のうち, オプションと判定されるのは以下の場合です.

  • 1 文字目が ’-’ の場合. この場合は短いオプションとなり, ’-’ の次の一文字のみがオプションとして有効になります.
  • 1-2文字目が ’—’ (ハイフン 2 文字) の場合. この場合は長いオプションとなり, ’—’ 以降の文字列がオプションとして有効になります.

オプションの値は, "=" よりも後ろの文字列になります.

コマンドライン引数 :オプション名, 値
-h :-h, 無し
help :help, 無し
-D=6 :-D, 6
-debug= :-d, 無し
—include=/usr :—include, /usr

[Source]

  subroutine DCArgsOption0(arg, options, flag, value, help)
    !
    ! オプション情報の登録と取得を行います. 
    !
    ! コマンドライン引数のうち, *options* に与えるオプションに関する情
    ! 報を *flag* と *value* に取得します. *options* がコマンドライン
    ! 引数に与えられていれば *flag* に .true. が, そうでない場合は 
    ! .false. が返ります. オプションに値が指定される場合は *value* に
    ! その値が返ります. オプション自体が与えられていない場合には
    ! *value* には空文字が返ります.
    !
    ! *help* には *options* に関するヘルプメッセージを *arg* に
    ! 登録します. サブルーチン DCArgsHelp を
    ! 用いた際に, このメッセージが出力されます.
    ! *value* を与えているかどうかでこのメッセージは変化します.
    !
    !=== オプションの書式
    !
    ! コマンドライン引数のうち, オプションと判定されるのは以下の場合です.
    !
    ! * 1 文字目が '-' の場合. この場合は短いオプションとなり, '-'
    !   の次の一文字のみがオプションとして有効になります.
    !
    ! * 1-2文字目が '--' (ハイフン 2 文字) の場合.
    !   この場合は長いオプションとなり,
    !   '--' 以降の文字列がオプションとして有効になります.
    !
    ! オプションの値は, "=" よりも後ろの文字列になります.
    !
    ! 例
    !
    ! <b>コマンドライン引数</b>  :: <b>オプション名, 値      </b>
    ! -h                         ::    -h,           無し
    ! --help                     ::    --help,       無し
    ! -D=6                       ::    -D,            6
    ! -debug=                    ::    -d,           無し
    ! --include=/usr             ::    --include,    /usr
    !

    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    character(len = *), intent(in) :: options(:)
    logical, intent(out) :: flag
    character(len = *), intent(out), optional :: value
    character(len = *), intent(in), optional :: help
    integer :: i, j, options_size, table_size
    type(OPT_ENTRY), allocatable :: local_tables(:)
    character(len = STRING) :: opt_name, opt_value, opt_full
    character(len = *), parameter  :: subname = 'DCArgsOption'
  continue
    flag = .false.
    if (present(value)) value = ''
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Option in dc_args.')
      call DCArgsOpen(arg)
    end if
    options_size = size(options)
    if (options_size < 1) then
      return
    end if

    !-----------------------------------
    ! 構造体 ARGS へのヘルプメッセージ用の情報登録
    ! * まずはテーブル arg % opt_table を一つ広げる.
    !-----------------------------------
    table_size = size(arg % opt_table)
    allocate(local_tables(table_size))
    local_tables(1:table_size) = arg % opt_table(1:table_size)
    deallocate(arg % opt_table)
    allocate(arg % opt_table(table_size + 1))
    arg % opt_table(1:table_size) = local_tables(1:table_size)
    deallocate(local_tables)
    !----- 値の代入 -----
    allocate(arg % opt_table(table_size + 1) % options(options_size))
    arg % opt_table(table_size + 1) % options = options
    arg % opt_table(table_size + 1) % help_message = ''
    if (present(help)) then
      arg % opt_table(table_size + 1) % help_message = help
    end if
    arg % opt_table(table_size + 1) % optvalue_flag = present(value)

    !----- options の正規化 -----
    do i = 1, options_size
      opt_full = arg % opt_table(table_size + 1) % options(i)
      if (DCOptionFormC(opt_full, opt_name, opt_value)) then
        arg % opt_table(table_size + 1) % options(i) = opt_name
      else
        if (len(trim(adjustl(opt_full))) < 2) then
          arg % opt_table(table_size + 1) % options(i) = '-' // trim(adjustl(opt_full))
        else
          arg % opt_table(table_size + 1) % options(i) = '--' // trim(adjustl(opt_full))
        end if
      end if
    end do

    ! arg % cmd_opts_list 内の探査と flag, value への代入
    ! 呼ばれたものに関しては arg % cmd_opts_list % flag_called を
    ! .true. に
    do i = 1, options_size
      do j = 1, size(arg % cmd_opts_list)
        if (trim(arg % opt_table(table_size + 1) % options(i)) == trim(arg % cmd_opts_list(j) % name)) then
          flag = .true.
          if (present(value)) then
            value = arg % cmd_opts_list(j) % value
          end if
          arg % cmd_opts_list(j) % flag_called = .true.
        end if
      end do
    end do
  end subroutine DCArgsOption0
Subroutine :
arg :type(ARGS), intent(in)

arg に関する情報を標準出力に表示します.

[Source]

  subroutine DCArgsPutLine0(arg)
    !
    ! *arg* に関する情報を標準出力に表示します.
    !
    use dc_types, only: STDOUT
    use dc_string, only: Printf, JoinChar
    implicit none
    type(ARGS), intent(in) :: arg
    integer :: i
  continue
    if (.not. arg % initialized) then
      call Printf(STDOUT, '#<ARGS:: @initialized=%y>', l=(/arg % initialized/))
      return
    end if
    call Printf(STDOUT, '#<ARGS:: @initialized=%y,', l=(/arg % initialized/))
    call Printf(STDOUT, '  @opt_table(:)=')
    do i = 1, size(arg % opt_table)
      call Printf(STDOUT, '    #<OPT_ENTRY:: ')
      call Printf(STDOUT, '      @options=%c, @help_message=%c, @optvalue_flag=%y', c1=trim(JoinChar(arg % opt_table(i) % options)), c2=trim(arg % opt_table(i) % help_message), l=(/arg % opt_table(i) % optvalue_flag/))
      call Printf(STDOUT, '    >')
    end do
    call Printf(STDOUT, '  ,')
    call Printf(STDOUT, '  @cmd_opts_list(:)=')
    do i = 1, size(arg % cmd_opts_list)
      call Printf(STDOUT, '    #<CMD_OPTS_INTERNAL:: ')
      call Printf(STDOUT, '      @name=%c, @value=%c, @flag_called=%y', c1=trim(arg % cmd_opts_list(i) % name), c2=trim(arg % cmd_opts_list(i) % value), l=(/arg % cmd_opts_list(i) % flag_called/))
      call Printf(STDOUT, '    >')
    end do
    call Printf(STDOUT, '  ,')
    call Printf(STDOUT, '  @cmd_argv_list(:)=%c', c1=trim(JoinChar(cmd_argv_list)))
    call Printf(STDOUT, '>')

  end subroutine DCArgsPutLine0
Subroutine :
arg :type(ARGS), intent(inout)
severe :logical, intent(in), optional

オプションチェックを行います.

コマンドライン引数のオプションとして指定されたものの内, DCArgsOption サブルーチンで設定されていないものが存在する 場合には警告を返します. severe に .true. を指定すると エラーを返して終了します. このサブルーチンを呼ぶ前に, DCArgsOption, DCArgsDebug, DCArgsHelp サブルーチンを呼んでください.

構造体 ARGS の変数に対してこのサブルーチンを適用しておく ことで, コマンドライン引数として与えたオプションが正しく プログラムが認識しているかどうかをチェックすることができます.

[Source]

  subroutine DCArgsStrict0(arg, severe)
    !
    ! オプションチェックを行います. 
    !
    ! コマンドライン引数のオプションとして指定されたものの内,
    ! DCArgsOption サブルーチンで設定されていないものが存在する
    ! 場合には警告を返します. *severe* に .true. を指定すると
    ! エラーを返して終了します.
    ! このサブルーチンを呼ぶ前に, DCArgsOption, DCArgsDebug, 
    ! DCArgsHelp サブルーチンを呼んでください.
    !
    ! 構造体 ARGS の変数に対してこのサブルーチンを適用しておく
    ! ことで, コマンドライン引数として与えたオプションが正しく
    ! プログラムが認識しているかどうかをチェックすることができます.
    !
    !
    use dc_types, only: STRING
    use dc_present, only: present_and_true
    use dc_message, only: MessageNotify
    implicit none
    type(ARGS), intent(inout) :: arg
    logical, intent(in), optional :: severe
    character(STRING) :: err_mess
    integer :: i
    character(len = *), parameter  :: subname = 'DCArgsStrict'
  continue
    if (.not. arg % initialized) then
      call MessageNotify('W', subname, 'Call Open before Help in dc_args.')
      call DCArgsOpen(arg)
    end if
    do i = 1, size(arg % cmd_opts_list)
      err_mess = trim(arg % cmd_opts_list(i) % name) // ' is invalid option.'
      if (.not. arg % cmd_opts_list(i) % flag_called) then
        if (present_and_true(severe)) then
          call MessageNotify('E', subname, err_mess)
        else
          call MessageNotify('W', subname, err_mess)
        end if
      end if
    end do
  end subroutine DCArgsStrict0

[Validate]