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.3.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.3-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for argstart-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b776503f394a00a4491cb72fab1508dcb380b7eb7ca10a9fbd08cc6eed5843bd
MD5 36d9380bec531ba7add0a70b72dacbec
BLAKE2b-256 30cf9bfad2f705b014e8ee15b41836fdf2ad26e6c84a1a6a5424d2e54fcdf24a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for argstart-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 17c477b0e85ff69a946bb7bb23a25fe2cf477104271f3c2a91cdfbe329601124
MD5 99e6c49ae265277cf0746412dbaec84b
BLAKE2b-256 257c321adcea0a98f966e4c7d6921bb99b6e1e5ffc1165f8cbdcf5a01aeab75c

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