Skip to content

Commit

Permalink
Merge pull request #107 from Goddard-Fortran-Ecosystem/hotfix/#105-gr…
Browse files Browse the repository at this point in the history
…eedy-plus

Fixes #105
  • Loading branch information
tclune authored Aug 15, 2022
2 parents d7f63db + a2bf82f commit 17e175f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.12)

project (FARGPARSE

VERSION 1.3.0
VERSION 1.3.1
LANGUAGES Fortran)

# Most users of this software do not (should not?) have permissions to
Expand Down
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.3.1] 2022-09-15

### Fixed

- Fixed problem where `narguments='+'` was too greedy and absorbed all remaining arguments. (Issue #105) Reproducing unit test added.

## [1.3.0] 2022-06-02

### Changed
Expand Down
5 changes: 5 additions & 0 deletions src/ArgParser.F90
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,13 @@ subroutine handle_option(this, action, argument, iter, end, embedded_value, args
if (n_arguments == '+' .and. iter == end) then
! TODO: throw exception. '+' requires at least one value
end if

do while (iter /= end)
argument => iter%get()
if (argument(1:1) == '-') then
call iter%previous()
exit
end if

select case (action%get_type())
case ('string')
Expand Down
31 changes: 31 additions & 0 deletions tests/Test_ArgParser.pf
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,35 @@ contains

end subroutine test_unprocessed_argument

! Reproducer for issue #105
@test
subroutine test_greedy_plus
use fp_CommandLineArguments
use fp_String
type (ArgParser) :: p
type (StringVector) :: arguments
type (StringUnlimitedMap) :: options
class(*), pointer :: opt
type(StringVector) :: p_vals

p = ArgParser()
call p%add_argument('-p','--potential',type='string',n_arguments='+',help='...')
call p%add_argument('-q','--query',type='string',help='...')

call arguments%push_back('-p')
call arguments%push_back('s1')
call arguments%push_back('s2')
call arguments%push_back('s3')
call arguments%push_back('-q')
call arguments%push_back('hello')
options = p%parse_args(arguments)
@assert_that(int(options%size()), is(2))

call cast(options%at('potential'), p_vals)
@assert_that(int(p_vals%size()), is(3))
@assertEqual('s1', p_vals%of(1))
@assertEqual('s2', p_vals%of(2))
@assertEqual('s3', p_vals%of(3))

end subroutine test_greedy_plus
end module Test_ArgParser

0 comments on commit 17e175f

Please sign in to comment.