Command-line Arguments

Knowing how to handle string variables allows us to process arguments to a program from the command line. You've used programs that take arguments on the command line:

        mypc: ape asg01.f90
        mypc: f90 asg01.f90
        

How do programs like ape and f90 get arguments like asg01.f90 from the command line?

In C, command-line arguments are received in the argument variable argv in the main program.

        cla.c.dbk
        

In a Fortran program, this is done using the intrinsic subroutine getarg().

Suppose we want to write a program that computes baseexponent, but instead of asking the user to input the base and exponent, it takes them on the command line.

        mypc: f90 power.f90 -o power
        mypc: power 2 10
                   2  **           10  =         1024
        

The getarg() subroutine grabs a command line argument and stores it in a string variable. The first argument after the program name is argument 1, and so on. Argument 0 is the name of the program as it was invoked from the Unix command-line.

        character(MAX_INTEGER_DIGITS) :: program_name, &
                                         base_string, exponent_string
        
        call getarg(0, program_name)
        call getarg(1, base_string)
        call getarg(2, exponent_string)
        

If the argument number given is greater than the actual number of arguments, getarg() simply returns a blank string. We can use this to check whether the correct number of arguments were given. Note that argument 1 will never be missing if argument 2 is present, so we need only check the last argument to ensure that all are there.

        if ( exponent_string == '' ) then
            print *, trim(program_name), ': Usage: power base exponent'
            stop
        endif
        

To convert the strings to integers or reals, we use a read statement, with the string variable as the unit:

        integer :: base, exponent
        
        read (base_string, *) base
        read (exponent_string, *) exponent
        
!-----------------------------------------------------------------------
!   Program description:
!       Compute a power of command line arguments base and exponent
!
!   Arguments:
!       First:  An integer base
!       Second: An integer exponent
!-----------------------------------------------------------------------

!-----------------------------------------------------------------------
!   Modification history:
!   Date        Name        Modification
!   2011-03-25  Jason Bacon Begin
!-----------------------------------------------------------------------

module constants
    ! Global Constants
    integer, parameter :: MAX_INTEGER_DIGITS = 10
end module constants

! Main program body
program power
    use constants           ! Constants defined above
    
    ! Disable implicit declarations (i-n rule)
    implicit none
    
    ! Variable defintions
    integer :: base, exponent
    character(MAX_INTEGER_DIGITS) :: base_string, exponent_string
    
    ! First command line argument is the base, second is the exponent
    call getarg(1, base_string)
    call getarg(2, exponent_string)
    
    ! Make sure user provided both base and exponent
    if ( exponent_string == '' ) then
	stop 'Usage: power base exponent'
    endif

    ! Convert strings to integers
    read (base_string, *) base
    read (exponent_string, *) exponent
    
    ! Compute power
    print *, base, ' ** ', exponent, ' = ', base ** exponent
end program

If we do not know how many command line arguments there are, we can use the fact that getarg() returns a blank string for any index higher than the number of arguments.