1
0
Fork 0

Adding upstream version 0.6.2.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-04-21 10:59:46 +02:00
parent e5d20de2e1
commit 271f3e2863
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
20 changed files with 2544 additions and 0 deletions

0
tests/__init__.py Normal file
View file

197
tests/examples.py Normal file
View file

@ -0,0 +1,197 @@
import argparse
import textwrap
from decli import cli
def main_example():
"""https://docs.python.org/3/library/argparse.html#example"""
data = {
"description": "Process some integers.",
"arguments": [
{
"name": "integers",
"metavar": "N",
"type": int,
"nargs": "+",
"help": "an integer for the accumulator",
},
{
"name": "--sum",
"dest": "accumulate",
"action": "store_const",
"const": sum,
"default": max,
"help": "sum the integers (default: find the max)",
},
],
}
parser = cli(data)
return parser
def complete_example():
data = {
"prog": "cz",
"formatter_class": argparse.RawDescriptionHelpFormatter,
"description": "The software does this and that",
"epilog": "This is the epilooogpoe ",
"arguments": [
{
"name": "--debug",
"action": "store_true",
"default": False,
"help": "use debug mode",
},
{
"name": ["-v", "--version"],
"action": "store_true",
"default": False,
"help": "get the installed version",
"group": "ops",
},
{"name": "--save", "group": "ops"},
],
"subcommands": {
"title": "main",
"description": "main commands",
"commands": [
{
"name": "all",
"help": "check every values is true",
"func": all,
},
{
"name": ["sum", "s"],
"help": "new project",
"func": sum,
"arguments": [
{
"name": "integers",
"metavar": "N",
"type": int,
"nargs": "+",
"help": "an integer for the accumulator",
},
{"name": "--name", "nargs": "?"},
],
},
],
},
}
parser = cli(data)
return parser
def name_or_flags():
"""https://docs.python.org/3/library/argparse.html#name-or-flags"""
data = {
"prog": "sti",
"arguments": [{"name": ["-f", "--foo"]}, {"name": "bar"}],
}
return data
def compose_clis_using_parents():
"""
Sometimes, several cli share a common set of arguments.
Rather than repeating the definitions of these arguments,
one or more parent clis with all the shared arguments can be passed
to parents= argument to cli.
https://docs.python.org/3/library/argparse.html#parents
"""
parent_foo_data = {
"add_help": False,
"arguments": [{"name": "--foo-parent", "type": int}],
}
parent_bar_data = {
"add_help": False,
"arguments": [{"name": "--bar-parent", "type": int}],
}
parent_foo_cli = cli(parent_foo_data)
parent_bar_cli = cli(parent_bar_data)
parents = [parent_foo_cli, parent_bar_cli]
return parents
def using_formatter_class():
"""https://docs.python.org/3/library/argparse.html#formatter-class"""
data = {
"prog": "PROG",
"formatter_class": argparse.RawDescriptionHelpFormatter,
"description": textwrap.dedent(
"""\
Please do not mess up this text!
--------------------------------
I have indented it
exactly the way
I want it
"""
),
}
return data
def prefix_chars():
data = {
"prog": "PROG",
"prefix_chars": "+",
"arguments": [{"name": ["+f", "++foo"]}, {"name": "++bar"}],
}
return data
def grouping_arguments():
data = {
"prog": "mycli",
"arguments": [
{
"name": "--new",
"help": "This does not belong to a group but its a long help",
},
{
"name": "--init",
"help": "This does not belong to a group but its a long help",
},
{
"name": "--run",
"group": "app",
"help": "This does not belong to a group",
},
{
"name": "--build",
"group": "app",
"help": "This does not belong to a group",
},
{
"name": ["--install", "--add"],
"group": "package",
"metavar": "package_name",
"nargs": "+",
"help": "This belongs to a group",
},
{
"name": "--remove",
"group": "package",
"help": "This belongs to a group",
},
{
"name": "--why",
"group": "package",
"help": "This belongs to a group",
},
],
}
return data
def exclusive_group():
data = {
"prog": "app",
"arguments": [
{"name": "--install", "exclusive_group": "ops"},
{"name": "--purge", "exclusive_group": "ops"},
],
}
return data

198
tests/test_decli.py Normal file
View file

@ -0,0 +1,198 @@
"""Most of argparse examples rebuilt with climp."""
import pytest
import argparse
from . import examples
from decli import cli
import unittest
class Test(unittest.TestCase):
def test_main_example_ok(self):
parser = examples.main_example()
args = parser.parse_args("1 2 3 4".split())
assert args.accumulate(args.integers) == 4
def test_main_example_sums_ok(self):
parser = examples.main_example()
args = parser.parse_args("1 2 3 4 --sum".split())
assert args.accumulate(args.integers) == 10
def test_main_example_fails(self):
parser = examples.main_example()
with pytest.raises(SystemExit):
args = parser.parse_args("a b c".split())
args.accumulate(args.integers)
def test_complete_example(self):
parser = examples.complete_example()
args = parser.parse_args("sum 1 2 3".split())
assert args.func(args.integers) == 6
def test_compose_clis_using_parents(self):
data = {"prog": "daddy", "arguments": [{"name": "something"}]}
parents = examples.compose_clis_using_parents()
parser = cli(data, parents=parents)
args = parser.parse_args(["--foo-parent", "2", "XXX"])
assert args.something == "XXX"
assert args.foo_parent == 2
def test_compose_clis_using_parents_and_arguments(self):
data = {"prog": "daddy", "arguments": [{"name": "--something"}]}
parents = examples.compose_clis_using_parents()
parser = cli(data, parents=parents)
args = parser.parse_args(["--something", "XXX"])
assert args.something == "XXX"
def test_prefix_chars(self):
data = examples.prefix_chars()
parser = cli(data)
args = parser.parse_args("+f X ++bar Y".split())
assert args.foo == "X"
assert args.bar == "Y"
def test_name_or_flags(self):
data = examples.name_or_flags()
parser = cli(data)
args = parser.parse_args(["HELLO"])
assert args.bar == "HELLO"
args = parser.parse_args(["BAR", "--foo", "FOO"])
assert args.bar == "BAR"
assert args.foo == "FOO"
def test_name_or_flags_fail(self):
data = examples.name_or_flags()
parser = cli(data)
with pytest.raises(SystemExit):
parser.parse_args(["--foo", "FOO"])
def test_cli_no_args(self):
data = {"prog": "daddy", "description": "helloo"}
parser = cli(data)
args = parser.parse_args([])
assert args.__dict__ == {}
def test_groups(self):
data = examples.grouping_arguments()
parser = cli(data)
help_result = parser.format_help()
assert "app" in help_result
assert "package" in help_result
def test_not_optional_arg_name_validation_fails(self):
data = {"arguments": [{"name": ["f", "foo"]}]}
with pytest.raises(ValueError):
cli(data)
def test_exclusive_group_ok(self):
data = {
"prog": "app",
"arguments": [
{
"name": "--install",
"action": "store_true",
"exclusive_group": "ops",
},
{
"name": "--purge",
"action": "store_true",
"exclusive_group": "ops",
},
],
}
parser = cli(data)
args = parser.parse_args(["--install"])
assert args.install is True
assert args.purge is False
def test_exclusive_group_fails_when_same_group_called(self):
data = {
"prog": "app",
"arguments": [
{
"name": "--install",
"action": "store_true",
"exclusive_group": "opas",
},
{
"name": "--purge",
"action": "store_true",
"exclusive_group": "opas",
},
],
}
parser = cli(data)
with pytest.raises(SystemExit):
parser.parse_args("--install --purge".split())
def test_exclusive_group_and_group_together_fail(self):
"""
Note:
Exclusive group requires at least one arg before adding groups
"""
data = {
"prog": "app",
"arguments": [
{
"name": "--install",
"action": "store_true",
"exclusive_group": "ops",
"group": "cmd",
},
{
"name": "--purge",
"action": "store_true",
"exclusive_group": "ops",
"group": "cmd",
},
{"name": "--fear", "exclusive_group": "ops"},
],
}
with pytest.raises(ValueError):
cli(data)
def test_subcommand_required(self):
data = {
"prog": "cz",
"description": (
"Commitizen is a cli tool to generate conventional commits.\n"
"For more information about the topic go to "
"https://conventionalcommits.org/"
),
"formatter_class": argparse.RawDescriptionHelpFormatter,
"arguments": [
{"name": "--debug", "action": "store_true", "help": "use debug mode"},
{
"name": ["-n", "--name"],
"help": "use the given commitizen (default: cz_conventional_commits)",
},
],
"subcommands": {
"title": "commands",
"required": True,
"commands": [
{
"name": ["init"],
"help": "init commitizen configuration",
}
],
},
}
parser = cli(data)
args = parser.parse_args(["-n", "cz_jira", "init"])
assert args.debug is False
assert args.name == "cz_jira"