Skip to content

Commit fca9aba

Browse files
committed
Add exit code example package
1 parent 80d7f3f commit fca9aba

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# fpm_test_exit_code
2+
Test program for application exit codes
3+
see https://github.com/fortran-lang/fpm/issues/848
4+
5+
This app expects to receive an integer command line argument, to check whether it is odd or even.
6+
It returns 0 on success (odd input), or among a few error codes otherwise.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
! Test program for application exit codes
2+
! see https://github.com/fortran-lang/fpm/issues/848
3+
4+
! This app expects to receive an integer command line argument, to check whether it is odd or even.
5+
! It returns 0 on success (odd input), or among a few error codes otherwise.
6+
7+
program check_odd_number
8+
implicit none
9+
10+
integer, parameter :: SUCCESS = 0
11+
integer, parameter :: INVALID_ARGUMENT = 1
12+
integer, parameter :: NOT_AN_INTEGER = 2
13+
integer, parameter :: NOT_ODD = 3
14+
15+
character(len=1024) :: buffer
16+
integer :: ierr,ln,the_number
17+
18+
! If the argument is missing or not an integer, return an error flag
19+
if (command_argument_count()/=1) stop INVALID_ARGUMENT
20+
21+
! Get command argument
22+
call get_command_argument(1,value=buffer,length=ln,status=ierr)
23+
24+
! On invalid string
25+
if (ln<1 .or. ierr/=0) stop INVALID_ARGUMENT
26+
27+
! Read to int
28+
read(buffer(:ln),*,iostat=ierr) the_number
29+
30+
! On invalid integer
31+
if (ierr/=0) stop NOT_AN_INTEGER
32+
33+
! Check if it is odd or even
34+
if (mod(the_number,2)==0) then
35+
! Is even
36+
stop NOT_ODD
37+
else
38+
! Is odd
39+
stop SUCCESS
40+
end if
41+
42+
end program check_odd_number
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name = "fpm_test_exit_code"
2+
version = "0.1.0"
3+
license = "license"
4+
author = "Jane Doe"
5+
maintainer = "[email protected]"
6+
copyright = "Copyright 2023, Jane Doe"
7+
[build]
8+
auto-executables = true
9+
[install]
10+
library = false

0 commit comments

Comments
 (0)