Skip to main content

Argparse-based way of CLI argument handling boosted by YAML reusable configurations

Project description

configurable_argparse - Argparse-based way of CLI argument handling boosted by YAML reusable configurations

When writing a command-line Python script that needs to take many arguments, usability issues may arise:

  • Tediousness: Especially if used often with mostly the same arguments
  • Incompleteness: It's easy to miss arguments
  • RTFM fatigue: having to often refer to documentation takes away from actually using the software
  • Need to write execution scripts: Writing scripts with arguments is sensible but locks users into static configurations

Configurable argparse aims at offloading the "noise" (often used argument configurations) into "recipes" that can be reused, while retaining the ability to override what needs to be adapted (the "signal"), therefore giving the user an experience that allows to efficiently use the program.

Example

This simple example is there show how to use configurable argparse and how it makes using

With argparse

def get_args():
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '-t', '--nb_threads',
        type: int, default=4,
        help="Number of threads to use"
    )
    ...
    exclusive_group = parser.add_mutually_exclusive_group()
    exclusive_group.add_argument(
        '--verbose',
        action='store_true',
        help='Show all messages'
    )
    exclusive_group.add_argument(
        '--quiet',
        action='store_true',
        help='Show no messages'
    )
    parser.parse_args()

...

def main():
    args = get_args()

Usage example:

  • Every time: python example_program.py -t 4 --use_library=libabc --ignore=E123,E321 --add_src=/homes/user/documents/ --quality=0.65 --no_audio --quiet

In this example, if the user wants to change an argument from run to run, the difference is lost in the "noise".

With configurable_argparse

ARGPARSE_PARSER_ARGS = {"prog": "Example program"}
ARGPARSE_ARGUMENTS = [
    Argument(
        ('-t', '--nb_threads'),
        {
            'type': int,
            'default': 4,
            'help': "Number of threads to use"
        }
    ),
    ...
    ArgumentExclusiveGroup([
        Argument(
            '--verbose',
            {
                'action': 'store_true',
                'help': 'Show all messages'
            }
        ),
        Argument(
            '--quiet',
            {
                'action': 'store_true',
                'help': 'Show no messages'
            }
        )
    ])
]
...

def main():
    args = get_args(
        parser_args=ARGPARSE_PARSER_ARGS,
        arguments=ARGPARSE_ARGUMENTS,
        yaml_config_base=Path(__file__)
    )

Usage example:

  • First run: python example_program.py -t 4 --use_library=libabc --ignore=E123,E321 --add_src=/homes/user/documents/ --quality=0.65 --no_audio --quiet (save config as usual)
  • Subsequent runs: python example_program.py --use_config=usual --quality=0.55

In this example, if the user wants to change an argument from run to run, the difference is immediately apparent since the "noise" is abstracted away in a usual configuration.

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

Built Distribution

File details

Details for the file configurable_argparse_DavidRodriguezSoaresCUI-0.0.2.tar.gz.

File metadata

File hashes

Hashes for configurable_argparse_DavidRodriguezSoaresCUI-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c677ed05835d925b3d2ac7ada7bd02fee02adf08d0ed3e39f29c063636ec1e1a
MD5 41a108e8f5f6ae1532b7b6888f4a05eb
BLAKE2b-256 753ed98e85d01e2f3d88a20e44e46311c08652e07648c67d0c072e45a749ee47

See more details on using hashes here.

File details

Details for the file configurable_argparse_DavidRodriguezSoaresCUI-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for configurable_argparse_DavidRodriguezSoaresCUI-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5021c471e3183ee2695810fb77a6a57049a74a1ef9799b23eb317cb71cddb602
MD5 66f309d64be82366acf6222bc7ee106b
BLAKE2b-256 bd46ba6fb3a465140c71a34a2b61a6f2bdbbfb264bc7270eddbaa79b88857459

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