get_argument Subroutine

public subroutine get_argument(idx, arg)

Obtain the command line argument at a given index

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: idx

Index of command line argument, range [0:command_argument_count()]

character(len=:), intent(out), allocatable :: arg

Command line argument


Source Code

subroutine get_argument(idx, arg)

   !> Index of command line argument, range [0:command_argument_count()]
   integer, intent(in) :: idx

   !> Command line argument
   character(len=:), allocatable, intent(out) :: arg

   integer :: length, stat

   call get_command_argument(idx, length=length, status=stat)
   if (stat /= 0) then
      return
   endif

   allocate(character(len=length) :: arg, stat=stat)
   if (stat /= 0) then
      return
   endif

   if (length > 0) then
      call get_command_argument(idx, arg, status=stat)
      if (stat /= 0) then
         deallocate(arg)
         return
      end if
   end if

end subroutine get_argument