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)
-hand--helptake no arguments (0).-oand--outputtake any argument (1).-pand--porttake an argument that, when passed to theis_portfunction, returns True.-vand--verbosetake an optional argument that, when passed to theis_intfunction, returns True. If no argument is specified, it defaults to1.
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 -cis equivalent to-abc). If the option takes a mandatory argument, the argument may appear with or without whitespaces (-o valueis equivalent to-ovalue). -
Long options. If the option takes a mandatory argument, the argument may appear with or without an equal sign (
--option valueis 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 be0if the option takes no argument, or1if it takes an argument. Instead of0or1, it may specify a validation function that returns1if the argument is valid, or0otherwise. 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 ingetopt.optarg.?: An invalid option. An error message has been printed togetopt.stderrand the option that caused the error is stored ingetopt.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 ingetopt.optarg.
The following variable names are available:
getopt.optind: The index of the nextargv.getopt.optopt: The last option processed.getopt.optarg: The argument to the last option.
License
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)
| 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.