Command-line arguments

Contents
Count them
Get by index
Parse numbers
Print usage

Count them

Command_argument_count reports how many arguments followed the program name. The program name itself is not counted. Use the count to decide whether enough input was given.

program count_demo implicit none integer :: n n = command_argument_count() print *, n end program count_demo

Get by index

Get_command_argument copies one argument into a character variable by index, starting at 1. Length and status tell you the true size and whether the index existed. Always test status before using the value.

program get_demo implicit none character(len=256) :: arg integer :: L, stat call get_command_argument(1, arg, length=L, status=stat) if (stat == 0) write(*, "(a)") trim(arg(1:L)) end program get_demo

Parse numbers

Arguments arrive as text, so read them with an internal read to convert to numbers. Iostat reports conversion failures without stopping the program. Fall back to a default when parsing fails.

program parse_demo implicit none character(len=256) :: arg integer :: n = 0 integer :: ios call get_command_argument(1, arg, status=ios) if (ios == 0) read(arg, *, iostat=ios) n if (ios /= 0) n = 42 print *, n end program parse_demo

Print usage

Print a short usage message when the count is wrong, then stop. Name the program and list each expected argument in order. Clear usage text saves users from guessing after a mistyped command.

program usage_demo implicit none integer :: n n = command_argument_count() if (n < 2) then write(*, "(a)") "usage: average n1 n2" stop 1 end if end program usage_demo

Next: Environment variables

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: