Skip to main content

oppapī

Ergonomic option parser on top of dataclasses, inspired by structopt.

Usage

from typing import Optional
from oppapi import from_args, oppapi

@oppapi
class Opt:
    """
    Option parser using oppapi
    """

    host: str
    """ This will be positional argument of type `str` """

    port: Optional[int] = 8000
    """ Optional argument will be option argument """

opt = from_args(Opt)
print(opt)

The code above generates such option parser that

  • Generates parser description from class's docstring
  • Generates argument description from field's docstring
  • A field will be a positional argument
  • An optional field will be an optional argument

See the parser help message:

$ python simple.py -h
usage: simple.py [-h] [-p PORT] host

Option parser using oppapi

positional arguments:
  host                  This will be positional argument of type `str`

optional arguments:
  -h, --help            show this help message and exit
  -p PORT, --port PORT  Optional argument will be option argument

Running the program deserializes the command line arguments into an object of the declared class.

$ python simple.py localhost -p 20000
Opt(host='localhost', port=20000)

Supported types

short/long

oppapi generates flag names automatically, but you can specify arbitrary short/long names.

from typing import Optional
from oppapi import from_args, oppapi, field

@oppapi
class Opt:
    host: Optional[str] = field(short="-n", long="--hostname")

enum

enum.Enum and enum.IntEnum will be an argument with choices parameter.

class Food(Enum):
    A = "Apple"
    B = "Beer"
    C = "Chocolate"

class Price(IntEnum):
    A = 10
    B = 20
    C = 30

@oppapi
class Opt:
    food: Food
    price: Optional[Price]

usage will be like this:

positional arguments:
  {Apple,Beer,Chocolate}

optional arguments:
  -h, --help            show this help message and exit
  -p {10,20,30}, --price {10,20,30}

oppapi converts the command line arguments back to Enum.

$ python choice.py Apple --price 20
Opt(food=<Food.A: 'Apple'>, price=<Price.B: 20>)

List/Tuple

List will be an arbitrary number of arguments (nargs="+"). Tuple will be a fixed number of arguments (nargs=NUM).

@oppapi
class Opt:
    values: List[int]
    opts: Optional[Tuple[int, str, float, bool]]
$ python nargs.py 1 2 3 --opts 10 foo 10.0 True
Opt(values=[1, 2, 3], opts=(10, 'foo', 10.0, True))

SubCommand

Union of dataclasses will be subcommands.

@oppapi
class Foo:
    a: int

@oppapi
class Bar:
    a: str
    b: Optional[int]

@oppapi
class Opt:
    sub: Union[Foo, Bar]
usage: subcommand.py [-h] {foo,bar} ...

positional arguments:
  {foo,bar}

  optional arguments:
    -h, --help  show this help message and exit

Flatten

TODO

LICENSE

This project is licensed under the MIT license

Metadata

Release files for oppapi 0.0.6

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

Source distribution (sdist)

Source distribution for oppapi 0.0.6
File Size Uploaded
oppapi-0.0.6.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for oppapi 0.0.6
File Interpreter ABI Platform
oppapi-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 12.7 kB

Release files / oppapi-0.0.6.tar.gz

Download URL oppapi-0.0.6.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e1fe8fc16e2528910751a4da1a62f6d5c8d9b07469ad5ad7396473ba9932434e
BLAKE2b-256 checksum
How to use checksums
a5e3908c4e2a809f51a2d4c263af16c736a520e19c8a98b0ceb99cd1d1eb72a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.9.13 Darwin/21.6.0

Release files / oppapi-0.0.6-py3-none-any.whl

Download URL oppapi-0.0.6-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d3dd469c144ea636b1ac714e0e5393b1577d274b57d61dd79ba8f41be08a88a
BLAKE2b-256 checksum
How to use checksums
b76be40370a1887d9f7a73ea62f53bbcefa5a7e98437a29de3697847502c8463
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.9.13 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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