This is my own interpretation of the well known getopt functionality.
And now there's also a tiny alternative version.
Note
The regular/big version has at least one bug. And my tiny alternative version is maintained a bit more frequently (currently I'm not in the mood of fixing the regular version).
Tip
So the tiny alternative version (which got it's own polyfill, so it should
run 'as is'..) got the latest feature of supporting multiple radix (if parsing/etc. of paramters is
enabled), like --perm '(8)1777' or --int '(16)ffff'. ;-)
- [2026-05-24] New updates to the
getopt.tiny.js, w/type.js; ... - [2026-03-02] Updated the
getopt.tiny.js
getopt.js(252 code lines, in v0.4.0)polyfill.js(321 code lines) if you don't use my librarygetopt.tiny.js(updated 2026-05-24, w/ 588 code lines in total);test.js(tiny test, jfyi..);type.js(more type check features for the tiny version);
Just for your info: Both versions stop parsing the rest of the command line after the
occurence of an 'empty' -- parameter. That's a (good) convention.
Another info: with enabled _cast also the regular parameters (not only those with the
-- prefix, or rather (only) their values) are being converted (e.g. to Numbers).
The tiny alternative version doesn't have most of the features of the regular version. But works great as well.
There are not so many features, but the main difference is that you don't define a 'vector' with all possible/allowed
parameters, so any -- prefixed parameter will be interpreted as valid key. So handling this version is
also much easier and quicker.
The getopt.tiny.js does not need an extra polyfill (at least until now.. I don't really know now);
Everything's included on the file's bottom.
Tip
Since v2.4.0 it also supports different radix/base, e.g. --perm '(8)1777'
or --int '(16)ffff'. ;-) Well, it's really untested, but should work well..
The newest version also supports C unescaping now. So --separator \\n or --separator '\n'
will unescape the string to interprete with a newline character \n.
Since v2.0.0 the keys are stored with -- prefix, to prevent collisions with the
.prototype implementation of the resulting Array (which is preferred over
Object.create(null) because that's the best way to also store the complete (filtered)
command line).
With v2.1.0 there's also an interface (extends Array) to manage this thing better.
And since v2.2.0 assigning via = char (so e.g. --key=value) will directly replace
the whole value with the newest one, even when it's set to count multiple occurences of
the same key into an array (see parameter/const _array = DEFAULT_ARRAY).
These new features are barely tested...
You all know the getopt feature, either in shells or in C and much more. Nearly every language will have an implementation of it.
Since I really love to implement any feature I need for myself, I also created this implementation. Maybe you like it, or my way of handling it? :-)
I'm not sure if the following list is really complete. But most things are encountered here:
- Efficient design using also
MapandSet, resulting in better performance (even if not really much more here, since we're using relatively small amounts) - Keys etc. are defined in a special 'vector', which is an
Objectfor the maingetopt()function - The resulting object after parsing the/a command line (or list) is an array with: .. (a) regular elements pushed to it; .. (b) known keys by their vector indices
- The vector keys are those to be addressed when using the parse result, but they may(!) contain different { long, short }, etc.
- It's possible to enqueue multiple parameters consecutive, after which the values are collectable in order (so
-ab eins zweior--one --two eins zwei) - Multiple short parameters with only one
-prefix are possible (e.g.-abcwill be enforced (then) to-a -b -c, or-abc=defto-a=def -b=def -c=def) - Values after equal sign assignment can sometimes be an advantage (
--key=value); they also can encode lists, separated by,(escapable!) - All values can be parsed (so checking for Numbers, RegExp, Booleans, ..); also the normally pushed parameters (without key match), if wished
- If a single
--occures in the command line, the regular behavior is (usually) to abort the process and add the rest as regular list items - Undefined parameters or those without any value will result in an Integer which counts the amount of their occurences
- BUT if defaults are defined in the vector, these will be used (can be either for all, or separately one for undefined keys and one for empty keys, without values)
- Defaults can be optionally cloned.. and if
.params>1in a vector item plus an array as default value, the array items will be used adequately - By default multiple values are possible (use
.paramsvector item). - Use the
groupvector item to group all defined values together in the(result).group[]array (beneath their original(result).indexs) - It's possible to let short keys be found automatically (finding nearest possible character)
- Automatically created help pages/views (if no manuall
--help / -?override defined in the vector); using.helpvector items to show each ones descriptions - [2024-02-25] Update: now w/
-p8080, e.g.. so direct parameters on[short]items..
This is the exported function getopt(_vector, _parse, _parse_values, _assign, _assigned_list, _list = process.argv, _start = 0);
All other functions are not reachable above the exported getopt, I'm most times using anonymous (closure) functions for the rest.
These are the items your getopt vector (first argument to getopt(), is an object) supports.
| Key | Type(s) | Description |
|---|---|---|
long |
Boolean, String | The long key index, with -- double dash prefix in the command line |
short |
Boolean, String | Short key index, with - single dash prefix |
params |
Boolean, Integer | How many arguments per key index, or if at all any |
index |
Boolean, Integer | If multiple values are defined, in the end this setting will select one of the elements |
parse |
Boolean | Recongnizing RegExp, Numbers, etc.. |
assign |
Boolean | If '--key=value' are allowed (otherwise these ones will be only regular cmdline elements) |
list |
Boolean | If comma , in the values of = assignments (only there!) should create array elements (if not escaped) |
group |
String | All results will be referenced in a (result)[GROUP] array, too.. out of possibly more than one key/index |
default |
Array, * | Default value(s) for unspecified parameters (if .params > 0!): will set BOTH null and undefined |
null |
Array, * | If a parameter is given by it's index keys, but no value for it defined |
undefined |
Array, * | If a parameter is not given by index keys at all; so no --key, not only a missing value |
clone |
Boolean, Integer | The both default values can optionally be cloned every time (Integers are still TODO) |
help |
String | Automatically created help pages/views use this for the switches' descriptions |
Just some const DEFAULT_* on top of the file. The DEFAULT_EXPAND is the most important one, because it is a global setting,
no default value for the getopt vector.. the rest is just being used if it's vector items are not properly set-up.
| Name | Description |
|---|---|
DEFAULT_EXPAND |
Multiple shorts in one dash - item (-abc to -a -b -c or -abc=def to -a=def -b=def -c=def) |
DEFAULT_GROUPS |
Possibility to globally disable any group feature (but why should you?) |
DEFAULT_PARSE |
If no .parse vector item defined, this will be the default (list parsing will have the same value) |
DEFAULT_ASSIGN |
Default behavior for = assignments |
DEFAULT_ASSIGN_LIST |
Default setting whether to enable assigned (only!) lists, separated by , (escpable) |
DEFAULT_CLONE |
The default setting for cloning default elements (or not, or the depth) |
DEFAULT_HELP |
If --help / -? should be inserted automatically, if not already present |
DEFAULT_HELP_INDENT |
Base indention for the help view |
DEFAULT_HELP_INDENT_PREFIX |
Prefix for the real [help] strings |
DEFAULT_HELP_INDENT_DOUBLE |
More indention for more types of (help) parameters |
DEFAULT_HELP_INDENT_KEYS |
And the last indention (all above and this are integers (>=0) |
If enabled, arguments like -abc (so shorts!) are expanded to -a -b -c, or with assignment -abc=def to -a=def -b=def -c=def.
Warning If enabled, you can't use strings with
.length > 1, so only single chars! Otherwise also possible 'd be-short.
I implemented it with the help of my own JavaScript Library (all my own, just from scratch).
The library is originally the libjs.de, but this one was replaced by a newer,
more compact version which I'm using at kekse.biz: this is the GitHub link
to my v4 repository; also visible (with rendered .md) on my
kekse.biz/projects page.
Warning If using this script stand-alone, without my library being loaded/used, you should also import the
polyfill.js. I hope this file is complete, I neither tested any case, nor did I scrolled through thegetopt.js.. xD~
The whole base getopt() function is being exported under the global namespace! So you just have to getopt(vector, ...).
This only holds for the regular, big version. The tiny alternative version should have no bugs at all..
- ONE atm: multiple
[params]are not being collected (but the first only).. even though they disappear from the list of regular cmd-line-arguments.. :-/
Maybe also interesting for you: the config.js? And a reference to my documentation for this getopt.js:
The documentation (located in my v4 repository) is also still TODO.
The Copyright is (c) Sebastian Kucharczyk, and it's licensed under the MIT (also known as 'X' or 'X11' license).