Skip to main content

Manage python packages on Ubuntu20 systems

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 decorated with @CommandLine() or ChoiceCommand(choice) Create an ArgparserAdapter, passing your object as a constructor. Decorated methods will be added to an argparser via the register call as positional or options. 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.

Choice Options

Choice options are added by first creating a Choice instance and then adding a ChoiceCommand decorator for each class method that should be a choice. The method name becomes a choice option for the specified choice. The method docstring becomes part of the the help.

Choices may be positional or options, depending the value of the Choice is_position attribute. Default values may be supplied.

CommandLine Options

Arguments may be designed as required using @CommandLine(required=True). Default values may be specified with @CommandLine(default=10). Note specifying both required and a default is possible but not useful.


Logging is to: logging.getLogger(‘argparser_adapter’)


import argparse
from ipaddress import IPv4Address

from argparser_adapter import CommandLine, ArgparserAdapter, Choice, ChoiceCommand

petchoice = Choice("pet",False,default='cat',help="Pick your pet")
funchoice = Choice("fun",True,help="Pick your fun time")

class Something:

    def seven(self) -> int:
        # no help for this argument
        return 7

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

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

    def triple(self, x: int):
        """triple a value"""
        print(3 * int(x))

    def ipv4address(self, x: IPv4Address):
        """Print ip address"""

    def hello(self):

    def binary(self, value: bool):
        """True or false"""

    def morning(self,name:str='Truman'):
        """The sun has risen"""
        print(f"morning {name}!")

    def night(self):
        print("it's dark")

    def dog(self):

    def cat(self,name:str='Morris'):
        print(f"meow {name}")

def main():
    something = Something()
    adapter = ArgparserAdapter(something)
    #parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
    args = parser.parse_args()

if __name__ == "__main__":

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

The resulting argument argparser help is:

usage: [-h] [--binary value] [--double x] [--hello]
                   [--ipv4address x] [--seven] [--sum x y] [--triple x]
                   [--pet {cat,dog}]

positional arguments:
  {morning,night}  Pick your fun time
                   morning (The sun has risen)
                   night (dark)

optional arguments:
  -h, --help       show this help message and exit
  --binary value   True or false
  --double x       double a number
  --ipv4address x  Print ip address
  --sum x y        sum arguments
  --triple x       triple a value
  --pet {cat,dog}  Pick your pet
                   cat (feline)
                   dog (canine)

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 Distribution

argparser_adapter-2.2.3.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distribution

argparser_adapter-2.2.3-py3-none-any.whl (7.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page