Skip to content

Commit c83fcca

Browse files
committed
fix(command_line_m): arbritrary-length flag values
1 parent 5c869b5 commit c83fcca

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

example/get-flag-value.f90

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
program get_flag_value
22
!! Demonstrate how to find the value of a command-line flag
3+
!! Running this program as follows with the command
4+
!!
5+
!! fpm run --example get-flag-value -- --input-file foo
6+
!!
7+
!! result in normal termination.
8+
use assert_m, only : assert
39
use command_line_m, only : command_line_t
410
implicit none
511

612
type(command_line_t) command_line
713
character(len=:), allocatable :: input_file_name
14+
character(len=*), parameter :: expected_name="some_file_name"
815

916
input_file_name = command_line%flag_value("--input-file")
1017

11-
! Running this program as follows with the command
12-
!
13-
! fpm run --example get-flag-value -- --input-file foo
14-
!
15-
! result in normal termination.
16-
17-
print *,"input file: ",input_file_name
18-
19-
if (input_file_name/="foo") error stop "example/get-flag-value: expected flag value 'foo' not receieved"
18+
call assert(input_file_name==expected_name,"get_flag_value: input_file_name==expected_name",diagnostic_data=input_file_name )
2019
end program

src/command_line_m.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ module function argument_present(acceptable_argument) result(found)
2323
logical found
2424
end function
2525

26-
module function flag_value(flag) result(flag_val)
26+
module function flag_value(flag)
2727
!! result is the value passed adjacent to a command-line flag
2828
implicit none
2929
character(len=*), intent(in) :: flag
30-
character(len=:), allocatable :: flag_val
30+
character(len=:), allocatable :: flag_value
3131
end function
3232

3333
end interface

src/command_line_s.f90

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
submodule(command_line_m) command_line_s
2-
use assert_m, only : assert
32
implicit none
43

54
contains
@@ -37,16 +36,21 @@
3736

3837
module procedure flag_value
3938

40-
integer argnum, arglen
41-
character(len=64) arg
39+
integer argnum, arglen, flag_value_length
40+
character(len=:), allocatable :: arg
41+
4242

4343
flag_search: &
4444
do argnum = 1,command_argument_count()
45-
call get_command_argument(argnum, arg, arglen)
45+
46+
call get_command_argument(argnum, length=arglen)
47+
allocate(character(len=arglen) :: arg)
48+
call get_command_argument(argnum, arg)
49+
4650
if (arg==flag) then
47-
call assert(arglen<=len(arg), "flag_value: arglen<=len(arg)")
48-
allocate(character(len=arglen) :: flag_val)
49-
call get_command_argument(argnum+1, flag_val)
51+
call get_command_argument(argnum+1, length=flag_value_length)
52+
allocate(character(len=flag_value_length) :: flag_value)
53+
call get_command_argument(argnum+1, flag_value)
5054
exit flag_search
5155
end if
5256
end do flag_search

test/command_line_test.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ function check_flag_value() result(test_passes)
3434
integer exit_status, command_status
3535
character(len=132) command_message
3636

37+
! command = "fpm run --example get-flag-value -- --input-file ni_weights_iter131072 > /dev/null 2>&1", &
3738
call execute_command_line( &
38-
command = "fpm run --example get-flag-value -- --input-file foo > /dev/null 2>&1", &
39+
command = "fpm run --example get-flag-value -- --input-file some_file_name", &
3940
wait = .true., exitstat = exit_status, cmdstat = command_status, cmdmsg = command_message &
4041
)
4142
test_passes = exit_status == 0

0 commit comments

Comments
 (0)