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 -o ONE [-f FOO] [-b BAR] two

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 [-t TWO] foo bar [one]

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.1.tar.gz (4.6 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.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: argstart-0.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.0

File hashes

Hashes for argstart-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f0583f0c38febc7fbbbb6f8d25cd5f85b473f26feb7e6d47745a58ad1883594e
MD5 dca983d06d6027ea6ce7c5d6d63a3baa
BLAKE2b-256 35e65d83d6c442c9764d3f106b59df95c85fa230af4181c3baa89f322d49d0e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: argstart-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.0

File hashes

Hashes for argstart-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7fec4fd2e3ac2a950f5640867403595d4fb46c6c8bff98942715d1646446147d
MD5 6d45e49f7a14497057ab6be5f07e3b26
BLAKE2b-256 d12b9d973fb08cca9b832bffcfa6122493f63bb86c9df023dc87bf34b6e7cc63

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