Skip to content

Commit

Permalink
feat(*PostProcess*): Add *GetResult* aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Feb 19, 2024
1 parent 71625ae commit 6cb744d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 6.2.0
* Add `ParseResults.ProgramName` [#999](https://github.com/fsprojects/Argu/pull/999)
* 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` [#999](https://github.com/fsprojects/Argu/pull/999)

### 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 docs/tutorial.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ In this case,
* [`AltCommandLine`](reference/argu-arguattributes-altcommandlineattribute.html): specifies an alternative command line switch.
* [`EqualsAssignment`](reference/argu-arguattributes-equalsassignmentattribute.html) : enforces `--assignment=value` and `--assignment key=value` CLI syntax.
* [`EqualsAssignmentOrSpaced`](reference/argu-arguattributes-equalsassignmentorspacedattribute.html) : enforces `--assignment=value` and `--assignment value` CLI syntax.
* [`Unique`](reference/argu-arguattributes-uniqueattribute.html) : parser will fail if CLI provides this argument more than once.
Expand Down Expand Up @@ -414,7 +414,7 @@ let parsePort p =
failwith "invalid port number."
else p

let ports = results.PostProcessResults (<@ Port @>, parsePort)
let ports = results.GetResults(<@ Port @>, parsePort)

(**
Expand Down
53 changes: 43 additions & 10 deletions src/Argu/ParseResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ type ParseResults<[<EqualityConditionalOn; ComparisonConditionalOn>]'Template wh
/// <param name="expr">The name of the parameter, expressed as quotation of DU constructor.</param>
/// <param name="parser">The post-processing parser.</param>
/// <param name="source">Optional source restriction: AppSettings or CommandLine.</param>
// [<System.Obsolete("Please use the revised API name instead: GetResult")>]
member r.PostProcessResult ([<ReflectedDefinition>] expr : Expr<'Field -> 'Template>, parser : 'Field -> 'R, ?source) : 'R =
expr |> getResult source |> parseResult parser

Expand All @@ -179,6 +180,7 @@ type ParseResults<[<EqualityConditionalOn; ComparisonConditionalOn>]'Template wh
/// <param name="expr">The name of the parameter, expressed as quotation of DU constructor.</param>
/// <param name="parser">The post-processing parser.</param>
/// <param name="source">Optional source restriction: AppSettings or CommandLine.</param>
// [<System.Obsolete("Please use the revised API name instead: GetResults")>]
member r.PostProcessResults ([<ReflectedDefinition>] expr : Expr<'Field -> 'Template>, parser : 'Field -> 'R, ?source) : 'R list =
expr |> getResults source |> Seq.map (parseResult parser) |> Seq.toList

Expand All @@ -189,9 +191,40 @@ type ParseResults<[<EqualityConditionalOn; ComparisonConditionalOn>]'Template wh
/// <param name="expr">The name of the parameter, expressed as quotation of DU constructor.</param>
/// <param name="parser">The post-processing parser.</param>
/// <param name="source">Optional source restriction: AppSettings or CommandLine.</param>
// [<System.Obsolete("Please use the revised API name instead: TryGetResult")>]
member r.TryPostProcessResult ([<ReflectedDefinition>] expr : Expr<'Field -> 'Template>, parser : 'Field -> 'R, ?source) : 'R option =
expr |> tryGetResult source |> Option.map (parseResult parser)

/// <summary>Returns the *last* specified parameter of given type.
/// Command line parameters have precedence over AppSettings parameters.
/// Results are passed to a post-processing function that is error handled by the parser.
/// </summary>
/// <param name="expr">The name of the parameter, expressed as quotation of DU constructor.</param>
/// <param name="parser">The post-processing parser.</param>
/// <param name="source">Optional source restriction: AppSettings or CommandLine.</param>
member r.GetResult([<ReflectedDefinition>] expr : Expr<'Field -> 'Template>, parser : 'Field -> 'R, ?source) : 'R =
expr |> getResult source |> parseResult parser

/// <summary>Query parse results for given argument kind.
/// Command line parameters have precedence over AppSettings parameters.
/// Results are passed to a post-processing function that is error handled by the parser.
/// </summary>
/// <param name="expr">The name of the parameter, expressed as quotation of DU constructor.</param>
/// <param name="parser">The post-processing parser.</param>
/// <param name="source">Optional source restriction: AppSettings or CommandLine.</param>
member r.GetResults([<ReflectedDefinition>] expr : Expr<'Field -> 'Template>, parser : 'Field -> 'R, ?source) : 'R list =
expr |> getResults source |> Seq.map (parseResult parser) |> Seq.toList

/// <summary>Returns the *last* specified parameter of given type.
/// Command line parameters have precedence over AppSettings parameters.
/// Results are passed to a post-processing function that is error handled by the parser.
/// </summary>
/// <param name="expr">The name of the parameter, expressed as quotation of DU constructor.</param>
/// <param name="parser">The post-processing parser.</param>
/// <param name="source">Optional source restriction: AppSettings or CommandLine.</param>
member r.TryGetResult([<ReflectedDefinition>] expr : Expr<'Field -> 'Template>, parser : 'Field -> 'R, ?source) : 'R option =
expr |> tryGetResult source |> Option.map (parseResult parser)

/// <summary>
/// Iterates through *all* parse results for a given argument kind.
/// Command line parameters have precedence over AppSettings parameters.
Expand Down Expand Up @@ -239,30 +272,30 @@ type ParseResults<[<EqualityConditionalOn; ComparisonConditionalOn>]'Template wh

// used by StructuredFormatDisplay attribute
member private r.StructuredFormatDisplay = r.ToString()

// used by EqualityConditionalOn attribute
// used by ComparisonConditionalOn attribute
member val private CachedAllResults = lazy (getAllResults None) with get

// used by EqualityConditionalOn attribute
override r.Equals (other : obj) =
override r.Equals (other : obj) =
match other with
| :? ParseResults<'Template> as other ->
Unchecked.equals
| :? ParseResults<'Template> as other ->
Unchecked.equals
r.CachedAllResults.Value
other.CachedAllResults.Value
| _ -> false

// used by EqualityConditionalOn attribute
override r.GetHashCode () =
override r.GetHashCode () =
Unchecked.hash r.CachedAllResults.Value

// used by ComparisonConditionalOn attribute
interface System.IComparable with
interface System.IComparable with
member r.CompareTo other =
match other with
| :? ParseResults<'Template> as other ->
Unchecked.compare
r.CachedAllResults.Value
| :? ParseResults<'Template> as other ->
Unchecked.compare
r.CachedAllResults.Value
other.CachedAllResults.Value
| _ -> invalidArg "other" "cannot compare values of different types"
10 changes: 5 additions & 5 deletions tests/Argu.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ module ``Argu Tests Main List`` =
test <@ results.Contains <@ Detach @> @>
test <@ results.GetResult <@ Listener @> = ("localhost", 8080) @>
test <@ results.GetResults <@ Log_Level @> = [2] @>
test <@ results.PostProcessResult (<@ Log_Level @>, fun x -> x + 1) = 3 @>
test <@ results.GetResult (<@ Log_Level @>, fun x -> x + 1) = 3 @>

[<Fact>]
let ``Simple AppSettings parsing`` () =
Expand All @@ -203,7 +203,7 @@ module ``Argu Tests Main List`` =
test <@ results.Contains <@ Detach @> @>
test <@ results.GetResult <@ Listener @> = ("localhost", 8080) @>
test <@ results.GetResults <@ Log_Level @> = [2] @>
test <@ results.PostProcessResult (<@ Log_Level @>, fun x -> x + 1) = 3 @>
test <@ results.GetResult (<@ Log_Level @>, fun x -> x + 1) = 3 @>

[<Fact>]
let ``Simple AppSettings contains usage comments`` () =
Expand Down Expand Up @@ -423,7 +423,7 @@ module ``Argu Tests Main List`` =
test <@ results.Contains <@ Detach @> @>
test <@ results.GetResult <@ Listener @> = ("localhost", 8080) @>
test <@ results.GetResults Log_Level = [2] @>
test <@ results.PostProcessResult (<@ Log_Level @>, fun x -> x + 1) = 3 @>
test <@ results.GetResult(Log_Level, fun x -> x + 1) = 3 @>

[<Fact>]
let ``Fail on misplaced First parameter`` () =
Expand Down Expand Up @@ -980,7 +980,7 @@ module ``Argu Tests Main Primitive`` =
test <@ results.Contains <@ Detach @> @>
test <@ results.GetResult <@ Listener @> = ("localhost", 8080) @>
test <@ results.GetResults <@ Log_Level @> = [2] @>
test <@ results.PostProcessResult (<@ Log_Level @>, fun x -> x + 1) = 3 @>
test <@ results.GetResult(<@ Log_Level @>, fun x -> x + 1) = 3 @>

[<Fact>]
let ``Help String`` () =
Expand All @@ -1005,7 +1005,7 @@ module ``Argu Tests Main Primitive`` =
test <@ results.Contains <@ Detach @> @>
test <@ results.GetResult <@ Listener @> = ("localhost", 8080) @>
test <@ results.GetResults Log_Level = [2] @>
test <@ results.PostProcessResult (<@ Log_Level @>, fun x -> x + 1) = 3 @>
test <@ results.GetResult(<@ Log_Level @>, fun x -> x + 1) = 3 @>

[<Fact>]
let ``Unrecognized CLI params`` () =
Expand Down

0 comments on commit 6cb744d

Please sign in to comment.