argparseware middleware library
Project description
argparseware
The argparseware library is a simple to use, extensible library that complements the
argparse
package from the standard library. It provides the same interfaces - with
some additions - allowing developers to work quickly with tools they already know and
are well documented, while promoting code reusability.
More specifically, argparseware extends the default argument parser of argparse
by providing a quick and clean interface to define and use middleware components for
command-line applications, for such things as logging, loading configurations or
serving WSGI applications.
Its interface is reliant on argparse
with only some minor additions to improve
code reusability.
Getting started
Requirements
These are the general requirements for using this package:
- Python 3.6 or higher
No other dependencies are required for the base package and interfaces to work.
Optional dependencies
Some bundled middleware require optional dependencies:
ConfigMiddleware
requiresanyconfig
FlaskServerMiddleware
requiresflask
GunicornServerMiddleware
requiresgunicorn
GeventServerMiddleware
requiresgevent
Installation
Since this is a standard Python package, install with:
pip install argparseware
Development mode
Again, as this is a standard Python package, install in development (editable) mode with:
pip install -e .
Usage
This package can be used the same way as you would argparse
:
import argparseware
parser = argparseware.ArgumentParser(
prog='myprog',
description='Some test program',
)
parser.add_argument('--arg', help='some arg')
namespace = parser.parse_args()
...then run with:
python3 your-script.py --arg foo
What is middleware?
Where argparseware
really shines is by using the bundled or custom middleware. Middleware
have two phases: execution and configuration.
Execution is the step that is executed after all arguments have been parsed. It is essentially
a function that accepts the Namespace
object from argparse as its first argument and does
something with it:
parser = argparseware.ArgumentParser()
parser.add_argument('--some-arg', 'some generic argument')
@parser.middleware
def my_middleware(args):
print('some_arg value is:', args.some_arg)
parser.run()
It's useful for executing and reusing code that is run after the arguments are parsed.
While it's pretty useful in itself, this is an argparse extension, so you probably will want to be able to define your own arguments:
parser = argparseware.ArgumentParser()
@parser.middleware
def my_middleware(args):
print('some_arg value is:', args.some_arg)
@my_middleware.configure
def config_my_middleware(parser):
parser.add_argument('--some-arg', help='some arg as a string')
parser.run()
The configure
step is executed before the arguments are parsed, so this is where you'll
want to add your custom arguments or perform validation on other defined arguments.
This is the easiest form of middleware definition, but it doesn't stop there! Keep reading for more useful ways of using argparseware.
Loading middleware
If you already have existing middleware in a common library, or if you want to use some of the bundled middleware, for example, the logging middleware:
import argparseware
from argparseware.common import LoggingMiddleware
parser = argparseware.ArgumentParser()
parser.add_middleware(LoggingMiddleware())
parser.run()
...then run with:
python3 your-script.py --verbose
The above will automatically configure logging after the arguments are parsed.
Defining middleware
The easiest way to define your own reusable middleware component is to use the
middleware
decorator:
import argparseware
@argparseware.middleware
def my_middleware(args):
print('some_arg value is "foo":', args.some_arg == 'foo')
@my_middleware.configure
def config_my_middleware(parser):
parser.add_argument('--some-arg', 'some argument, try value: foo')
parser = argparseware.ArgumentParser(prog='testprog')
parser.add_middleware(my_middleware)
parser.run()
Complex middleware
With some middleware, you'll want to be able to customize it and pass arguments
to it. This can be done with the IMiddleware
abstract class:
from argparseware.core import IMiddleware
class MyMiddleware(IMiddleware):
def __init__(self, default_value):
self.default_value = default_value
def configure(self, parser):
parser.add_argument('--some-arg', default=self.default, help='some arg')
def run(self, args):
print('you passed', args.some_arg, 'default was', self.default_value)
parser = argparseware.ArgumentParser()
parser.add_middleware(MyMiddleware(42))
parser.run()
Adapting existing codebases
While it's great to have code reuse, sometimes you want the best of both worlds. In
argparseware, the parser's run
method returns the same thing as the argparse
parse_args
method would, so you can easily adapt existing code:
import argparseware
from argparseware.common import LoggingMiddleware
parser = argparseware.ArgumentParser()
parser.add_argument('--test')
parser.add_middleware(LoggingMiddleware())
args = parser.run()
if args.test == 'hello world':
print('hello world')
License
This code and its documentation is provided under the MIT License, bundled as the LICENSE
file. All original rights are reserved to Relevance.io 2020-.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file argparseware-0.9.9.tar.gz
.
File metadata
- Download URL: argparseware-0.9.9.tar.gz
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0316f62abfc14af4f8c19131c4fb833bd24eb1c21a43e8decb09e39f9139ef02 |
|
MD5 | 7f52c969977fbc3d6b3a978054f8761b |
|
BLAKE2b-256 | c065d901ada3aaab44a06558962f4437e0ff341e608eecf3055b1f0341ecbba6 |
File details
Details for the file argparseware-0.9.9-py3-none-any.whl
.
File metadata
- Download URL: argparseware-0.9.9-py3-none-any.whl
- Upload date:
- Size: 22.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b47596aec493f1a4ff781f53fdb91fcd6a5cb90019e756fd5e4ac9b517717f9e |
|
MD5 | f62912ab3f1b7ffc7efec56a9fb01aef |
|
BLAKE2b-256 | 51caf05ada4ffefe329a3d7798ac0e20c0e4f7f7101c921e6f81f585412482d1 |