Skip to content

env_var

Class info

Classes

Name Children Inherits
MacOSEnvVarCommand
anyenv.os_commands.env_var
macOS environment variable command implementation.
    UnixEnvVarCommand
    anyenv.os_commands.env_var
    Unix/Linux environment variable command implementation.
      WindowsEnvVarCommand
      anyenv.os_commands.env_var
      Windows environment variable command implementation.

        🛈 DocStrings

        Environment variable command implementations for different operating systems.

        MacOSEnvVarCommand

        Bases: EnvVarCommand

        macOS environment variable command implementation.

        Source code in src/anyenv/os_commands/env_var.py
        class MacOSEnvVarCommand(EnvVarCommand):
            """macOS environment variable command implementation."""
        
            def create_command(self, name: str) -> str:
                """Generate printenv command (same as Unix).
        
                Args:
                    name: Name of environment variable
        
                Returns:
                    The printenv command string
                """
                return f"printenv {sh_quote(name)}"
        
            def parse_command(self, output: str, exit_code: int = 0) -> str | None:
                """Parse printenv result (same as Unix).
        
                Args:
                    output: Raw command output
                    exit_code: Command exit code
        
                Returns:
                    Variable value or None if not set
                """
                if exit_code != 0:
                    return None
                return output.rstrip("\n") if output else None
        

        create_command

        create_command(name: str) -> str
        

        Generate printenv command (same as Unix).

        Parameters:

        Name Type Description Default
        name str

        Name of environment variable

        required

        Returns:

        Type Description
        str

        The printenv command string

        Source code in src/anyenv/os_commands/env_var.py
        def create_command(self, name: str) -> str:
            """Generate printenv command (same as Unix).
        
            Args:
                name: Name of environment variable
        
            Returns:
                The printenv command string
            """
            return f"printenv {sh_quote(name)}"
        

        parse_command

        parse_command(output: str, exit_code: int = 0) -> str | None
        

        Parse printenv result (same as Unix).

        Parameters:

        Name Type Description Default
        output str

        Raw command output

        required
        exit_code int

        Command exit code

        0

        Returns:

        Type Description
        str | None

        Variable value or None if not set

        Source code in src/anyenv/os_commands/env_var.py
        def parse_command(self, output: str, exit_code: int = 0) -> str | None:
            """Parse printenv result (same as Unix).
        
            Args:
                output: Raw command output
                exit_code: Command exit code
        
            Returns:
                Variable value or None if not set
            """
            if exit_code != 0:
                return None
            return output.rstrip("\n") if output else None
        

        UnixEnvVarCommand

        Bases: EnvVarCommand

        Unix/Linux environment variable command implementation.

        Source code in src/anyenv/os_commands/env_var.py
        class UnixEnvVarCommand(EnvVarCommand):
            """Unix/Linux environment variable command implementation."""
        
            def create_command(self, name: str) -> str:
                """Generate Unix printenv command.
        
                Args:
                    name: Name of environment variable
        
                Returns:
                    The printenv command string
                """
                return f"printenv {sh_quote(name)}"
        
            def parse_command(self, output: str, exit_code: int = 0) -> str | None:
                """Parse Unix printenv result.
        
                Args:
                    output: Raw command output
                    exit_code: Command exit code
        
                Returns:
                    Variable value or None if not set
                """
                if exit_code != 0:
                    return None
                # printenv includes trailing newline
                return output.rstrip("\n") if output else None
        

        create_command

        create_command(name: str) -> str
        

        Generate Unix printenv command.

        Parameters:

        Name Type Description Default
        name str

        Name of environment variable

        required

        Returns:

        Type Description
        str

        The printenv command string

        Source code in src/anyenv/os_commands/env_var.py
        def create_command(self, name: str) -> str:
            """Generate Unix printenv command.
        
            Args:
                name: Name of environment variable
        
            Returns:
                The printenv command string
            """
            return f"printenv {sh_quote(name)}"
        

        parse_command

        parse_command(output: str, exit_code: int = 0) -> str | None
        

        Parse Unix printenv result.

        Parameters:

        Name Type Description Default
        output str

        Raw command output

        required
        exit_code int

        Command exit code

        0

        Returns:

        Type Description
        str | None

        Variable value or None if not set

        Source code in src/anyenv/os_commands/env_var.py
        def parse_command(self, output: str, exit_code: int = 0) -> str | None:
            """Parse Unix printenv result.
        
            Args:
                output: Raw command output
                exit_code: Command exit code
        
            Returns:
                Variable value or None if not set
            """
            if exit_code != 0:
                return None
            # printenv includes trailing newline
            return output.rstrip("\n") if output else None
        

        WindowsEnvVarCommand

        Bases: EnvVarCommand

        Windows environment variable command implementation.

        Source code in src/anyenv/os_commands/env_var.py
        class WindowsEnvVarCommand(EnvVarCommand):
            """Windows environment variable command implementation."""
        
            def create_command(self, name: str) -> str:
                """Generate PowerShell environment variable command.
        
                Args:
                    name: Name of environment variable
        
                Returns:
                    The PowerShell command string
                """
                return powershell(f"[Environment]::GetEnvironmentVariable({ps_literal(name)})")
        
            def parse_command(self, output: str, exit_code: int = 0) -> str | None:
                """Parse PowerShell environment variable result.
        
                Args:
                    output: Raw command output
                    exit_code: Command exit code
        
                Returns:
                    Variable value or None if not set
                """
                # PowerShell returns empty string for unset variables
                value = output.strip()
                return value if value else None
        

        create_command

        create_command(name: str) -> str
        

        Generate PowerShell environment variable command.

        Parameters:

        Name Type Description Default
        name str

        Name of environment variable

        required

        Returns:

        Type Description
        str

        The PowerShell command string

        Source code in src/anyenv/os_commands/env_var.py
        def create_command(self, name: str) -> str:
            """Generate PowerShell environment variable command.
        
            Args:
                name: Name of environment variable
        
            Returns:
                The PowerShell command string
            """
            return powershell(f"[Environment]::GetEnvironmentVariable({ps_literal(name)})")
        

        parse_command

        parse_command(output: str, exit_code: int = 0) -> str | None
        

        Parse PowerShell environment variable result.

        Parameters:

        Name Type Description Default
        output str

        Raw command output

        required
        exit_code int

        Command exit code

        0

        Returns:

        Type Description
        str | None

        Variable value or None if not set

        Source code in src/anyenv/os_commands/env_var.py
        def parse_command(self, output: str, exit_code: int = 0) -> str | None:
            """Parse PowerShell environment variable result.
        
            Args:
                output: Raw command output
                exit_code: Command exit code
        
            Returns:
                Variable value or None if not set
            """
            # PowerShell returns empty string for unset variables
            value = output.strip()
            return value if value else None