Skip to content

parsing windows options

SimonC edited this page Apr 25, 2023 · 2 revisions

Since version v0.5.0, getopt.net supports using Windows conventions for options.

Enabling support for Windows conventions (while not enabled by default), does not disable support for GNU/POSIX conventions!

Real-World Example with Short Options

private string _filePath = ".file.txt";

// only ShortOpts
// called with myapp -h
// called with myapp -v
// called with myapp /h
// called with myapp /v
// called with myapp -c/path/to/file
// called with myapp -c /path/to/file
// called with myapp /c /path/to/file
// called with myapp /c/path/to/file
public static void Main(string[] args) {
    var getopt = new GetOpt {
        ShortOpts = "hvc:",
        AppArgs = args, // pass the args parameter
        AllowWindowsConventions = true
    };

    var optChar = 0;
    while ((optChar = getopt.GetNextOpt(out var optArg)) != -1) {
        switch (optChar) {
            case 'h':
                PrintHelp();
                return;
            case 'v':
                PrintVersion();
                return;
            case 'c':
                _filePath = optArg;
                break;
        }
    }
}

void PrintHelp() {
    Console.WriteLine(
        """
        myapp v1.0.0

        myapp shows off getopt.net :)

        Usage:
            myapp
            myapp [options]

        Arguments:
            -h          Displays this menu and exits
            -v          Displays the version info and exits
            -c [file]   Overrides the file
        """
    );
}

printVersion() => Console.WriteLine("myapp v1.0.0");

Example with Long Options

private string _filePath = ".file.txt";

// only Options
// also works with short options!
// called with myapp /help
// called with myapp /h
// called with myapp /version
// called with myapp /v
// called with myapp /config=/path/to/file
// called with myapp /config /path/to/file
// called with myapp /c/path/to/file
// called with myapp /c /path/to/file
// called with myapp /help
// called with myapp /h
// called with myapp /version
// called with myapp /v
// called with myapp /config=/path/to/file
// called with myapp /config /path/to/file
// called with myapp /c/path/to/file
// called with myapp /c /path/to/file
public static void Main(string[] args) {
    var getopt = new GetOpt {
        Options = new[] {
            new Option("help",      ArgumentType.None,     'h'),
            new Option("version",   ArgumentType.None,     'v'),
            new Option("config",    ArgumentType.Required, 'c')
        }
        AppArgs = args, // pass the args parameter
        AllowWindowsConventions = true
    };

    var optChar = 0;
    while ((optChar = getopt.GetNextOpt(out var optArg)) != -1) {
        switch (optChar) {
            case 'h':
                PrintHelp();
                return;
            case 'v':
                PrintVersion();
                return;
            case 'c':
                _filePath = optArg;
                break;
        }
    }
}

void PrintHelp() {
    Console.WriteLine(
        """
        myapp v1.0.0

        myapp shows off getopt.net :)

        Usage:
            myapp
            myapp [options]

        Arguments:
            --help,     -h          Displays this menu and exits
            --version,  -v          Displays the version info and exits
            --config,   -c [file]   Overrides the file
        """
    );
}

printVersion() => Console.WriteLine("myapp v1.0.0");