Skip to main content

getopt-py

A classic getopt library for Python with long options and optional arguments support.

Motivation

For developers that find argparse too constraining.

Example

import getopts

...

getopt = getopts.getopts(sys.argv, {
    "h": 0         , "help"   : 0,
    "o": 1         , "output" : 1,
    "p": is_port   , "port"   : is_port,
    "v": [is_int,1], "verbose": [is_int,1]
})

for c in getopt:
    if   c in ("-")           : opts.files.append(getopt.optarg)
    elif c in ("h", "help")   : usage() ; sys.exit(0)
    elif c in ("o", "output") : opts.output  = getopt.optarg
    elif c in ("p", "port")   : opts.port    = int(getopt.optarg)
    elif c in ("v", "verbose"): opts.verbose = int(getopt.optarg)
    else: sys.exit(1)
  • -h and --help take no arguments (0).
  • -o and --output take any argument (1).
  • -p and --port take an argument that, when passed to the is_port function, returns True.
  • -v and --verbose take an optional argument that, when passed to the is_int function, returns True. If no argument is specified, it defaults to 1.

See example.py for a more complete example.

Features

The features are based on GNU getopt_long:

  • Short options. Options may be combined (-a -b -c is equivalent to -abc). If the option takes a mandatory argument, the argument may appear with or without whitespaces (-o value is equivalent to -ovalue).

  • Long options. If the option takes a mandatory argument, the argument may appear with or without an equal sign (--option value is equivalent to --option=value).

  • Optional arguments. If the option takes an optional argument, the argument must appear without a space after a short option (-ovalue) or with an equal sign after a long option (--option=value).

  • Options may appear in any order.

  • -- can be used to denote the end of options.

Installation

With pip:

pip install getopts

(note the plural 's')

With [dpm]:

dpm install https://github.com/markuskimius/getopt-py.git

Usage

getopts.getopts(argv, optdict)

All parameters are mandatory:

  • argv - The argument list (e.g., sys.argv)
  • optdict - A dictionary containing the valid options and a specification of whether they take an argument. The keys are the options. The value may be 0 if the option takes no argument, or 1 if it takes an argument. Instead of 0 or 1, it may specify a validation function that returns 1 if the argument is valid, or 0 otherwise. To specify the argument as optional, surround it in brackets (make it a list), with the optional second element specifying the default value.

The function returns an iterable object that, if evaluated, returns one of the following:

  • -: An optionless argument. The value of the argument is stored in getopt.optarg.
  • ?: An invalid option. An error message has been printed to getopt.stderr and the option that caused the error is stored in getopt.optopt.
  • All other values: A valid option. This value is also stored in getopt.optopt. If the option takes an argument, the value is stored in getopt.optarg.

The following variable names are available:

  • getopt.optind: The index of the next argv.
  • getopt.optopt: The last option processed.
  • getopt.optarg: The argument to the last option.

License

Apache 2.0

[dpm]: https://github.com/markuskimius/dpm

Metadata

Release files for getopts 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for getopts 1.0.5
File Size Uploaded Provenance
getopts-1.0.5.tar.gz 8.4 kB Without provenance Details

Release files / getopts-1.0.5.tar.gz

Download URL getopts-1.0.5.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0c7c0bbbaaa9ba0cdca49898b2e11cd5032fe8557ca1f6cc2d94bcc4c1d1eff8
BLAKE2b-256 checksum
How to use checksums
7114ab1b8a25be44d96bfc242dfd9689f455c1fc621a5e392c536e9f598df2fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.9

Security for getopts 1.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.5 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page