Argument Parser

Standard command-line argument parser for prismalog logging system.

This module provides a standardized way to add prismalog-related command line arguments to any application using the package. It enables consistent handling of logging configuration options across different applications and scripts.

Features:

  • Standardized logging arguments for all prismalog applications

  • Support for configuration via command line or config file

  • Automatic mapping between CLI args and LoggingConfig settings

  • Integration with Python’s argparse module

  • Consistent argument naming conventions

Available Arguments:

--log-config

Path to a YAML configuration file

--log-level

Set the default logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)

--log-dir

Directory where log files will be stored

--log-format

Format string for log messages

--log-datefmt

Format string for log timestamps

--log-filename

Base filename prefix for log files

--no-color

Disable colored console output

--disable-rotation

Disable log file rotation

--exit-on-critical

Exit program on critical errors

--rotation-size

Log file rotation size in MB

--backup-count

Number of backup log files to keep

Usage Examples:

  1. Basic usage:

    from prismalog.argparser import get_argument_parser, extract_logging_args
    from prismalog.log import LoggingConfig
    
    # Create parser with standard logging arguments
    parser = get_argument_parser(description="My Application")
    
    # Add own application-specific arguments
    parser.add_argument("--my-option", help="Application-specific option")
    
    # Parse arguments
    args = parser.parse_args()
    
    # Extract and apply logging configuration
    logging_args = extract_logging_args(args)
    LoggingConfig.from_dict(logging_args)
    
  2. Adding to an existing parser:

    import argparse
    from prismalog.argparser import add_logging_arguments, extract_logging_args
    
    # Create your own parser
    parser = argparse.ArgumentParser(description="My Application")
    parser.add_argument("--my-option", help="Application-specific option")
    
    # Add standard logging arguments
    add_logging_arguments(parser)
    
    # Parse and extract
    args = parser.parse_args()
    logging_args = extract_logging_args(args)
    
class prismalog.argparser.LoggingArgumentParser[source]

Bases: object

Helper class for adding standard logging arguments to argparse.

static add_arguments(parser=None)[source]

Add standard prismalog arguments to an existing parser using LoggingConfig.

Parameters:

parser (ArgumentParser | None) – An existing ArgumentParser instance. If None, a new one is created.

Returns:

The ArgumentParser with prismalog arguments added

Return type:

ArgumentParser

static create_parser(description=None)[source]

Create a new ArgumentParser with prismalog arguments.

Parameters:

description (str | None) – Description for the ArgumentParser

Returns:

A new ArgumentParser with prismalog arguments

Return type:

ArgumentParser

static extract_logging_args(args)[source]

Extract logging-related arguments from parsed args based on LoggingConfig defaults.

Parameters:

args (Namespace) – The parsed args from ArgumentParser.parse_args()

Returns:

Dictionary with only the logging-related arguments mapped to LoggingConfig keys.

Return type:

Dict[str, Any]

prismalog.argparser.get_argument_parser(description=None)[source]

Create a new ArgumentParser with prismalog arguments.

Parameters:

description (str | None) – Description for the ArgumentParser

Returns:

A new ArgumentParser with prismalog arguments

Return type:

ArgumentParser

prismalog.argparser.add_logging_arguments(parser)[source]

Add standard prismalog arguments to an existing parser.

Parameters:

parser (ArgumentParser) – An existing ArgumentParser instance

Returns:

The ArgumentParser with prismalog arguments added

Return type:

ArgumentParser

prismalog.argparser.extract_logging_args(args)[source]

Extract logging-related arguments from parsed args.

Parameters:

args (Namespace) – The parsed args from ArgumentParser.parse_args()

Returns:

Dictionary with only the logging-related arguments

Return type:

Dict[str, Any]

Core Functions

prismalog.argparser.get_argument_parser(description=None)[source]

Create a new ArgumentParser with prismalog arguments.

Parameters:

description (str | None) – Description for the ArgumentParser

Returns:

A new ArgumentParser with prismalog arguments

Return type:

ArgumentParser

prismalog.argparser.extract_logging_args(args)[source]

Extract logging-related arguments from parsed args.

Parameters:

args (Namespace) – The parsed args from ArgumentParser.parse_args()

Returns:

Dictionary with only the logging-related arguments

Return type:

Dict[str, Any]