Skip to main content

A Python library for parsing command-line args automagically.

Project description

arg-magiq

argmagiq is a Python library that allows for parsing command-line args automagically. To that end, all you have to do is to define a configuration class that contains a property for each of the user-defined args, and let argmagiq take care of parsing them for you

Installation

The package argmagiq can be installed via pip as follows:

pip install git+https://github.com/phohenecker/arg-magiq

How-To

Step 1: Define Your Config Class

Every property (not attribute) of your configuration class that has both a getter and a setter method is considered as an arg. Please notice that all args are treated as options, i.e., argmagiq does not generate positional args, and the name of a considered property my_property is translated into an according option --my-property. For example, the following code defines an arg --my-property:

@property
def my_property(self) -> str:
    """str: This property defines an arg."""
    return self._my_property

@my_property.setter
def my_property(self, my_property: str) -> None:
    self._my_property = my_property

An important detail of this code snippet is the return-type annotation of the getter method, which allows argmagiq to identify the data type of the according arg and sanitize values provided by the user. At the moment, the types bool, int, float, and str are supported. Notice that generic type-aliases are not allowed except for typing.Optional[X], if X is any of the supported types. Finally, notice that the docstring of the getter method, if present, is printed as description of the arg in the help text of the application.

Notice: Just like Python's argparse package, argmagiq prints an automatically generated help text, if either -h or --help is provided.

Step 2: Let argmagiq Parse Args

Once you defined your config class, parsing args is as easy as importing argmagiq and running a single command:

import argmagiq

parsed_config = argmagiq.parse_args(
        YourConfigClass,
        app_name,
        app_description
)

In this code snippet, app_name and app_description are two strings that define the name of your application, which is used in its synopsis (the usage instruction at the beginning of the help), as well as a description of the same, which is displayed as part of the help text. The return value of parse_args is an instance of YourConfigClass that has been populated with the values provided for the corresponding args.

Notice: When the help text is printed (i.e., the user provided -h or --help), then parse_args returns None.

Default Values

If an arg has a default value, then this has to be specified as an attribute of the config class. The name of such an attribute has to be an all-caps version of the according property prefixed with DEFAULT_. For example, the following snippet defines a default value for the property my_property (which, as before, defines the arg --my-property):

class YourConfigClass(object):

    DEFAULT_MY_PROPERTY = "blub"
    ...

By default, every arg without default value is considered as required, and an error will be raised, if the user does not specify the same. If you want to explicitly mark an arg without default value as optional, though, then you can annotate the according getter method with @argmagiq.optional:

@argmagiq.optional
@property
def my_property(self) -> str:
    ...

Reading Args From A JSON File

As an alternative way of specifying args, which is particularly handy, if an applications requires a lot of user-defined configuration, argmagiq allows for providing them as a JSON file as follows:

$ ./your-app.py -- /path/to/config.json

In this example, the file at path /path/to/config.json specifies the args for the application. Notice that JSON files have to be shallow dictionaries that describe key-value pairs using the same naming as the configuration class. For example:

{
    "my_property": "some value",
    ...
}

Examples

For full working examples, please have a look at the examples folder.

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

argmagiq-0.1.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

argmagiq-0.1.0-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

Details for the file argmagiq-0.1.0.tar.gz.

File metadata

  • Download URL: argmagiq-0.1.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for argmagiq-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ac96c89ca4b4b7d555ebf5f91dfbab88ba63e863933d855291e8940f622dcf4c
MD5 0e78762bd446878bf6fb9feb3e352df3
BLAKE2b-256 89f244d6687bf7ec68c3130dc0a0fc089e5461dfda1b1f16317896ec14da0b57

See more details on using hashes here.

File details

Details for the file argmagiq-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: argmagiq-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for argmagiq-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 74bd8c2b81e166ccc283f9a3141355f48b57314b0fe9248cbb6b0c4111148e88
MD5 076301f29851623e4240d05e9a75a1f0
BLAKE2b-256 675e6f6a7824b582561f5695d6251c06117ea4185f80614faa9c1c80beaa2da9

See more details on using hashes here.

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