Shell

The Shell class is based on the builtin module cmd.Cmd. It extends it with a custom grammer, user-definable variables, functions, pipes and more. There are a few subclasses with specific purposes.

Subclass hierarchy:

cmd.Cmd # Python's builtin framework for CLI tools
└── Cmd2 # Override methods from Cmd. Add error handling and I/O methods.
    └── BaseShell # Support environment variables and sessions
        └── Shell # Use a language model: shell.ast

See Shell implementation.

Core classes

class mash.shell.base.Cmd2(completekey='tab', stdin=None, stdout=None)[source]

Extend cmd.Cmd with various capabilities. This class is restricted to functionality that requires Cmd methods to be overrriden.

Features:

  • Confirmation mode to allow a user to accept or decline commands.

  • Error handling.

  • I/O methods: cat, source, print, println, exit

  • String methods: echo, flatten

Methods:

cmdloop(): An interactive REPL.
onecmd(): Run commands ad-hoc.
class mash.shell.base.BaseShell(*args, env: ~typing.Dict[str, ~typing.Any] = None, use_model=True, save_session_prehook=<function identity>, load_session_posthook=<function identity>, **kwds)[source]

Extend Cmd with various capabilities. This class is restricted to functionality that requires Cmd methods to be overrriden.

Features:

  • An environment with local and global variable scopes.

  • Save/load sessions.

  • Decoration with functions, both at runtime and compile time.

class mash.shell.Shell(*args, env: ~typing.Dict[str, ~typing.Any] = None, use_model=True, save_session_prehook=<function identity>, load_session_posthook=<function identity>, **kwds)[source]

Extend BaseShell with a language model: Support multiline statements, pipes, conditions, variables and inline function definitions.

The language grammer is based on a BNF-based grammer, which is defined in shell.grammer.parser

The AST is implemented here: shell.ast

The language implementation is defined in shell.ast.

class mash.shell.ShellWithFileSystem(data={}, repository: FileSystem = None, **kwds)[source]