Skip to content

Commit

Permalink
Fix/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Feb 19, 2024
1 parent d1bb94f commit 613ba2e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
4 changes: 2 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### 6.2.0
* Add `ParseResults.ProgramName` [#229](https://github.com/fsprojects/Argu/pull/229)
* Add `ParseResults.GetResult(expr, 'Field -> 'R): 'R` as alias for `PostProcessResult`, `ParseResults.GetResults(expr, 'Field -> 'R): 'R list` as alias for `PostProcessResults`, `ParseResults.TryGetResult(expr, 'Field -> 'R): 'R option` as aliases for `TryPostProcessResult` [#230](https://github.com/fsprojects/Argu/pull/230)
* Add `ParseResults.GetResult(expr, unit -> 'Field, 'Field -> 'R): 'R` and `ParseResults.GetResults(expr, 'Field, ''Field -> 'R): 'R` which trap parse exceptions and map them to exit messages [#230](https://github.com/fsprojects/Argu/pull/230)
* Add `ParseResults.GetResult(expr, 'Field -> 'R): 'R` as alias for `PostProcessResult`, `ParseResults.GetResults(expr, 'Field -> 'R): 'R list` as alias for `PostProcessResults`, `ParseResults.TryGetResult(expr, 'Field -> 'R): 'R option` as alias for `TryPostProcessResult` [#230](https://github.com/fsprojects/Argu/pull/230)
* Add `ParseResults.GetResult(expr, defThunk: unit -> 'Field, parse: 'Field -> 'R): 'R` and `ParseResults.GetResults(expr, def: 'Field, parse: 'Field -> 'R): 'R` that trap parse exceptions, mapping them to parse exit messages [#230](https://github.com/fsprojects/Argu/pull/230)

### 6.1.5
* Fix the regression of the [#127](https://github.com/fsprojects/Argu/pull/127) merged in 6.1.2 and fix Mandatory arguments in nested subcommands. [#220](https://github.com/fsprojects/Argu/issues/220) [@fpellet](https://github.com/fpellet)
Expand Down
4 changes: 2 additions & 2 deletions src/Argu/ParseResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ type ParseResults<[<EqualityConditionalOn; ComparisonConditionalOn>]'Template wh
/// <param name="source">Optional source restriction: AppSettings or CommandLine.</param>
/// <param name="errorCode">The error code to be returned.</param>
/// <param name="showUsage">Print usage together with error message. Defaults to <c>true</c></param>
member r.GetResult([<ReflectedDefinition>] expr : Expr<'Field -> 'Template>, defThunk : unit -> 'Field, parser : 'Field -> 'R, ?source : ParseSource, ?errorCode, ?showUsage) : 'R =
member r.GetResult([<ReflectedDefinition>] expr: Expr<'Field -> 'Template>, defThunk: unit -> 'Field, parser: 'Field -> 'R, ?source: ParseSource, ?errorCode, ?showUsage) : 'R =
match expr |> tryGetResult source |> Option.map (parseResult parser) with
| Some x -> x
| None -> r.Catch(defThunk >> parser, ?errorCode = errorCode, ?showUsage = showUsage)
Expand All @@ -156,7 +156,7 @@ type ParseResults<[<EqualityConditionalOn; ComparisonConditionalOn>]'Template wh
/// <param name="source">Optional source restriction: AppSettings or CommandLine.</param>
/// <param name="errorCode">The error code to be returned.</param>
/// <param name="showUsage">Print usage together with error message.</param>
member r.GetResult([<ReflectedDefinition>] expr : Expr<'Field -> 'Template>, defaultValue : 'Field, parser : 'Field -> 'R, ?source, ?errorCode : ErrorCode, ?showUsage : bool) : 'R =
member r.GetResult([<ReflectedDefinition>] expr: Expr<'Field -> 'Template>, defaultValue: 'Field, parser: 'Field -> 'R, ?source, ?errorCode : ErrorCode, ?showUsage : bool) : 'R =
match expr |> tryGetResult source |> Option.map (parseResult parser) with
| Some x -> x
| None -> r.Catch((fun () -> parser defaultValue), ?errorCode = errorCode, ?showUsage = showUsage)
Expand Down
63 changes: 32 additions & 31 deletions tests/Argu.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,47 +1022,48 @@ module ``Argu Tests Main Primitive`` =
test <@ results.Contains <@ Detach @> @>
test <@ results.GetResult <@ Main @> = "main" @>

module ``Defaulting`` =
module ``Traps for defaulting and or post-processing functions`` =
let results = parser.ParseCommandLine [| "--mandatory-arg" ; "true"; "command" |]
let failingDefThunk (): string = failwith "Defaulting Failed"

[<Fact>]
let ``Trap defaulting function exceptions`` () =
let failingDefThunk (): string = failwith "Defaulting Failed"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, showUsage = false) @>
<| fun e -> <@ e.Message = "Defaulting Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk) @>
<@ results.GetResult(Working_Directory, failingDefThunk) @>
(fun e -> <@ e.Message.StartsWith "Defaulting Failed" && e.Message.Contains "--working-directory" @>)

module ``Trap defaulting function exceptions and parse exceptions`` =
[<Fact>]
let ``Trap defaulting function exceptions (for overloads with parse functions)`` () =
let parser (_ : string): string = failwith "should not be triggered"
let failingDefThunk (): string = failwith "Defaulting Failed"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Defaulting Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, parser) @>
(fun e -> <@ e.Message.StartsWith "Defaulting Failed" && e.Message.Contains "--working-directory" @>)

let results = parser.ParseCommandLine [| "--mandatory-arg" ; "true"; "command" |]
[<Fact>]
let ``Trap post processing exceptions for GetResult overloads with defaulting functions`` () =
let parser value: string = if value = "default" then failwith "Parse Failed" else failwith "unexpected"
let okDefThunk (): string = "default"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, okDefThunk, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Parse Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, okDefThunk, parser) @>
(fun e -> <@ e.Message.StartsWith "Parse Failed" && e.Message.Contains "--working-directory" @>)

[<Fact>]
let ``Trap post processing exceptions and/or defaulting function exceptions`` () =
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Defaulting Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, parser) @>
(fun e -> <@ e.Message.StartsWith "Defaulting Failed" && e.Message.Contains "--working-directory" @>)

let okDefThunk (): string = "default"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, okDefThunk, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Parse Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, okDefThunk, parser) @>
(fun e -> <@ e.Message.StartsWith "Parse Failed" && e.Message.Contains "--working-directory" @>)

[<Fact>]
let ``Trap post processing exceptions for default values`` () =
let def: string = "default"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, def, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Parse Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, def, parser) @>
(fun e -> <@ e.Message.StartsWith "Parse Failed" && e.Message.Contains "--working-directory" @>)
[<Fact>]
let ``Trap post processing exceptions for GetResult overloads with default values`` () =
let def: string = "default"
let parser value: string = if value = "default" then failwith "Parse Failed" else failwith "unexpected"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, def, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Parse Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, def, parser) @>
(fun e -> <@ e.Message.StartsWith "Parse Failed" && e.Message.Contains "--working-directory" @>)

0 comments on commit 613ba2e

Please sign in to comment.