前へ 上へ 次へ
前へ 上へ 次へ
17 コマンドライン引数を取得する方法
Fortran 2003 から導入された組込み手続 command_argument_count と get_command_argument を用いてコマンドライン引数を取得する例を以下に示します。
[ get-command-argument.f90 ] - コマンドライン引数を取得するサンプル
program arguments
implicit none
integer :: i, length, status
character(:), allocatable :: arg
intrinsic :: command_argument_count, get_command_argument
!
! コマンドライン引数を全て表示する.
!
do i = 0, command_argument_count()
call get_command_argument(i, length = length, status = status)
if (status == 0) then
!
! 引数の文字長で "arg" をアロケートする.
!
allocate (character(length) :: arg)
call get_command_argument(i, arg, status = status)
if (status == 0) then
!
! 取得した引数を表示する.
!
if (i == 0) then
print *, 'Command = "', arg, '"'
else
print *, 'Argument', i, '= "', arg, '"'
end if
end if
deallocate (arg)
end if
!
! エラーの場合はエラーメッセージを表示する.
!
if (status /= 0) print *, 'Error', status, 'on argument', i
end do
end program
前へ 上へ 次へ
