Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

COMMAND_ARGUMENT_COUNT

Inquiry Intrinsic Function (Generic): Returns the number of command arguments.

result = COMMAND_ARGUMENT_COUNT ()

Results

The result is a scalar of type default integer. The result value is equal to the number of command arguments available. If there are no command arguments available, the result is 0. The command name does not count as one of the command arguments.

Example

Consider the following:

  program echo_command_line
  integer i, cnt, len, status
  character c*30, b*100

  call get_command (b, len, status)
  if (status .ne. 0) then
     write (*,*) 'get_command failed with status = ', status
     stop
  end if
  write (*,*) 'command line = ', b (1:len)

  call get_command_argument (0, c, len, status)
  if (status .ne. 0) then
     write (*,*) 'Getting command name failed with status = ', status
     stop
  end if
  write (*,*) 'command name = ', c (1:len)

  cnt = command_argument_count ()
  write (*,*) 'number of command arguments = ', cnt

  do i = 1, cnt
     call get_command_argument (i, c, len, status)
     if (status .ne. 0) then
       write (*,*) 'get_command_argument failed: status = ', status, ' arg = ', i
       stop
     end if
     write (*,*) 'command arg ', i, ' = ', c (1:len)
  end do

  write (*,*) 'command line processed'
  end

If the above program is invoked with the command line " echo_command_line.exe −o 42 −a hello b", the following is displayed:

  command line = echo_command_line.exe −o 42 −a hello b
  command name = echo_command_line.exe
  number of command arguments = 5
  command arg 1 = -o
  command arg 2= 42
  command arg 3 = -a
  command arg 4 = hello
  command arg 5 = b
  command line processed