Skip to content

parsing options

SimonC edited this page Apr 25, 2023 · 4 revisions

getopt.net provides all the means for parsing your user's command-line options.

Below are some real-world examples. For more information and options, please check the Home page

Real-World Example with Short Options

private string _filePath = ".file.txt";

// only ShortOpts
// called with myapp -h
// called with myapp -v
// 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
    };

    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
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
    };

    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");

Example with no, optional, and required arguments for short options!

private string _filePath = ".file.txt";

// only Options
// also works with short options!
// called with myapp -h
// called with myapp -htest
// called with myapp -v
// 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 = "h;vc:",
        AppArgs = args, // pass the args parameter
    };

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

void PrintHelp(string? helpArg) {
    if (helpArg is not null) {
        Console.WriteLine($"You requested help for a subtopic. \"{ helpArg }\"");
    }

    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");