Skip to content

Commit b60533f

Browse files
authored
Merge pull request #82 from MangelMaxime/feature/remaining_args
feat: add support for remaining args
2 parents f7390ab + cb963f6 commit b60533f

6 files changed

Lines changed: 111 additions & 2 deletions

File tree

Fun.Build.Tests/PipelineBuilderTests.fs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,61 @@ let ``runBeforeEachStage and runAfterEachStage should work`` () =
485485
Assert.Equal(4, j)
486486
Assert.Equal(3, ti)
487487
Assert.Equal(3, tj)
488+
489+
[<Fact>]
490+
let ``check GetAllCmdArgs and RemainingArgs works when remaining args is not empty`` () =
491+
let mutable actualAllCmdArgs = []
492+
let mutable actualRemainingArgs = []
493+
494+
pipeline "demo" {
495+
cmdArgs [ "-p"; "demo"; "test1"; "v1"; "--"; "test2"; "v2" ]
496+
stage "shows arguments" {
497+
run (fun ctx ->
498+
actualAllCmdArgs <- ctx.GetAllCmdArgs()
499+
actualRemainingArgs <- ctx.GetRemainingCmdArgs()
500+
)
501+
}
502+
runImmediate
503+
}
504+
505+
Assert.Equal<string list>([ "-p"; "demo"; "test1"; "v1" ], actualAllCmdArgs)
506+
Assert.Equal<string list>([ "test2"; "v2" ], actualRemainingArgs)
507+
508+
[<Fact>]
509+
let ``check GetAllCmdArgs and RemainingArgs works when remaining args is provided but empty`` () =
510+
let mutable actualAllCmdArgs = []
511+
let mutable actualRemainingArgs = []
512+
513+
pipeline "demo" {
514+
cmdArgs [ "-p"; "demo"; "test1"; "v1"; "--" ]
515+
stage "shows arguments" {
516+
run (fun ctx ->
517+
actualAllCmdArgs <- ctx.GetAllCmdArgs()
518+
actualRemainingArgs <- ctx.GetRemainingCmdArgs()
519+
)
520+
}
521+
runImmediate
522+
}
523+
524+
Assert.Equal<string list>([ "-p"; "demo"; "test1"; "v1" ], actualAllCmdArgs)
525+
Assert.Equal<string list>([], actualRemainingArgs)
526+
527+
528+
[<Fact>]
529+
let ``check GetAllCmdArgs and RemainingArgs works when remaining args is not provided`` () =
530+
let mutable actualAllCmdArgs = []
531+
let mutable actualRemainingArgs = []
532+
533+
pipeline "demo" {
534+
cmdArgs [ "-p"; "demo"; "test1"; "v1" ]
535+
stage "shows arguments" {
536+
run (fun ctx ->
537+
actualAllCmdArgs <- ctx.GetAllCmdArgs()
538+
actualRemainingArgs <- ctx.GetRemainingCmdArgs()
539+
)
540+
}
541+
runImmediate
542+
}
543+
544+
Assert.Equal<string list>([ "-p"; "demo"; "test1"; "v1" ], actualAllCmdArgs)
545+
Assert.Equal<string list>([], actualRemainingArgs)

Fun.Build/PipelineBuilder.fs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,13 @@ type PipelineBuilder(name: string) =
177177
member inline _.cmdArgs([<InlineIfLambda>] build: BuildPipeline, args: string list) =
178178
BuildPipeline(fun ctx ->
179179
let ctx = build.Invoke ctx
180-
{ ctx with CmdArgs = args }
180+
181+
let argsInfo = resolveCmdArgsAndRemainings args
182+
183+
{ ctx with
184+
CmdArgs = argsInfo.CmdArgs
185+
RemainingCmdArgs = argsInfo.RemainingArgs
186+
}
181187
)
182188

183189
/// Set working dir for all steps under the stage.

Fun.Build/PipelineContextExtensions.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ module PipelineContextExtensionsInternal =
2222
with _ ->
2323
envVars.Add(key, "")
2424

25+
let argsInfo = Environment.GetCommandLineArgs() |> Array.toList |> resolveCmdArgsAndRemainings
26+
2527
{
2628
Name = name
2729
Description = ValueNone
2830
Mode = Mode.Execution
2931
Verify = fun _ -> true
30-
CmdArgs = Seq.toList (Environment.GetCommandLineArgs())
32+
CmdArgs = argsInfo.CmdArgs
33+
RemainingCmdArgs = argsInfo.RemainingArgs
3134
EnvVars = envVars |> Seq.map (fun (KeyValue(k, v)) -> k, v) |> Map.ofSeq
3235
AcceptableExitCodes = set [| 0 |]
3336
Timeout = ValueNone

Fun.Build/StageContextExtensions.fs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,40 @@ module StageContextExtensions =
493493
// If not find then return ""
494494
member inline ctx.GetEnvVar(key: string) = ctx.TryGetEnvVar key |> ValueOption.defaultValue ""
495495

496+
/// <summary>
497+
/// Get the command arguments from the command line without the remaining arguments that are placed after <c>--</c>.
498+
///
499+
/// For example, if you run the following arguments:
500+
///
501+
/// <c> -p demo test1 v1 -- test2 v2</c>
502+
///
503+
/// will return
504+
///
505+
/// <c>[ "-p"; "demo"; "test1"; "v1" ]</c>
506+
/// </summary>
496507
member ctx.GetAllCmdArgs() =
497508
match ctx.ParentContext with
498509
| ValueSome(StageParent.Pipeline p) -> p.CmdArgs
499510
| ValueSome(StageParent.Stage s) -> s.GetAllCmdArgs()
500511
| ValueNone -> []
501512

513+
/// <summary>
514+
/// Return the remaining command arguments they are the arguments placed after <c>--</c> in the command line.
515+
///
516+
/// For example, if you run the following arguments:
517+
///
518+
/// <c> -p demo test1 v1 -- test2 v2</c>
519+
///
520+
/// will return
521+
///
522+
/// <c>[ "test2"; "v2" ]</c>
523+
/// </summary>
524+
member ctx.GetRemainingCmdArgs() =
525+
match ctx.ParentContext with
526+
| ValueSome(StageParent.Pipeline p) -> p.RemainingCmdArgs
527+
| ValueSome(StageParent.Stage s) -> s.GetRemainingCmdArgs()
528+
| ValueNone -> []
529+
502530
member ctx.TryGetCmdArg(key: string) =
503531
let cmdArgs = ctx.GetAllCmdArgs()
504532
match cmdArgs |> List.tryFindIndex ((=) key) with

Fun.Build/Types.Internal.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type PipelineContext = {
7676
/// Verify before run pipeline, will throw PipelineFailedException if return false
7777
Verify: PipelineContext -> bool
7878
CmdArgs: string list
79+
RemainingCmdArgs: string list
7980
EnvVars: Map<string, string>
8081
AcceptableExitCodes: Set<int>
8182
Timeout: TimeSpan voption

Fun.Build/Utils.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ let getFsiFileName () =
7474
else
7575
"your_script.fsx"
7676

77+
let resolveCmdArgsAndRemainings (args: string list) =
78+
79+
let cmdArgs = args |> List.takeWhile (fun x -> x <> "--")
80+
81+
let remainingArgs =
82+
let remainingArgsStartIndex = cmdArgs.Length + 1
83+
84+
if remainingArgsStartIndex < args.Length then
85+
args.[remainingArgsStartIndex..]
86+
else
87+
[]
88+
89+
{| CmdArgs = cmdArgs; RemainingArgs = remainingArgs |}
7790

7891
module ValueOption =
7992

0 commit comments

Comments
 (0)