powercli.methods

Methods influence the behavior of how flags are parsed.

Module Contents

Classes

Method

The base class for a method.

Normal

The default way of parsing an argument.

Count

Counts the amount of times the flag has been specified.

Repeat

Allows multiple presence of the flag.

Switch

Evaluates P when the value is present or A for absence.

API

class powercli.methods.Method

The base class for a method.

class powercli.methods.Normal

Bases: powercli.methods.Method

The default way of parsing an argument.

class powercli.methods.Count

Bases: powercli.methods.Method

Counts the amount of times the flag has been specified.

Examples

from powercli.args import Flag
from powercli.methods import Count
from powercli.static import Static

Flag(
    short="v",
    long="verbose",
    description="Enables verbosity up to 4 different levels"
    method=Count(
        lambda _, amount: amount in range(0, 5),  # restrict range
        default=Static(2)  # returned when absent
    )
)

Requirements

  • The flag does not take any values.

  • The flag does not have any default values.

validate_amount: collections.abc.Callable[[powercli.typedefs.Context[powercli.methods.Count.FV, powercli.methods.Count.PV], int], bool | str]

‘Static(…)’

default: powercli.typedefs.WithContext[powercli.methods.Count.FV, powercli.methods.Count.PV, int] | None

‘Static(…)’

class powercli.methods.Repeat

Bases: powercli.methods.Method

Allows multiple presence of the flag.

The returned [powercli.parser.ParsedFlag] contains its converted values within a list even if the flag does not take any values.

Examples

from pathlib import Path

from powercli.args import Flag
from powercli.methods import Repeat

Flag(
    short="W",
    long="warning",
    description="Displays warning messages",
    method=Repeat()
    values=[("PATH", Path)]
)

Requirements

  • The flag does not take any default values.

validate_amount: collections.abc.Callable[[powercli.typedefs.Context[powercli.methods.Repeat.FV, powercli.methods.Repeat.PV], int], bool | str]

‘Static(…)’

A function that validates the amount of repetitions.

class powercli.methods.Switch

Bases: powercli.methods.Method

Evaluates P when the value is present or A for absence.

Parameters

  • on_presence - The value evaluated when the flag is present.

  • on_absence - The value evaluated when the flag is absent.

Examples

from powercli.args import Flag
from powercli.methods import Switch
from powercli.utils import static

Flag(
    short="r",
    description="Walks the directory recursively",
    method=Switch.boolean()
)

Requirements

  • The flag does not take any values.

  • The flag does not take any default values.

on_presence: powercli.typedefs.WithContext[powercli.methods.Switch.FV, powercli.methods.Switch.PV, powercli.methods.Switch.P]

None

The value evaluated when the flag is present.

on_absence: powercli.typedefs.WithContext[powercli.methods.Switch.FV, powercli.methods.Switch.PV, powercli.methods.Switch.A]

None

The value evaluated when the flag is absent.

static boolean() powercli.methods.Switch[Any, Any, bool, bool]

A constructor for a commonly used switch that evaluates True on presence and False on absence.