clap_python

clap_python.style contains all classes to construct a arg parser.

class clap_python.App(name='__main__.py')[source]

Bases: SubCommand

Base class to build cli.

Example:

from clap_python import App, Arg
args = (
    App()
    .arg(Arg("--hello"))
    .arg(Arg("--items").multiple_values(True))
    .parse_args()
)
parse_args(args=None)[source]

Parse known arguments. Any unknown arguments will stop execution.

Return type:

dict

Args:

args: List of arguments to parse (don’t include the app name).

Returns:

Parsed result as dict.

parse_known_args(args=None)[source]

Parse known and unknown arguments. Unknown arguments are separated by “–“.

Return type:

dict

Args:

args: List of arguments to parse (don’t include the app name).

Example:

from clap_python import App, Arg

args = (

App() .about(“run application”) .arg(Arg(“-c”).help(“name of application to run”)) .parse_known_args([“-c”, “nuke”, “–”, “-t”])

) print(args) {“c”: “nuke”, “unknown”: [“-t”]}

Returns:

Parsed result as dict.

style(style)[source]

Apply style to app.

Return type:

App

Args:

style: Style to use when coloring stdout.

Returns:

Self.

version(version_number)[source]

Set version number for app.

Return type:

App

Args:

version_number: App version number.

Returns:

Self.

width(value)[source]

Set max width of help text.

Return type:

App

Arg:

value: Max width.

Returns:

Self.

class clap_python.Arg(long_name, short_name='')[source]

Bases: object

Argument class to define cli arguments.

Example:

from clap_python import Arg, App

args = (
    App()
    .about("Cli to change color space on images.")
    .arg(Arg("images").multiple_values(True))
    .arg(
        Arg("--color-space")
        .help("Color space to convert to.")
        .choices(["Rec.709", "ACEScg"])
        .default("Rec.709")
    )
    .arg(Arg("--output-dir").help("Directory to export converted images to"))
    .arg(Arg("--fast").takes_value(False))
    .parse_args()
)
choices(choices)[source]

Specify values that are valid for the argument.

Return type:

Arg

Args:

choices: Values that are valid.

Returns:

Self

default(value)[source]

Default value for arg. If specified argument is not required.

Return type:

Arg

Args:

value: Default value.

Returns:

Self.

help(text)[source]

Add help text to argument.

Return type:

Arg

Args:

text: Description what the arg does.

Returns:

Self.

multiple_values(value)[source]

Set if arg takes multiple values.

Return type:

Arg

Args:
value:

If True arg can consume multiple values and result of arg is list.

Returns:

Self.

required(value)[source]

Set enabled and not passed parser will fail.

Return type:

Arg

Args:

value: If True and arg is missing parser will fail and exit.

Returns:

Self.

takes_value(value)[source]

Configure whether arg requires value to be passed.

Return type:

Arg

Args:

value: True if argument takes value else False.

Returns:

Self.

validate(callable_)[source]

Set callable object to validate arg value.

Return type:

Arg

Args:

callable_: Callable object that returns error message if failed.

Example:

from typing import Any from clap_python import Arg, App

def validate(value: str) -> str:
if value != “hello”:

return “Invalid arg… ‘hello’ is only allowed.”

return “”

args = App().arg(Arg(”–abc”).validate(validate)).parse_args()

Returns:

Self.

value_name(text)[source]

Set argument value name.

Return type:

Arg

Args:

text: Argument value name.

Returns:

Self.

value_parser(parser)[source]

Func to convert argument string into value.

Return type:

Arg

Args:

parser: Callable object to cast parsed value into new. Default is str.

Returns:

Self.

Example:

from clap_python import App, Arg
app = App().arg(Arg("--number").value_parser(int))
print(app.parse_args(["--number", "101"]))
{"number": 101}
exception clap_python.ClapPyException(msg, command)[source]

Bases: Exception

Exception to raise when parser fail.

class clap_python.MutuallyExclusiveGroup[source]

Bases: object

Group to define group where only one command can be called.

__iter__()[source]
arg(arg)[source]

Add arg to group.

Return type:

MutuallyExclusiveGroup

Args:

arg: Arg to add to mutually exclusive group.

Raises:

ValueError: Unsupported data type.

Returns:

Self.

help_heading(label)[source]

Lets you organize the help message visually by adding a header above related options.

Return type:

MutuallyExclusiveGroup

Args:

label: Name of header.

Returns:

Self.

required(value)[source]

Set enabled and not passed parser will fail.

Return type:

Arg

Args:

value: If True and arg is missing parser will fail and exit.

Returns:

Self.

class clap_python.SubCommand(long_name, short_name='')[source]

Bases: object

Sub command.

about(text)[source]

Description about subcommand or app.

Return type:

SubCommand

Args:

text: Description about command.

Returns:

Self.

arg(arg)[source]

Add arg to self.

Return type:

SubCommand

Args:

arg: Arg to add.

Returns:

Self.

arg_required_else_help(value)[source]

If True and no args are passed show help message.

Return type:

SubCommand

Args:

value: If True and no args provided show help message and exit.

Returns:

Self.

help_heading(label)[source]

Lets you organize the help message visually by adding a header above related options.

Return type:

SubCommand

Args:

label: Name of header.

Returns:

Self.

subcommand_required(value)[source]

If True and no subcommand passed show help message.

Return type:

SubCommand

Args:

value: If True and no subcommand provided show help message and exit.

Returns:

Self.

clap_python.color_text(text, style=0, color=37)[source]

Color text with ANSI Escape Code.

Return type:

str

Args:

text: Text to format. style: Text style. color: Color to set.

Returns:

Colored text.