_static/logo_on_light.svg
Sourcecode on GitHub Code license Documentation - Read Now! Documentation License
PyPI - Tag PyPI - Status PyPI - Python Version
GitHub Workflow - Build and Test Status Libraries.io status for latest release Codacy - Quality Codacy - Line Coverage Codecov - Branch Coverage

The pyTooling.CLIAbstraction Documentation

pyTooling.CLIAbstraction is an abstraction layer and wrapper for command line programs, so they can be used easily in Python. All parameters like --value=42 are implemented as argument classes on the executable.

Main Goals

  • Offer access to CLI programs as Python classes.

  • Abstract CLI arguments (a.k.a. parameter, option, flag, …) as members on such a Python class.

  • Derive program variants from existing programs.

  • Assemble parameters in list format for handover to subprocess.Popen with proper escaping and quoting.

  • Launch a program with Popen and hide the complexity of Popen.

  • Get a generator object for line-by-line output reading to enable postprocessing of outputs.

Use Cases

  • Wrap command line interfaces of EDA tools (Electronic Design Automation) in Python classes.

Example

The following example implements a portion of the git program and its commit sub-command.

Program Definition

Git program defining commit argument.
# Definition
# ======================================
class Git(Executable):
  _executableNames = {
    "Darwin":  "git",
    "Linux":   "git",
    "Windows": "git.exe"
  }

  @CLIArgument()
  class FlagVerbose(LongFlag, name="verbose"):
    """Print verbose messages."""

  @CLIArgument()
  class CommandCommit(CommandArgument, name="commit"):
    """Command to commit staged files."""

  @CLIArgument()
  class ValueCommitMessage(ShortTupleFlag, name="m"):
    """Specify the commit message."""

  def GetCommitTool(self):
    """Derive a new program from a configured program."""
    tool = self.__class__(executablePath=self._executablePath)
    tool[tool.CommandCommit] = True
    self._CopyParameters(tool)

    return tool

# Usage
# ======================================
# Create a program instance and set common parameters.
git = Git()
git[git.FlagVerbose] = True

# Derive a variant of that pre-configured program.
commit = git.getCommitTool()
commit[commit.ValueCommitMessage] = "Bumped dependencies."

# Launch the program and parse outputs line-by-line.
commit.StartProcess()
for line in commit.GetLineReader():
  print(line)

Consumers

This layer is used by:

News

Feb. 2022 - Major Update

  • Reworked names of Argument classes.

  • Added missing argument formats like PathArgument.

  • Added more unit tests and improved code-coverage.

  • Added doc-strings and extended documentation pages.

Dec. 2021 - Extracted CLIAbstraction from pyIPCMI

  • The CLI abstraction has been extracted from pyIPCMI.

Contributors

License

This Python package (source code) is licensed under Apache License 2.0.
The accompanying documentation is licensed under Creative Commons - Attribution 4.0 (CC-BY 4.0).


This document was generated on 08.Jul 2023 - 20:55.