Pass args to F#/C# Scripts

Octopus, beside PowerShell and Bash scripts, can also run F# and C# scripts.

For F# scripting is something natural, functional paradigm nicely fits into scripting approach as it does not include Circular Dependency. F# scripts can be saved as .fsx files and run from console.

C#, hovewer, is Object Oriented and things were a little bit more tricky. Luckly, some folks have created ScriptCs which allow C# to be used in scripting way. ScriptCs scripts can be saved as .csx files.

But here's a catch: scripting doesn't know the idea of Main method as starting point. You simply start going through the code, top to bottom.

So how can we access passed arguments if there's no Main?

Turns out, it's quite simple. There is System.Environment.GetCommandLineArgs

Here is how it looks like for F#

|> Seq.iter (fun x -> printfn "%s" x)

Run it with

dotnet fsi .\fsharp.fsx -- arg1 arg2

And for ScriptCs (C#)

foreach (var i in Environment.GetCommandLineArgs())

Run it with

 scriptcs main.csx -- arg1 arg2
comments powered by Disqus