@@ -311,6 +311,91 @@ let main argv =
311311 |> Async.RunSynchronously
312312```
313313
314+ ### Global Options
315+
316+ This example shows to create global options to all child commands.
317+
318+ ``` F#
319+ module ProgramNestedSubCommands
320+
321+ open System.IO
322+ open FSharp.SystemCommandLine
323+ open System.CommandLine.Invocation
324+
325+ module Global =
326+ let enableLogging = Input.Option<bool>("--enable-logging", false)
327+ let logFile = Input.Option<FileInfo>("--log-file", FileInfo @"c:\temp\default.log")
328+
329+ type Options = { EnableLogging: bool; LogFile: FileInfo }
330+
331+ let options: HandlerInput seq = [ enableLogging; logFile ]
332+
333+ let bind (ctx: InvocationContext) =
334+ { EnableLogging = enableLogging.GetValue ctx
335+ LogFile = logFile.GetValue ctx }
336+
337+ let listCmd =
338+ let handler (ctx: InvocationContext, dir: DirectoryInfo) =
339+ let options = Global.bind ctx
340+ if options.EnableLogging then
341+ printfn $"Logging enabled to {options.LogFile.FullName}"
342+
343+ if dir.Exists then
344+ dir.EnumerateFiles()
345+ |> Seq.iter (fun f -> printfn "%s" f.FullName)
346+ else
347+ printfn $"{dir.FullName} does not exist."
348+
349+ let dir = Input.Argument("directory", DirectoryInfo(@"c:\default"))
350+
351+ command "list" {
352+ description "lists contents of a directory"
353+ inputs (Input.Context(), dir)
354+ setHandler handler
355+ addAlias "ls"
356+ }
357+
358+ let deleteCmd =
359+ let handler (ctx: InvocationContext, dir: DirectoryInfo, recursive: bool) =
360+ let options = Global.bind ctx
361+ if options.EnableLogging then
362+ printfn $"Logging enabled to {options.LogFile.FullName}"
363+
364+ if dir.Exists then
365+ if recursive then
366+ printfn $"Recursively deleting {dir.FullName}"
367+ else
368+ printfn $"Deleting {dir.FullName}"
369+ else
370+ printfn $"{dir.FullName} does not exist."
371+
372+ let dir = Input.Argument("directory", DirectoryInfo(@"c:\default"))
373+ let recursive = Input.Option("--recursive", false)
374+
375+ command "delete" {
376+ description "deletes a directory"
377+ inputs (Input.Context(), dir, recursive)
378+ setHandler handler
379+ addAlias "del"
380+ }
381+
382+ let ioCmd =
383+ command "io" {
384+ description "Contains IO related subcommands."
385+ setHandler id
386+ addGlobalOptions Global.options
387+ addCommands [ deleteCmd; listCmd ]
388+ }
389+
390+ [<EntryPoint>]
391+ let main argv =
392+ rootCommand argv {
393+ description "Sample app for System.CommandLine"
394+ setHandler id
395+ addCommand ioCmd
396+ }
397+ ```
398+
314399### Database Migrations Example
315400
316401This real-life example for running database migrations demonstrates the following features:
0 commit comments