Nikhil.Nixel
Nikhil.Nixel

Reputation: 584

How to make your OWN console like windows command prompt

As you have seen in PyCharm, the state of the art console, I am trying to make one please tell me how to do it.

I know that subprocess module is quite handy for this case.

I have a exe file called add.exe The code in Python 3x for that add.exe file will be,

a = input('Enter 1st Number')
b = input('Enter 2nd Number')
print('a + b =', a + b)

Now when i use subprocess to Run this in Background and fetch me the output and then supply input i get just one big black Empty Console Screen. My Console Window Oh! This looks Ugly.

I just want to fetch my Output and get prompt when the program demands input but Without Opening an Console My code So far is this,

from subprocess import Popen, PIPE
p = Popen(['add.exe'],
          stdout=PIPE,
          stdin=PIPE,
          )
p.stdin.write(b'4')
p.stdin.write(b'6')
print(p.stdout.read())

Then I get that stupid Console And When I close that console I get the output on my IDLE,

b'Enter 1st Number: '

What should I do!! Some body please help.

Upvotes: 1

Views: 1040

Answers (1)

Nelson
Nelson

Reputation: 952

console.py

#!/usr/bin/env python3

from subprocess import check_output, PIPE

commands = ['add.exe', 'sub.exe', 'print', 'exit', 'quit']

# Only commands with a set number of arguments
num_cmd_args = {
    'add.exe': 2,
    'sub.exe': 2
}

# Command help messages
cmd_helps = {
    'add.exe': 'Adds 2 numbers together. Returns num1 + num2',
    'sub.exe': 'Subtracts 2 numbers. Returns num1 - num2',
    'print': 'Prints all arguments passed'
}

while True:
    user_in = input('$ ').split()
    cmd = user_in[0]
    args = user_in[1:]
    # Default to '*' if not cmd not found in num_cmd_args
    n_args_needed = num_cmd_args.get(cmd, '*')

    # Check cmd
    if cmd not in commands:
        print('command not found:', cmd)
        continue
    elif cmd in ('exit', 'quit'):
        break

    # To make this much better, you're probably going to want to
    #  do some type checking to make sure that the user entered
    #  numbers for commands like add.exe and sub.exe. I suggest
    #  also looking at word splitting and maybe escaping special
    #  characters like \x07b or \u252c.

    # Check cmd's args
    if n_args_needed != len(args):
        print(cmd_helps[cmd])
    elif n_args_needed in ('*', num_cmd_args[cmd]):
        out = check_output(['./'+cmd, *args])
        print(out.decode(), end='')

And these are my c++ files:


add.cpp

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
    cout << atoi(argv[1]) + atoi(argv[2]) << endl;
    return 0;
}


sub.cpp

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
    cout << atoi(argv[1]) - atoi(argv[2]) << endl;
    return 0;
}


print.cpp

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
    for (int i=1; i<argc; i++)
        cout << argv[i] << endl;
    return 0;
}

Upvotes: 2

Related Questions