getopt.net-bsd 0.8.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package getopt.net-bsd --version 0.8.1
NuGet\Install-Package getopt.net-bsd -Version 0.8.1
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.8.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add getopt.net-bsd --version 0.8.1
#r "nuget: getopt.net-bsd, 0.8.1"
#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.8.1

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

getopt.net - A GNU and POSIX 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 šŸ˜ƒ

<p align="center" > <img align="center" src="https://raw.githubusercontent.com/SimonCahill/getopt.net/master/img/getopt.net-logo-128.png" alt="getopt.net-logo" > </p>

Build Workflow Test Workflow Nuget Version GitHub all releases Nuget Downloads GitHub CodeQL

Installation

There are several methods of installing and using getopt.net in your project.

  1. Add the repository as a submodule, checkout a tag and include it as a project reference in your solution
  2. Use the NuGet package manager: install-package getopt.net-bsd Note the -bsd ending which shows the license used and not system requirements! getopt.net was already in use šŸ˜¦

NuGet page

Features

Full support for getopt-like command-line options

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

Support for options using the Windows convention

  • /h (short opts)
  • /long-opts
  • /hxcfsdf (GNU-style short opts!)
  • /long-opts:with-win-style-args
  • /long-opts with-args-separated-by-space
  • /long-opts=with-posix-separator
  • /fmyfile (short opts with parameters!)

Support for Powershell-style options

  • -myoption
  • -myoption=argument
  • -myoption:argument (AllowWindowsConvention must also be enabled!)
  • -myoption argument
  • -myoption argument

Support for paramfiles

Some applications, such as GCC, allow passing of paramfile arguments. A paramfile is a line-separated text file which contains one option (and argument) per line. Each line of the paramfile is parsed as if it were passed to getopt.net directly.

Syntax:

myapp @/path/to/paramfile

The standard getopt shortopt-string format is supported:

: denotes a required argument!

; denotes an optional argument!

If none of the above is present after a character in ShortOpts, then no argument is required.

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

If getopt.ShortOpts is prefixed by a +, or the environment variable POSIXLY_CORRECT is set, then getopt.net will stop processing more options as soon when the first non-option string is found.

If getopt.ShortOpts is prefixed by a -, then each non-option string will be treated as if it were the argument to an option with the value 1.

Customisation is available with long opts:

getopt.Options = new[] {
    new Option { Name = "help", ArgumentType = ArgumentType.None, Value = 'h' }, // brace-initialiser
    new Option("config", ArgumentType.Required, 'c'), // standard constructor
    new Option("version", ArgumentType.Optional, '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:

For a more detailled description of using getopt.net, please consult the Wiki.

Basic usage in Cā™Æ


using getopt.net;

static void Main(string[] args) {

    var getopt = new Getopt {
        Options = new[] {
            new Option("help",    ArgumentType.None, 'h'),
            new Option("version", ArgumentType.None, 'v'),
            // or, alternatively
            new Option { Name = "config", ArgumentType.Required, 'c' }
        },
        ShortOpts = "hvc:",
        AppArgs = args, // REQUIRED
        OnlyShortOpts = false,
        // AllowWindowsConventions = true, // enable this for Windows-style options
        // 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;
            //
        }
    }
}

Basic usage in VB.Net

Imports getopt.net

module Program

    Dim _progOptions() As [Option] = {
        New [Option]("help", ArgumentType.None, "h"c),
        New [Option]("version", ArgumentType.None, "v"c),
        New [Option]("file", ArgumentType.Required, "f"c)
    }

    Dim _progShortOptions As String = "hvf:"

    sub Main(args as string())
        Dim getopt = New GetOpt With {
            .AppArgs = args,
            .Options = _progOptions,
            .ShortOpts = _progShortOptions,
            ' .AllowWindowsConventions = true ' enable me for Windows-style options!
        }

        Dim optChar = 0
        Dim optArg As String = Nothing
        Dim fileToRead As String = Nothing

        While optChar <> -1
            optChar = getopt.GetNextOpt(optArg)

            Select Case optChar
                Case Convert.ToInt32("h"c)
                    ' do something
                    Return
                Case Convert.ToInt32("v"c)
                    ' do something else
                    Return
                Case Convert.ToInt32("f"c)
                    ' do something with optArg
            End Select
        End While
    end sub

end module

Bugs and errors

If you encounter a bug, please add a GitHub Issue and/or create a fork of the project and create a pull request.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  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. 
.NET Framework net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6

    • No dependencies.
  • net6.0

    • No dependencies.
  • 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 185 12/22/2023
0.9.0 129 10/1/2023
0.8.1 166 4/28/2023
0.8.0 143 4/20/2023
0.7.0 149 4/19/2023
0.6.0 176 3/29/2023
0.5.1 372 3/5/2023
0.5.0 272 3/2/2023
0.4.0 315 2/28/2023
0.3.1 230 2/27/2023
0.3.0 212 2/26/2023
0.2.0 249 2/19/2023

v0.8.1
Version 0.8.1 introduces a non-breaking change which allows for more than 255 possible values for long options.

Changes
- Option struct now contains an Int32 for its Value property
   - An override constructor was added to allow for backwards-compatiblity.