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}

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

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: netargparse-0.1.5.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/5.14.0-503.15.1.el9_5.x86_64

File hashes

Hashes for netargparse-0.1.5.tar.gz
Algorithm Hash digest
SHA256 fbaac42676ebab6ad8b173456990052c8cca27daddded03a1611ca5dd40b8fe7
MD5 b49fe31a5c9eb8b07113689cab6e1450
BLAKE2b-256 f4ddaf88f1948d3cf3cd58363dd31ea1c03e8ebe0ea2bd79125a9f9c14d22e40

See more details on using hashes here.

File details

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

File metadata

  • Download URL: netargparse-0.1.5-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.19 Linux/5.14.0-503.15.1.el9_5.x86_64

File hashes

Hashes for netargparse-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fa88ea06239171c204d9acf0b9ddc4ebb14e0ec6d979721021a29a1b0b6c963f
MD5 b8fccaf798eba2ed24bafd0dc25fcb6e
BLAKE2b-256 e62a62bad91776c8a6acdfc52592a44b54bb0ae28a94c02160c49ba334d5bd12

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