Caesar Cipher Cmd Tool I know there are plenty of Caesar Cipher reviews on here, but I especially wanted to see how other programmers felt about my use of the cmd module and the efficiency of my functions.

Though this program in its current state is fully functional, I feel the design may be gridlocked and lacking flexibility, due to my lack of knowledge regarding the process of creating commands for the user to utilize.

The current design makes it difficult to implement the varying command attributes I hoped to add later on. How should I go about doing this? A format I see in other cmd programs goes something like command --numbers --property=number --filename

import cmd

def sym_extract(text,syms):
    text = text[text.find(syms[0])+1:text.find(syms[1])]
    return text

def del_punc(string):
    string = ''.join(ch if ch.isalnum() else '' if ch!=' ' else ' ' for ch in string)
    return(string.lower())

def cipher(text, shift):
    text = del_punc(text)
    secret = ''
    for c in text:
        if c in 'abcdefghijklmnopqrstuvwxyz':
            num = ord(c)
            num += shift
            if num &gt; ord('z'):
                num -= 26
            elif num &lt; ord('a'):
                num += 26
            secret = secret + chr(num)
        else:
            secret = secret + c
    return secret


class CryptoPromptCmd(cmd.Cmd):
    prompt = '\n&gt; '

    def default(self, arg):
        print('invalid command, try typing "help"')

    def do_quit(self, arg):
        '''exits the program'''
        return True

    def do_encrypt(self, arg):
        '''usage: encrypt (provided string or /filename)[shift number]'''
        if arg != '':
            text  = del_punc(sym_extract(arg,'()'))
            shift = int(sym_extract(arg,'[]'))
            encrypted = cipher(text,shift)
            print('\n ',encrypted.upper())
        else:
            print('invalid command\nusage: encrypt(provided string or /filename)[shift number]')
            return

    def do_decrypt(self, arg):
        '''usage:\noutput to file: decrypt(provided string or /filename)[file]\nattempt manually: decrypt(provided string or /filename)[shift number]'''
        if arg != '':
            text  = del_punc(sym_extract(arg,'()'))
            shift = -1*int(sym_extract(arg,'[]'))
            decrypted = cipher(text,shift)
            print('\n ',decrypted.upper())
        else:
            print('invalid command\nusage:')
            print('output to file: decrypt(provided string or /filename)[output /filename]')
            print('attempt manually: decrypt(provided string or /filename)[shift number]')
            return

if __name__ == '__main__':
    print('\n'* 3)
    print("Caesar Cipher Tool")
    print('==================')
    print()
    print('type "help" for commands')
    print()
    CryptoPromptCmd().cmdloop()
    print('\nexiting...\n')
If you're trying to make a shell-like utility to encrypt and decrypt strings the cmd module is exactly what you want.

In order to add argument lists to each command you could use the argparse module similarly to the following.

def do_decrypt(self, arg):
    parser = argparse.ArgumentParser(prog='decrypt')
    parser.add_argument('--filename', type=file, help='The file to decrypt')
    parser.add_argument('--shift', type=int, default=13, help='The amount to shift')
    try:
        args = parser.parse_args([arg])
    except SystemExit:
        print('The previous command was not understood')

    text = del_punc(args.file)
    shift = -1*args.shift
    decrypted = cypher(text, shift)
    ...


Some people may complain that the SystemExit exception shouldn't be caught you could also create a class that subclasses argparser.ArgumentParser and overloads the argparser.ArgumentParser.error function