Skip to main content

Add argparser argument based on object heuristics

Project description

This package provides automatic adding of arguments to an argparser. ArgumentParser based on a simple method naming convention.

Basic Usage

Write your class with methods you wish called from a command line starting with a given prefix. Create an ArgparserAdapter, passing your object as a constructor. Methods starting with a the specified prefix, do_ by default, will be added to an argparser via the register call as – arguments. After parsing, call_specified_methods will call methods specified on command. ArgparseAdapter will attempt to convert command line strings to appropriate types if Python type hints are provided.

Example

import argparse
from ipaddress import IPv4Address
from argparse_adapter.argparse_adapter import ArgparserAdapter

class Something:

    def do_seven(self)->int:
        print(7)
        return 7


    def do_double(self,x):
        """double a number"""
        print(2*int(x))

    def do_triple(self,x:int):
        print(3*x)

    def do_sum(self,x:int,y:int):
        """sum arguments"""
        print(x + y)

    def do_ipv4address(self,x:IPv4Address):
        print(x)


def main():
    something = Something()
    adapter = ArgparserAdapter(something)
    parser = argparse.ArgumentParser()
    adapter.register(parser)
    args = parser.parse_args()
    adapter.call_specified_methods(args)

Note the do_double will receive a string and must convert it to an integer. The type hint in do_triple ensures the argument will be an integer.

The resulting argument argparser help is:

usage: objecttest.py [-h]
                     (--double x | --ipv4address x | --seven | --sum x y | --triple x)

optional arguments:
  -h, --help       show this help message and exit
  --double x       double a number
  --ipv4address x
  --seven
  --sum x y        sum arguments
  --triple x

Docstrings, if present, become help arguments.

Advanced usage

When type conversion fails, the method

def param_conversion_exception(self, e: Exception, method_name: str, parameter_name: str, parameter_type: type,
                               value: str) -> Any:

is called. The default behavior is to raise a ValueError exception including the method and parameter names, the value passed and the original exception message. This method is provided for subclasses to override, if desired. An implementation should raise an Exception or return a suitable parameter for calling method_name.

Alternative packages

More complete packages are available for this purpose, such as Click. This implementation is intended to be simple, lightweight and easy to use.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

argparser_adapter-1.4-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file argparser_adapter-1.4-py3-none-any.whl.

File metadata

  • Download URL: argparser_adapter-1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.64.1 CPython/3.8.10

File hashes

Hashes for argparser_adapter-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 eee2391c623e6a125719796686ed92171c1e9725857f52864dfaea65386c01d7
MD5 2234123faa086051151da490bbb49ff6
BLAKE2b-256 d092200183ccaaf6814c40167393dfa5fc1278b4092fd65c242c7017c9463a3a

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