Skip to main content

Enhance ArgumentParser with a TCP-based API for argument handling.

Project description

netargparse

A Python library that imbues the standard ArgumentParser with an API for the Python script.

This library is intended as a replacement for the ArgumentParser of the standard argparse library, providing an additional TCP based API for handling the arguments of the script.

A minimal example minimal.py with the ArgumentParser could be

from argparse import ArgumentParser

def add_one(args):
    new_number = args.x + 1
    print(new_number)
    return(new_number)

parser = ArgumentParser()
parser.add_argument("-x", type=int, required=True)
args = parser.parse_args()
add_one(args)

and running the script results in

$ python minimal.py -x 5
6

Replacing the ArgumentParser with the NetArgumentParser from this library:

from netargparse import NetArgumentParser

def add_one(args):
    new_number = args.x + 1
    print(new_number)
    return(new_number)

nap = NetArgumentParser()
nap.parser.add_argument("-x", type=int, required=True)
nap(add_one)

The script can now be run in two modes:

  • main - standalone, same behaviour as above
  • nap - enable the API

Main

All arguments must be passed from the CLI after the main argument.

$ python minimal.py main -x 5
6

Nap

nap makes the script listen on a port and wait for the arguments.

$ python minimal.py nap --port 7000 --http

It is then possible to run the main function of the script by sending an HTTP get request with url parameters as arguments.

For example visit http://localhost:7000/?&-x=5 with a browser and receive the script's return as json.

{"response": 6, "exception": "", "finished": 1}

The following arguments are accepted for the nap mode

$ python minimal.py nap --help
usage: minimal.py nap [-h] [-i IP] -p PORT [--http]

optional arguments:
  -h, --help            show this help message and exit
  -i IP, --ip IP        IP address where NetArgumentParser listens. Default is localhost.
  -p PORT, --port PORT  Port number where NetArgumentParser listens.
  --http                Use http get requests instead of plain tcp messages (requires flask).

Installation

For a minimal installation, where only plain TCP messages are used for communication, run

pip install netargparse

No additional libraries will be installed. All libraries used are part of The Python Standard Library.

If HTTP requests are to be used, run

pip install netargparse[http]

This will also install flask.

Documentation

More documentation can be found in docs.

TODO

  • Windows support

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

netargparse-0.1.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

netargparse-0.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file netargparse-0.1.0.tar.gz.

File metadata

  • Download URL: netargparse-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.18 Linux/5.14.0-427.37.1.el9_4.x86_64

File hashes

Hashes for netargparse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1ddcce5bed5f4c0e1f65342b05a27d4abe4d6f4bb61a6c084ab857a447e55e8c
MD5 6bef1cc3587b95429be6546c8e3c5d3a
BLAKE2b-256 75377a7531e9fc40213936b9cf21db1f4ad5f2b10cf49d87e27b55a28fb4ff1e

See more details on using hashes here.

File details

Details for the file netargparse-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: netargparse-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.18 Linux/5.14.0-427.37.1.el9_4.x86_64

File hashes

Hashes for netargparse-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c3c2f898cd6bd836570d29ebf2967111c8262800ade349f7c2d65ee6d0a8129
MD5 28a428a015d5ac1d12d4c827784efa3a
BLAKE2b-256 e9649fb692a642d0296b52ffbcbf87d87165f9fd9f5643ebedc36615db453bc1

See more details on using hashes here.

Supported by

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