예제 #1
0
def test_required_value_with_default():
    opt = Option("option", flag=False, requires_value=True, default="Default")

    assert not opt.is_flag()
    assert opt.accepts_value()
    assert opt.requires_value()
    assert not opt.is_list()
    assert "Default" == opt.default
예제 #2
0
def test_required_value():
    opt = Option("option", flag=False, requires_value=True)

    assert not opt.is_flag()
    assert opt.accepts_value()
    assert opt.requires_value()
    assert not opt.is_list()
    assert opt.default is None
예제 #3
0
def test_multi_valued_with_default():
    opt = Option("option", flag=False, is_list=True, default=["foo", "bar"])

    assert not opt.is_flag()
    assert opt.accepts_value()
    assert opt.requires_value()
    assert opt.is_list()
    assert ["foo", "bar"] == opt.default
예제 #4
0
def test_list():
    opt = Option("option", flag=False, is_list=True)

    assert not opt.is_flag()
    assert opt.accepts_value()
    assert opt.requires_value()
    assert opt.is_list()
    assert [] == opt.default
예제 #5
0
def test_create():
    opt = Option("option")

    assert "option" == opt.name
    assert opt.shortcut is None
    assert opt.is_flag()
    assert not opt.accepts_value()
    assert not opt.requires_value()
    assert not opt.is_list()
    assert not opt.default
예제 #6
0
    def _describe_option(self, option: Option, **options: Any) -> None:
        if (option.accepts_value() and option.default is not None
                and (not isinstance(option.default, list) or option.default)):
            default = "<comment> [default: {}]</comment>".format(
                self._format_default_value(option.default))
        else:
            default = ""

        value = ""
        if option.accepts_value():
            value = "=" + option.name.upper()

            if not option.requires_value():
                value = "[" + value + "]"

        total_width = options.get(
            "total_width", self._calculate_total_width_for_options([option]))

        synopsis = "{}{}".format(
            "-{}, ".format(option.shortcut) if option.shortcut else "    ",
            "--{}{}".format(option.name, value),
        )

        spacing_width = total_width - len(synopsis)

        self._write("  <c1>{}</c1>  {}{}{}{}".format(
            synopsis,
            " " * spacing_width,
            re.sub(
                r"\s*[\r\n]\s*",
                "\n" + " " * (total_width + 4),
                option.description,
            ),
            default,
            "<comment> (multiple values allowed)</comment>"
            if option.is_list() else "",
        ))