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": new_number}

parser = NetArgumentParser()
parser.add_argument("-x", type=int, required=True)
parser(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": {"new_number": 6}, "exception": "", "finished": 1}

Installation

pip install netargparse

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

Documentation

More documentation can be found in docs.

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-1.0.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

netargparse-1.0.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: netargparse-1.0.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for netargparse-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b8a01c592fca3f8cdd85379d3b1494b2f84745ec87307c3538604723c9bbb332
MD5 1091e5a9f1e491d66f837c5105f38270
BLAKE2b-256 a9a7e6c1a5be84fcbccfa5542cf27dffbfc471c1a83ce6036513c9965fc36dbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for netargparse-1.0.0.tar.gz:

Publisher: cd.yml on kvnglb/netargparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: netargparse-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for netargparse-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8054ca28fd939175abf9a95a64cc3208c406002556a0db9c313dc6b57a0aa287
MD5 cfbeeff4e23a90bc4891a39c6c2377a2
BLAKE2b-256 e51365d9185d4a15efb6030d05c4b2585019625d9b8b3f088b317e4f7ccb5ea3

See more details on using hashes here.

Provenance

The following attestation bundles were made for netargparse-1.0.0-py3-none-any.whl:

Publisher: cd.yml on kvnglb/netargparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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