getopt.net-bsd 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package getopt.net-bsd --version 0.2.0
NuGet\Install-Package getopt.net-bsd -Version 0.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="getopt.net-bsd" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add getopt.net-bsd --version 0.2.0
#r "nuget: getopt.net-bsd, 0.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install getopt.net-bsd as a Cake Addin
#addin nuget:?package=getopt.net-bsd&version=0.2.0

// Install getopt.net-bsd as a Cake Tool
#tool nuget:?package=getopt.net-bsd&version=0.2.0

getopt.net - A GNU getopt port to .net

This repository contains the code for my port of the GNU getopt functionality found on most Unix-like systems.

getopt.net is written entirely in C# and is a "cleanroom port"; although not necessary it made the project that much more fun 😃

Features

Full support for getopt-like command-line options:

  • --long-opts
  • --long-opts=with_options
  • --long-opts with_options
  • -ShortOpTs
  • -ShortsWithOpTi0n5
  • -s with_opts

The standard getopt shortopt-string format is supported:

getopt.ShortOpts = "abC:dE:f:GhIjkLmnop:q:r:";

Customisation is available with long opts:

getopt.Options = new[] {
    new Option { Name = "help", ArgumentType.None, IntPtr.Zero, 'h' },
    new Option("config", ArgumentType.Required, IntPtr.Zero, 'c'),
    new Option("version", ArgumentType.Optional, IntPtr.Zero, 'v')
};

Fallback to long opts (if available!)

Most developers will have experienced this at some point when using getopt; you added an option to your long opts, but forgot it in your shortopt string. getopt.net improves this behaviour and will check the Options array to see if the option you've provided is there.

Customisable behaviour

getopt.net can be configured to not throw exceptions if that's your thing. Just set the IgnoreXXX options to true, and getopt.net will ignore bad user input!

If IgnoreInvalidOptions is enabled, entering an unknown option won't throw an exception, but instead a ! will be returned. If IgnoreMissingArguments is enabled, forgetting to add a required argument won't thow an exception either! Instead, ? will be returned.

The exceptions do contain more info, however.

Usage:

At the time of writing this, I have not figured out all the details of using the library! Anything written here, until noted otherwise, is subject to change!

Things that don't work

I forgot to implement the whole "provide a flag pointer" functionality. I'll do that later; it needs unsafe code anyway, so it'll have to be in its own little method.

Basic usage


using getopt.net;

static void Main(string[] args) {

    var getopt = new Getopt {
        Options = new[] {
            new Option("help",    ArgumentType.None, IntPtr.Zero, 'h'),
            new Option("version", ArgumentType.None, IntPtr.Zero, 'v'),
            // or, alternatively
            new Option { Name = "config", ArgumentType.Required, IntPtr.Zero, 'c' }
        },
        ShortOpts = "hvc:",
        DoubleDashStopsParsing = true, // optional
        AppArgs = args, // REQUIRED
        OnlyShortOpts = false,
        // other options here
    };

    int opt = 0;
    // GetNextOpt may throw exceptions, depending on your settings!
    while ((opt = getopt.GetNextOpt(out var optArg)) != -1) {
        switch (opt) {
            case 'h':
                // print help or something
                break;
            //
        }
    }
}

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.9.1 193 12/22/2023
0.9.0 132 10/1/2023
0.8.1 169 4/28/2023
0.8.0 145 4/20/2023
0.7.0 152 4/19/2023
0.6.0 179 3/29/2023
0.5.1 375 3/5/2023
0.5.0 274 3/2/2023
0.4.0 318 2/28/2023
0.3.1 233 2/27/2023
0.3.0 214 2/26/2023
0.2.0 252 2/19/2023