getopt.net
A port of getopt in pure C#.
|
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 š
There are several methods of installing and using getopt.net in your project.
install-package getopt.net-bsd
Note the -bsd
ending which shows the license used and not system requirements! getopt.net was already in use šdotnet add package getopt.net-bsd
Separate options
--help
-h
--config=/path/to/config
POSIX separator--config /path/to/config
GNU separator-c /path/to/config
POSIX separator-c/path/to/config
GNU extensionCompound options
--console, -C
--test, -t
--config, -c
-Ctc/path/to/config
Options with required or optional arguments go last!NOTE: It is possible to use the Windows argument separator (
:
) with all conventions.
To enable this, you must setAllowWindowsConventions
totrue
.
Separate options
/help
/h
/config=/path/to/config
POSIX separator/config /path/to/config
GNU separator/config:/path/to/config
Windows separator/c /path/to/config
POSIX separator/c/path/to/config
GNU extensionCompound options
/console, /C
/test, /t
/config, /c
/Ctc/path/to/config
Options with required or optional arguments go last!Separate options
-help
-h
-config=/path/to/config
POSIX separator-config /path/to/config
GNU separator-config:/path/to/config
Windows separator-c /path/to/config
POSIX separator-c/path/to/config
GNU extensionCompound options
-console, -C
-test, -t
-config, -c
-Ctc/path/to/config
Options with required or optional arguments go last!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:
:
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.
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
.
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.
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.
getopt.net can generate a help text for you, by simply calling getopt.GenerateHelpText()
.
The behaviour of the help text generator can be customised to suit your needs. By default, getopt.net will not output application name, version or copyright information. This must be provided with the HelpTextConfig
object.
If no application name is provided, getopt.net will attempt to read the application name from the Assembly.
Should this fail, getopt.net will identify your program as unknown
.
The default configuration outputs options and switches using the GNU/POSIX convention and doesn't print the conventions supported by your application.
Here's an example:
produces the following output:
For a more detailled description of using getopt.net, please consult the Wiki.
If you encounter a bug, please add a GitHub Issue and/or create a fork of the project and create a pull request.