Skip to main content

Run a "main" function automatically with the function's parameters as command-line arguments

Project description

argstart

Lets you define a "main" function to be run automatically. If that function has arguments, they'll be turned into command-line arguments.

So you can do:

from argstart import start

@start
def main(in_path: str, out_path: str, timeout: int = 500):
    ...

Instead of:

from argparse import ArgumentParser

def main(in_path: str, out_path: str, timeout: int = 500):
    ...

...

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("in_path", type=str)
    parser.add_argument("out_path", type=str)
    parser.add_argument("-t", "--timeout", type=int, default=500)
    args = parser.parse_args()

    main(args.in_path, args.out_path, args.timeout)

More details

Mostly: just write the function as you normally would, and it should work as you expect.

Use python your_file.py --help for full generated usage, like with argparse.

If specified, type annotations will be used to determine the command-line argument type.

Parses common docstring formats using docstring-parser to add command and argument descriptions.

So that all your functions get defined first, the main function is called at the end of your script - not immediately when it's defined/decorated. This is the same as if you had if __name__ == "__main__" at the bottom.

Command-line --flags also get a short acronym you can use instead, like -f.

It's command-line convention that flags are optional and positional arguments are required, so this is how arguments are translated by default:

def main(one, two, foo=1, bar=2)
# command-line: example.py [-f FOO] [-b BAR] one two

But you could force required arguments to become required flags by making them keyword-only:

def main(one, *, two, foo=1, bar=2)
# command-line: example.py -t TWO [-f FOO] [-b BAR] one

Or force optional arguments to become optional positional arguments by making them positional-only:

def main(one, two, foo=1, /, bar=2)
# command-line: example.py [-b BAR] one two [foo]

Supports *args and **kwargs:

def main(foo, *bar)
# command-line: example.py foo [bar ...]

def main(foo, **bar)
# command-line: example.py [-b [BAR ...]] foo

Finally, booleans will by default create a --toggle-flag which don't require any value after:

def main(foo=False)
# command-line: example.py [-f]
# (meaning foo defaults to False when -f flag is not given)

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

argstart-0.0.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argstart-0.0.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file argstart-0.0.2.tar.gz.

File metadata

  • Download URL: argstart-0.0.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.0

File hashes

Hashes for argstart-0.0.2.tar.gz
Algorithm Hash digest
SHA256 37f59ba9949561bc2cdbd87d4890bc203605a8572da6c206a190b8972700280e
MD5 babea30526a08ca4831c271aadff2c3b
BLAKE2b-256 063994b12814738bcd977614c070ac7e9f9a5ed62c8de3f0c0b68b69c74494c6

See more details on using hashes here.

File details

Details for the file argstart-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: argstart-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.0

File hashes

Hashes for argstart-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ffc724704b20dbeb26c5f4c1026c24d7545692201e1962e01348fa425bd0f57d
MD5 c7aa865853e521913e5798fdc0a75205
BLAKE2b-256 c9c1bf48cdd49b7c7578ee53ce0a2e7bcc2e80bfd1529d870261c915e8bfa565

See more details on using hashes here.

Supported by

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