Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Created September 17, 2022 14:09
Show Gist options
  • Save Sanix-Darker/34f1aa3698ef8a49fb36be2028bdb064 to your computer and use it in GitHub Desktop.
Save Sanix-Darker/34f1aa3698ef8a49fb36be2028bdb064 to your computer and use it in GitHub Desktop.
[PYTHON] a basic python shell
import time
from contextlib import suppress
from subprocess import Popen, PIPE
HISTORY = []
def _execute_ls(given_args: list = []):
final_args = ['ls'].extend(given_args)
output = Popen(args=final_args or ['ls'], stdout=PIPE).communicate()[0]
return output
def _execute_hs():
print('\n'.join(HISTORY))
COMMANDS = {
"help": "This is the help function, \nAvailable functions are:\n -ls to list file\n -hs to have the history",
"ls": _execute_ls,
"hs": _execute_hs,
}
inp = ''
while len(inp) == 0:
inp = input("> ")
with suppress(KeyError):
if isinstance(COMMANDS[inp], (int, str)):
print(COMMANDS[inp])
else:
if " " in inp:
COMMANDS[inp[0]](inp[1:])
else:
COMMANDS[inp]()
HISTORY.append(inp)
inp = ''
time.sleep(1)
# python3 main.py
# > help
#This is the help function,
#Available functions are:
# -ls to list file
# -hs to have the history
#> ls
#> hs
#help
#ls
#> hs
#help
#ls
#hs
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment