Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

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.


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.


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.


from pymain import pymain

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

Command line:

~ $ python 4 2
~ $ python 9 2 message

from pymain import pymain

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

Command line:

~ $ python 4 6
~ $ python 1 2 --message "Hello, World!"
Hello, World!

from pymain import pymain, alias

@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 2
~ $ python 5 -x 1 -y 1
~ $ python 10 --opt1 5 --opt2 2

Project details

Download files

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

Files for pymain, version 0.3.1
Filename, size File type Python version Upload date Hashes
Filename, size pymain-0.3.1-py3-none-any.whl (7.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size pymain-0.3.1.tar.gz (5.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page