Skip to main content

Shell Creator

This is a simple python library that can be used to create entire shells for CLI applications. It was originally a part of another application, and then I decided to split it and make it a library on its own. The shell has following features:

  • Python like arithmetic operators
  • Setting/Unsetting variables
  • Builtin commands for variable manipulation
  • Builtin support for if statements and while loops
  • Autocompletion for commands and variables
  • Colorful, with nice logging
  • Easily extensible with new commands through simple class inheritance

How to use

Help and Exiting

use the following commands to get help and exit:

  • help commands will give you the list of all commands in the shell
  • help variables will give you the list of all variables in the shell
  • exit to exit the shell

Data Types

The supported types are the same basic types of python. Those are:

  • Integers
  • Floats
  • Booleans (True or False)
  • Strings (single and double quoted, escaped by /)

Operators

The shell uses the same python arithmetic operators for convenience, it also maintains their precedence and associativity. The operators are:

Operator Description
** Exponent
-x Unary minus
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
==, !=, >, >=, <, <= Comparisons
not Logical NOT
and Logical AND
or Logical OR

WARNING: Chaining comparison arguments will have a different effect from python. The shell will parse them one by one, in other words: a < b > c will be parsed as (a < b) > c rather than a < b and b > c

Variables

The shell can handle saving, deleting, and accessing variables in the following ways:

echo $var
set var=$var2 + 485 * 12
echo "This is $var"
unset $var

When dealing with variables pay attention to the following points:

  • Reading a shell variable must precede its name with a $.
  • Writing to a variable must NOT precede its name with a $.
  • Setting an existing variable will overwrite its existing value.
  • Unsetting or Echoing a non-existent variable will generate an error.

There are builtin variables, those are variables that can be set to and modified, but never unset. A library user can choose to add those as needed by using the function call shell.addBuiltinVariable

If and While

The shell supports if conditions and while loops, the syntax is as follows:

if $var1
  command
  command
elif $var2 > 50
  command
else
  command
  command
end

while $var3 + 40 < $var4
  command
end

Adding commands

The Shell Creator utilizes the great docopt library to build the commands of the shell (including the builtin ones). There's a base Command class that must be inherited and overridden to implement new commands. Example:

class ReadFile(Command):
  usage='''
  read_file

  Usage:
      read_file -h
      read_file [--f FORMAT] FILE

  Options:
      -h, --help                    Print this help message
      -f FORMAT, --format=FORMAT    The format of the file to read
  '''

  def action(self):
      print(self.args)
      print(self.args['FORMAT'])
      print(self.args['FILE'])

  shell.addCommand('read_file', ReadFile)

for more info on other fields that can be overridden check the ShellCreator/Commands.py file

Styling and Logging

The shell uses logging for logging, with the namespace SHELL. It utilizes this formatter to better format and colorize logging. It also uses prompt_toolkit's styling to style the prompt itself. You can refer to the examples or to prompt_toolkit's documentation for more details

The logging format is as follows: self.logger.error('A logging message {}', value) where self refers to the command class you create.

Prerequisites and Installation

You can install by simply running pip3 install ShellCreator

The library depends on the following libraries:

  • Pygments
  • prompt_toolkit
  • docopt
  • pyparsing

Metadata

Release files for ShellCreator 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ShellCreator 0.5.1
File Size Uploaded
ShellCreator-0.5.1.tar.gz 13.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ShellCreator 0.5.1
File Interpreter ABI Platform
ShellCreator-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 27.5 kB

Release files / ShellCreator-0.5.1.tar.gz

Download URL ShellCreator-0.5.1.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b1c384e158e2014e389191bd1030b41e5e040259f6208d5222edfcc10184c648
BLAKE2b-256 checksum
How to use checksums
001266cd1127ff8b9f4caa9838e4498a8ed3a86a2a2dbcd7fbcd1250fb4f4ca5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.1 CPython/3.8.2

Release files / ShellCreator-0.5.1-py3-none-any.whl

Download URL ShellCreator-0.5.1-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1d3a36c821b84c49cefb9cb773b19253f104c51b5937d1a6457c2fe59f8992e
BLAKE2b-256 checksum
How to use checksums
12e99888399c5683d8b5c86cece56f4bc789c47a86cacd6361ede9a5c13a70b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.1 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5

3 release files

0.4.2

2 release files

0.4.1

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page