get_first_arg_from_command_line Function

public function get_first_arg_from_command_line() result(filename)

get the first argument from the command line, this is expected to be a filename

Usage: filename = get_first_arg_from_command_line()

Arguments

None

Return Value character(len=255)


Variables

Type Visibility Attributes Name Initial
character(len=255), private :: arg
integer(kind=default_int), private :: num_args

Source Code

   function get_first_arg_from_command_line() result(filename)
      !! get the first argument from the command line, this is expected to be a filename
      !!
      !! Usage: filename = get_first_arg_from_command_line()
      !!
      character(len=255) :: filename
      character(len=255) :: arg
      integer(default_int) :: num_args

      num_args = command_argument_count()

      if (num_args < 1) then
         write (*, "(A)") "Usage: ./my_executable <filename>"
         stop 1
      end if

      call get_command_argument(1, arg)

      filename = trim(adjustl(arg))

   end function get_first_arg_from_command_line