Skip to content
mtmk edited this page Jun 2, 2011 · 7 revisions

It is a common use-case to have actions aware of cross-cutting concerns that can also be overridden from the command line (for example trace level or log file location). They could be implemented by adding the relevant parameters to every command action or could be implemented by manually parsing the command line before passing the arguments to synoptic - neither of which are ideal.

There is a feature within synoptic which is more elegant (note this exposes Mono.Options and is therefore bound by any rules inherent to that library).

It is easiest to illustrate with an example:

    public class MyGlobalOptions
    {
        public bool Verbose { get; set; }
        public string Master { get; set; }
    }

    // This will be available to commands.
    public static MyGlobalOptions GlobalOptions = new MyGlobalOptions();

    var options= new OptionSet
    {
        { "verbose", v => { MyGlobalOptions.Verbose = v; } },
        { "master=", v => { MyGlobalOptions.Master = v; } }
    };
    
    new CommandRunner().WithGlobalOptions(options).Run(args);

The MyGlobalOptions variable will be populated by synoptic when the command line is parsed. Any parameters that are not matched as a global option will be passed on to the remainder of the parser.

Note: As an added bonus, the global options will be included in the usage pattern when displaying the command help.