Package: argparseware
argparsewareTheargparsewarelibrary is a simple to use, extensible library that complements theargparsepackage 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,argparsewareextends the default argument parser ofargparseby 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 onargparsewith only some minor additions to improve
code reusability.Getting startedRequirementsThese are the general requirements for using this package:Python 3.6 or higherNo other dependencies are required for the base package and interfaces to work.Optional dependenciesSome bundled middleware require optional dependencies:ConfigMiddlewarerequiresanyconfigFlaskServerMiddlewarerequiresflaskGunicornServerMiddlewarerequiresgunicornGeventServerMiddlewarerequiresgeventInstallationSince this is a standard Python package, install with:pipinstallargparsewareDevelopment modeAgain, as this is a standard Python package, install in development (editable) mode with:pipinstall-e.UsageThis package can be used the same way as you wouldargparse:importargparsewareparser=argparseware.ArgumentParser(prog='myprog',description='Some test program',)parser.add_argument('--arg',help='some arg')namespace=parser.parse_args()...then run with:python3your-script.py--argfooWhat is middleware?Whereargparsewarereally 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 theNamespaceobject from argparse as its first argument and does
something with it:parser=argparseware.ArgumentParser()parser.add_argument('--some-arg','some generic argument')@parser.middlewaredefmy_middleware(args):print('some_arg value is:',args.some_arg)parser.run()It's useful for executing and reusing code that is runafterthe 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.middlewaredefmy_middleware(args):print('some_arg value is:',args.some_arg)@my_middleware.configuredefconfig_my_middleware(parser):parser.add_argument('--some-arg',help='some arg as a string')parser.run()Theconfigurestep is executedbeforethe 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 usingargparseware.Loading middlewareIf 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:importargparsewarefromargparseware.commonimportLoggingMiddlewareparser=argparseware.ArgumentParser()parser.add_middleware(LoggingMiddleware())parser.run()...then run with:python3your-script.py--verboseThe above will automatically configure logging after the arguments are parsed.Defining middlewareThe easiest way to define your own reusable middleware component is to use themiddlewaredecorator:importargparseware@argparseware.middlewaredefmy_middleware(args):print('some_arg value is "foo":',args.some_arg=='foo')@my_middleware.configuredefconfig_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 middlewareWith some middleware, you'll want to be able to customize it and pass arguments
to it. This can be done with theIMiddlewareabstract 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 codebasesWhile it's great to have code reuse, sometimes you want the best of both worlds. Inargparseware, the parser'srunmethod returns the same thing as the argparseparse_argsmethod would, so you can easily adapt existing code:importargparsewarefromargparseware.commonimportLoggingMiddlewareparser=argparseware.ArgumentParser()parser.add_argument('--test')parser.add_middleware(LoggingMiddleware())args=parser.run()ifargs.test=='hello world':print('hello world')LicenseThis code and its documentation is provided under the MIT License, bundled as theLICENSEfile. All original rights are reserved to Relevance.io 2020-.