def _default_definition(self) -> Definition: from cleo.io.inputs.option import Option definition = super()._default_definition definition.add_option( Option("--no-plugins", flag=True, description="Disables plugins.")) definition.add_option( Option("--no-cache", flag=True, description="Disables Poetry source caches.")) return definition
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
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
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
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
def _default_definition(self) -> "Definition": from cleo.io.inputs.option import Option definition = super()._default_definition definition.add_option( Option("--no-plugins", flag=True, description="Disables plugins.")) return definition
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
def option( long_name: str, short_name: Optional[str] = None, description: Optional[str] = None, flag: bool = True, value_required: bool = True, multiple: bool = False, default: Optional[Any] = None, ) -> Option: return Option( long_name, short_name, flag=flag, requires_value=value_required, is_list=multiple, description=description, default=default, )
def test_fail_if_shortcut_is_empty(): with pytest.raises(ValueException): Option("option", "")
def test_multiple_shortcuts(): opt = Option("option", "-o|oo|-ooo") assert "o|oo|ooo" == opt.shortcut
def test_dashed_shortcut(): opt = Option("option", "-o") assert "o" == opt.shortcut
def test_shortcut(): opt = Option("option", "o") assert "o" == opt.shortcut
def test_fail_if_wrong_default_value_for_list_option(): with pytest.raises(LogicException): Option("option", flag=False, is_list=True, default="default")
def _parse_option(cls, token: str) -> Option: """ Parse an option expression. """ description = "" validator = None if " : " in token: token, description = tuple(token.split(" : ", 2)) token = token.strip() description = description.strip() # Checking validator: matches = re.match(r"(.*)\((.*?)\)", token) if matches: token = matches.group(1).strip() validator = matches.group(2).strip() shortcut = None matches = re.split(r"\s*\|\s*", token, 2) if len(matches) > 1: shortcut = matches[0].lstrip("-") token = matches[1] else: token = token.lstrip("-") default = None flag = True requires_value = False is_list = False if token.endswith("=*"): flag = False is_list = True requires_value = True token = token.rstrip("=*") elif token.endswith("=?*"): flag = False is_list = True token = token.rstrip("=?*") elif token.endswith("=?"): flag = False token = token.rstrip("=?") elif token.endswith("="): flag = False requires_value = True token = token.rstrip("=") else: matches = re.match(r"(.+)(=[?*]*)(.+)", token) if matches: flag = False token = matches.group(1) operator = matches.group(2) default = matches.group(3) if operator == "=*": requires_value = True is_list = True elif operator == "=?*": is_list = True elif operator == "=?": requires_value = False elif operator == "=": requires_value = True return Option( token, shortcut, flag=flag, requires_value=requires_value, is_list=is_list, description=description, default=default, )
def test_dashed_name(): opt = Option("--option") assert "option" == opt.name
i = ArgvInput() assert ["foo"] == i._tokens def test_parse_arguments(): i = ArgvInput(["cli.py", "foo"]) i.bind(Definition([Argument("name")])) assert i.arguments == {"name": "foo"} @pytest.mark.parametrize( ["args", "options", "expected_options"], [ (["cli.py", "--foo"], [Option("--foo")], { "foo": True }), ( ["cli.py", "--foo=bar"], [Option("--foo", "-f", flag=False, requires_value=True)], { "foo": "bar" }, ), ( ["cli.py", "--foo", "bar"], [Option("--foo", "-f", flag=False, requires_value=True)], { "foo": "bar" },
def test_fail_if_default_value_provided_for_flag(): with pytest.raises(LogicException): Option("option", flag=True, default="default")
def test_fail_if_name_is_empty(): with pytest.raises(ValueException): Option("")
class ImportExternalRequirements(Command): name = "install-from-requirements" arguments = [ Argument(name="file", required=True, description="A direct path to the requirements.txt file."), ] options = [ Option(name="requirements-file", shortcut="-r", description="set file as an requirements.txt"), Option(name="pipfile", shortcut="-p", description="set file as an Pipfile.lock"), Option(name="conda", shortcut="-c", description="set file as an conda.yml"), Option( name="tox", shortcut="-t", description="set file as an tox", ), # Optioanl Option( name="dry-run", shortcut="-D", description= "If enabled, this will NOT install any packages, and will simply display the poetry command to install", ) ] def handle(self): settings = { "path": self.argument("file"), } absolute_path_to_file = pathlib.Path(settings['path']).absolute() if absolute_path_to_file.exists() == False: self.line_error( f"File path {absolute_path_to_file} does not exist.") return 1 try: parse_requirements_file = rparse.parse( open(absolute_path_to_file, 'r').read()) # type: typing.List[rparse.Requirement] except Exception as error: self.line_error(f"Requirements file is malformed: {error}") for requirement in parse_requirements_file: if len(requirement.specs) > 1: generated_version = "".join(requirement.specs[0]) for req in requirement.specs[1:]: generated_version += f",{''.join(req)}" else: generated_version = "".join(requirement.specs[0]) generated_command = f"poetry install {requirement.name}{generated_version}" if self.option("dry-run") == False: self.call("install", f"{requirement.name}{generated_version}") else: self.line(f"<b>{generated_command}</b>")