clap_python
clap_python.style contains all classes to construct a arg parser.
- class clap_python.App(name='__main__.py')[source]
Bases:
SubCommandBase 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:
- 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:
- 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:
- Args:
style: Style to use when coloring stdout.
- Returns:
Self.
- class clap_python.Arg(long_name, short_name='')[source]
Bases:
objectArgument 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:
- Args:
choices: Values that are valid.
- Returns:
Self
- default(value)[source]
Default value for arg. If specified argument is not required.
- Return type:
- Args:
value: Default value.
- Returns:
Self.
- help(text)[source]
Add help text to argument.
- Return type:
- Args:
text: Description what the arg does.
- Returns:
Self.
- multiple_values(value)[source]
Set if arg takes multiple values.
- Return type:
- 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:
- 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:
- Args:
value: True if argument takes value else False.
- Returns:
Self.
- validate(callable_)[source]
Set callable object to validate arg value.
- Return type:
- 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:
- Args:
text: Argument value name.
- Returns:
Self.
- value_parser(parser)[source]
Func to convert argument string into value.
- Return type:
- 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:
ExceptionException to raise when parser fail.
- class clap_python.MutuallyExclusiveGroup[source]
Bases:
objectGroup to define group where only one command can be called.
- arg(arg)[source]
Add arg to group.
- Return type:
- Args:
arg: Arg to add to mutually exclusive group.
- Raises:
ValueError: Unsupported data type.
- Returns:
Self.
- class clap_python.SubCommand(long_name, short_name='')[source]
Bases:
objectSub command.
- about(text)[source]
Description about subcommand or app.
- Return type:
- Args:
text: Description about command.
- Returns:
Self.
- arg_required_else_help(value)[source]
If True and no args are passed show help message.
- Return type:
- 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:
- Args:
label: Name of header.
- Returns:
Self.