Skip to main content

Argument parser and general Python front-end

Project description

Pyr

pyr [TARGET [OPT..] [ARG..]]

Pyr is an experimental Python front-end to replace "python file", "python -m module", and even "python -c code".

  • Pyr isolates Python (-I).
  • Pyr parses command lines consistently.
  • Pyr handles signals.

Quick Example

$ cat >example <<'END'
#!/usr/bin/env /path/to/pyr
import pyr

def main(opts, args):
    print(f"{opts = }")
    print(f"{args = }")
    pyr.print_info("info")
    pyr.print_error("message")
    return "exit"

if __name__ == "__main__":
    pyr.run(main)
END
$ chmod +x example
$ ./example -a -b1 --cc --dd= --ee=2 foo 'bar baz'
opts = [('a', None), ('b', '1'), ('cc', None), ('dd', ''), ('ee', '2')]
args = ['foo', 'bar baz']
example: info
example error: message
example error: exit

Options & Arguments

Options must come before all arguments. All options follow identical syntax in two flavors: short and long. Short options have a hyphen, a single-character name, and an optional value. Long options have two hyphens, a name, and optionally an equals with a possibly-empty value. Long options can distinguish an empty value from no value, eg. --empty= vs --no-value. An option with a single-character name can used as either a short or long option (eg. -nv, --n=v). Option names cannot contain ASCII punctuation other than "+-.:_", though punctuation should be used sparingly.

Arguments follow all options. The first argument either starts without a hyphen or is one of two special arguments: "-" (which is retained) or "--" (which is discarded).

Combining Short Options

The special "+" option combines short options. This is parsed consistently, so that "-+abc", "-a -b -c", and "--+=abc" are all equivalent and indistinguishable. This avoids the problem where combined options cannot be parsed without knowing which options take a value.

Signal Exits

By default, Python shows tracebacks for several signals, such as SIGINT and SIGPIPE. Instead, Pyr exits without traceback using an exit code of 128 plus the signal number, like several common Unix tools. Option --signal-tb will instead re-raise these exceptions to show tracebacks. Pyr includes signal management in module pyr.sigexcept.

Use doc/yes.py and doc/date.py to see differences with SIGPIPE:

$ pyr doc/yes.py | head -n1
y

$ yes | head -n1
y

$ python3 -c 'while True: print("y")' | head -n1
y
Traceback (most recent call last):
  File "<string>", line 1, in <module>
BrokenPipeError: [Errno 32] Broken pipe

$ pyr doc/date.py | true
[no output]

$ date | true
[no output]

$ python3 -c 'import datetime; print(datetime.datetime.now())' | true
Exception ignored while flushing sys.stdout:
BrokenPipeError: [Errno 32] Broken pipe

Use doc/sigint and doc/sleep.py to see differences with SIGINT:

$ doc/sigint pyr doc/sleep.py
[no output]

$ doc/sigint python3 -c 'import time; time.sleep(3)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
KeyboardInterrupt

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

nfte_pyr-0.6.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

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

nfte_pyr-0.6.0-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file nfte_pyr-0.6.0.tar.gz.

File metadata

  • Download URL: nfte_pyr-0.6.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.1

File hashes

Hashes for nfte_pyr-0.6.0.tar.gz
Algorithm Hash digest
SHA256 2d4f5624640d911549e7bcbd1163bc2e0454002c4b1a007030543caf7668e6db
MD5 6c7cd0aaa39d2983ec5c909eb0fbce1a
BLAKE2b-256 522aa90bcd8c247c2f00ea80d815459b5e6be918188344b8be68665c76ee22ab

See more details on using hashes here.

File details

Details for the file nfte_pyr-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: nfte_pyr-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.1

File hashes

Hashes for nfte_pyr-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bea371a7d586efa3dd1712110ca99ebf3d79cf8a8c0e5d13f98e3e7bca2aa694
MD5 8e93bcfef9eff800f0a531938ba256d6
BLAKE2b-256 68e529ace260bf934c3a0a9f4f267015e60370ecd663959acc16b155092da607

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