9 Command Methods Overview
Craig Oates edited this page 4 years ago

Without going into too much detail, a "command-method" is a function within the ConsoleCommands module. The name "command-method" is inaccurate here but I have kept it to remain consistent with Console.Waterworks (C.W). If you would like to know more about command-methods, I recommend you read the following wiki entry:

For command-methods to work properly, there are several things they need. Which are:

  1. There us a module called ConsoleCommands in the project.
  2. The module must live in a name-space. (The module is this project resides in Commands.)
  3. ConsoleCommands must reference the Console.Waterworks namespace (I.E. open Console.Waterworks).
  4. If using C.W's "help" attributes, you must reference the Console.Waterworks.Attributes name-space.
  5. Command-methods must be a function (I.E. include unit/() in function definitions).

Please note, this list is not complete. With that said, the extra stuff is more to do with using C.W. in a C# project. If you would like to know more about using C.W. with F# and how it differs from using it with C#, checkout the following link:

Having got the above out of the way, here is a couple of command-method examples to help you get a feel for how they look.

[<ListCommand>]
[<Parameters "(image-width: int) (image-height: int)" >]
[<Description
("Saves an image to the desktop, using the default setting.\n" +
 "The user must specify the width and the height of the image.")>]
[<Usage "draw-default 500 500">]
let ``draw-default`` imgWidth imgHeight =
    try
        buildDefaultSpec imgWidth imgHeight
        |> makeImage
        |> Async.RunSynchronously
        showEndOfCommandMessage // This is what is returned.
    with
    | :? ArgumentException as ex -> ex.Message
    | _ as ex -> ex.Message

[<ListCommand>]
[<Parameters "None">]
[<Description "Lists out the colours this program uses to draw its images.">]
[<Usage "list-colours">]
let ``list-colours`` () =
    printfn "[INFO.] Listing available colours..."
    for item in colourList do
        printfn "%s" item.Key
    showEndOfCommandMessage // This is what is returned.

To help explain what the above code, please consider the image below.

command-method binding example

It is not essential to include the help attributes but they do provide help for the end-user and future developers. For more information about the help attributes, please use the following links:

The name of the command-method is what the end-user must enter into the console. If the command-method requires input arguments, the end-user must provide them, as well. For example, please consider the following image:

command-method matching run-time command

If all has gone well, you should be able to work your way through "ConsoleCommands.fs" and understand it. With that said, you will come across functions which appear to have no explanation for why the exist. If you dig around a little bit, you will notice these functions are either situated in "Validation.fs" or the .Net Standard library (I.E. "SmoulderingBeachBall"). Use the following links for more information: