hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Args

  • Command Line Arguments

    One of the projects I’m working on needed arguments passed in from the command line.  Coming from the C# world I'm use to getting the arguments from the main function.

        static void Main(string[] args)
        {
        }

    Since F# doesn't have an explicitly defined entry point I had to do some searching.  I came across a module in OCaml called Arg which worked.  The F# document can be found here.  Robert at strangelights.com has a great post on the usage of it.

    For those of you who just want an array the following will work.

       let cmdArgs = System.Environment.GetCommandLineArgs()

    Don't forget if you’re using .Net 1.x you'll need to convert the array to a compatible one.    

       let compatibleCmdArgs = Compatibility.CompatArray.to_array(cmdArgs)

    Easier yet, instead of calling GetCommandLineArgs() we can use the argv value in the module Microsoft.FSharp.MLLib.Sys which will return a compatible array for us.

       let cmdArgs = Sys.argv

  • Warming up

    After coming up with many inappropriate names for this blog title I finally gave up. 


    So here’s my first post...an example of using NMock with F#.  I was pleased to see Intellisense working correctly.

    open Microsoft.FSharp.Idioms
    open NMock

    type IService =
        interface
            abstract SomeCalculation: int
        end

    let mock = new DynamicMock((typeof() : typ<IService>).result)
    do mock.ExpectAndReturn("SomeCalculation", 5, null)

    let realinstance = mock.MockInstance :?> IService
    let x = realinstance.SomeCalculation

    do
        try
            mock.Verify( )
        with
            e -> Printf.printf "SomeCalculation not called!";;

This Blog

Post Calendar

<December 2008>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Syndication

Powered by Community Server, by Telligent Systems