Skip to main content

A simplified interface for your main function.

Project description

Pymain - Simplified main

Pymain is a decorator and related tools to simplify your main function(s). It is intended to be more simple to use and understand than argparse, while still providing most of the functionality of similar libraries.

Description

The basic idea of pymain is that your main function (though it doesn’t need to be called “main”), and therefore your script or application itself, probably takes parameters and keyword arguments in the form of command line arguments. Since that interface works very similar to calling a python function, pymain translates between those interfaces for you. In addition, so many scripts with entry points include the if __name__ == '__main__': boilerplate, and pymain aims to eliminate that.

Usage

Import and use the @pymain decorator before your main function that has type annotations for the parameters. If you don’t need any short options or aliases, that is all you need to do. Pymain will detect whether the defining module is run as a script (and therefore __name__ == "__main__") or if it is being imported. If it is run as a script, then main will be called and given arguments based on sys.argv. If it is imported, then pymain will not run the function as a main function and it can still be called normally.

Pymain uses the type annotations to determine what types to expect. For short options or aliases, you can add an @alias decorator after the @pymain decorator describing the alias (either a single alias or a dictionary of multiple)

All arguments that are greater than one character in length are long options (e.g. –arg), and arguments that have a single character are short options (e.g. -a). Aliases follow the same rules.

Examples

optional.py:

from pymain import pymain

@pymain
def main(a: float, b: float, c: str = None):
    print(a / b)
    if c is not None:
        print(c)

Command line:

~ $ python optional.py 4 2
2.0
~ $ python optional.py 9 2 message
4.5
message

keyword.py:

from pymain import pymain

@pymain
def main(first: int, second: int, *, message: str = None):
    print(first + second)
    if message is not None:
        print(message)

Command line:

~ $ python main.py 4 6
10
~ $ python main.py 1 2 --message "Hello, World!"
3
Hello, World!

alias.py:

from pymain import pymain, alias

@pymain
@alias({"opt1": "x", "opt2": "y"})
def foo(value: float, *, opt1: float = 1.0, opt2: float = 2.0):
    print(value + opt1)
    print(value - opt2)

Command line:

~ $ python alias.py 2
3.0
0.0
~ $ python alias.py 5 -x 1 -y 1
6.0
4.0
~ $ python alias.py 10 --opt1 5 --opt2 2
15.0
8.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymain-0.3.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

pymain-0.3.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pymain-0.3.1.tar.gz.

File metadata

  • Download URL: pymain-0.3.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pymain-0.3.1.tar.gz
Algorithm Hash digest
SHA256 956f7549f189d2b6a59684a37e66a7c6e2ad6fb04ed9e44329159af592bf4889
MD5 d8cea7f50d6669d9f3731edb08f9c961
BLAKE2b-256 1e9056f05ea1855ba1c7bce903111c3e0c54e4f57fa4e87ed6ce9e6b53a9a3b2

See more details on using hashes here.

File details

Details for the file pymain-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pymain-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a53c3807371ee956b57893df2e2946b251fdf04f6168c182ad17f3bcfcb88a1
MD5 836050de98b8be3be1ae8a7358a48770
BLAKE2b-256 b0d74e6afeca23e24e87279289e1386e4bdf0d2dd8879470d738114877677c77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page