Command line arguments interpretation

Dc_args module of gtool5 provides subroutines for interpretation of command line arguments. By using dc_args, run time options and help message can be specified to user's program.

For example, a sample program using the dc_args module (diffusion_7.f90) is shown here, which are modified from diffusion_3.f90 in Fortran 90/95 general-purpose modules: Type parameter specification. Statements with colored font (or bold font) are associated with the dc_message module.

!= Sample program for gtool_history/gtool5
!
! * 2007/06/27 M.Odaka
! * 2006/10/25 Y.Morikawa
! * 2003/08/21 M.Odaka
! * 2001/02/27 S.Takehiro
!
! Solving diffusion equation
! \[
!     du/dt = \kappa d^2 u/dx^2
! \]
! for giving values of $u$ at $x=[0,1]$.
!
program diffusion_7

  use gtool_history                                   ! Access module (モジュール指定)
  use dc_types, only : DP, STRING                   ! Access module (モジュール指定)
  use dc_message, only: MessageNotify               ! Access module (モジュール指定)
  use dc_string, only : StoA, StoI                  ! Access module (モジュール指定)
  use dc_args, only: ARGS, DCArgsOpen, DCArgsClose, DCArgsOption, &
    & DCArgsPutLine, DCArgsDebug, DCArgsHelp, DCArgsStrict
                                                    ! Access module (モジュール指定)

  integer, parameter     :: nx=30                   ! Grid number (グリッド数)
  integer, parameter     :: nt=200                  ! Time step number (時間ステップ数)
  integer, parameter     :: ndisp=10                ! Output interval (出力間隔)
  real(DP), parameter    :: dx=1.0/(nx-1)           ! Grid interval (グリッド間隔)
  real(DP), parameter    :: dt=0.0005               ! Time step (時間間隔)
  real(DP), dimension(nx):: x=(/(dx*(i-1),i=1,nx)/) ! X coordinate (座標変数)
  real(DP), dimension(nx):: temp                    ! Temperature (温度)
  real(DP), parameter    :: kappa=1.0               ! Diffusion coefficient (熱拡散係数)
  type(ARGS)             :: arg                     ! Command line argument (引数)
  logical                :: OPT_step                ! logical parameter (引数用論理変数)
  character(STRING)      :: VAL_step                ! Value of command line argument (引数の値)

  call DCArgsOpen(arg)                       ! Initialize variable 'arg'
                                             ! (引数の初期化) 
  call DCArgsOption(arg, StoA('-S', '--step'), OPT_step, VAL_step, &
    &         help="Specify time step (nt) [default value is 200]." )
                                             ! Set "-S/--step" option
                                             ! ("-S/--step" オプションの設定)

  call DCArgsDebug(arg)                      ! Set debug option
                                             ! (デバッグオプションの自動設定)
  call DCArgsHelp(arg)                       ! Set help option
                                             ! ヘルプオプションの自動設定
  call DCArgsStrict(arg)                     ! Set exception handling
                                             ! (無効なオプション指定時に警告表示)

  if (OPT_step) then
     nt = StoI(VAL_step)                     ! Set "nt" specified by command line argument 
                                             ! (引数の値を入力)

     call MessageNotify( "M", "diffusion_7", &
       &                 "Time step is %d", i=(/nt/) )
                                             ! Message dump
                                             ! (メッセージの出力)
  end if

  call DCArgsClose(arg)                      ! Finalize variable 'arg'
                                             ! (引数の使用終了)


  tinit = 0.0                                       ! 初期時刻設定

  temp = exp(-((x-0.5)/0.1)**2)                     ! 初期値設定

  call HistoryCreate( &                             ! ヒストリー作成
    & file='diffusion_7.nc', title='Diffusion equation', &
    & source='Sample program of gtool_history/gtool5',   &
    & institution='GFD_Dennou Club davis project',       &
    & dims=(/'x','t'/), dimsizes=(/nx,0/),               &
    & longnames=(/'X-coordinate','time        '/),       &
    & units=(/'m','s'/),                                 &
    & origin=real(tinit), interval=real(ndisp*dt) )

  call HistoryPut('x',x)                            ! 次元変数出力

  call HistoryAddVariable( &                        ! 変数定義
    & varname='temp', dims=(/'x','t'/), &
    & longname='temperature', units='K', xtype='double')

  call HistoryAddAttr('temp','gt_graph_tick_all',1)
  call HistoryAddAttr('temp','gt_graph_contour_spacing',(/0.0,1.0,0.01/))
  call HistoryAddAttr('temp','+gt_user_davis_kappa',kappa)

  call HistoryPut('temp',temp)                      ! 変数出力

  do it=1,nt
    temp(2:nx-1) = temp(2:nx-1) &                   ! 時間積分
      & + kappa*(temp(3:nx)-2*temp(2:nx-1)+temp(1:nx-2))/dx**2*dt

    if ( mod(it,ndisp) == 0 ) then
      call HistoryPut('temp',temp)                  ! 変数出力
    endif
  enddo

  call HistoryClose
  stop
end program diffusion_7

In this program, the time step number 'no' can be specified by command line argument. Execution in debug mode ( Fortran 90/95 general-purpose modules: (4) Debud support ), and help message can be also specified.

  • The time step number is specified as follows

    $ ./a.out -S=<time step number>

    or,

    $ ./a.out --step=<time step number>
  • Debug option is specified as follows.

    $ ./a.out -D

    Debug messages are dumped to standard out.

  • Help message can be displayed as follows.

    $ a.out -H

    If the '-H' is given, following messages are displayed.

    Options::
     -S=VAL, --step=VAL
         Specify time step (nt) [default value is 200].
    
     -D=VAL, --debug=VAL
         call dc_trace#SetDebug (display a lot of messages for debug).
         VAL is unit number (default is standard output)
    
     -h=VAL, -H=VAL, --help=VAL
         display this help and exit. VAL is unit number (default is
         standard output)

Summary of dc_args module and its subroutines used in the sample program are as follows. In detail, please see gtool5 reference manual.

use dc_args
Access dc_args module. This statement is located at the beginning of main program. In this case, ONLY option is used.
types(ARGS)
Definition of ARGS derived data type variable. Information of command line arguments is stored this type variable.
character(STRING)
Definition of character literal constant with STRING type parameter. The type parameter of character literal constant which store the value of command line arguments must be STRING. The type parameter STRING is provided by dc_types module. This module is accessed at the beginning of main program
DCArgsOpen(arg)
Initialize ARGS derived data type variable 'arg'.
DCArgsOption(arg, options, flag, [value], [help])
Define command line options. Descriptions of each argument are as follows.
  • arg specifies ARGS derived data type variable which store information of command line arguments.
  • options specifies string of command line option. In this case, '-S' and '--step' are specified, (StoA is type translation function provided by dc_string module.
  • flag is logical argument for judgement whether command line option is specified or not. If options is specified as command line option, return value of flag is '.true.', If not, return value flag is '.false.'
  • [value] is optional argument which store the value of command line option.
  • [help] is optional argument which specifies help message. This message is displayed when -H or --help options are specified.
DCArgsDebug(arg)
Set debug option (-D/--debug) automatically.
DCArgsHelp(arg)
Set help option (-H/--help) automatically. To call this subroutine, Option and Debug subroutine must be called previously.
DCArgsStrict(arg, [severe])
Show warning message if specified command line argument is not defined by Option subroutine. If the argument [severe] is '.true.' program is terminated. To call this subroutine, Option, Debug and Help subroutine must be called previously.
DCArgsClose(arg)
Finalize ARGS derived data type variable 'arg'.

$Id: dc_args.rd,v 1.3 2009-02-28 13:36:33 morikawa Exp $