Swap argv for argparse, Fixed #1 More robust help, code cleanup.

This commit is contained in:
Josh Mudge 2018-10-06 11:34:30 -06:00
parent 024c033ac2
commit d4bde8f26e
1 changed files with 15 additions and 17 deletions

View File

@ -1,22 +1,20 @@
# Python Caeser Cipher Helper v1.1.0 # Python Caeser Cipher Helper v1.2.0
# Usage (encrypt): python3 caeser-cipher.py encrypt secretmessage # Usage (encrypt): python3 caeser-cipher.py encrypt secretmessage
# Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage # Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage
import re # Import Regex tools import re # Import Regex tools
import string # Import string tools import string # Import string tools
import collections # Import tools to sort letters. import collections # Import tools to sort letters.
from sys import argv # Import tool to process command line arguments. import argparse
#if action == "help": # If error, print help https://stackoverflow.com/questions/4042452/display-help-message-with-python-argparse-when-script-is-called-without-any-argu parser = argparse.ArgumentParser(usage="(encrypt): python3 caeser-cipher.py encrypt [secretmessage] [shift]\nusage: (decrypt): python3 caeser-cipher.py decrypt [secretmessage] [shift]\nusage: If you don't know the shift of encrypted text, use 0\nusage: For example: python3 caeser-cipher.py decrypt hhhhh 0")
#print("\nUsage (encrypt): python3 caeser-cipher.py encrypt secretmessage") parser.add_argument("action")
#print("Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage\n") parser.add_argument("ciphertext")
parser.add_argument("shiftNum")
args = parser.parse_args()
script, action, ciphertext = argv # Take command line input. shiftNum = args.shiftNum
ciphertext = re.sub (r'([^a-zA-Z]+?)', '', args.ciphertext) # Remove all non-letters.
#ciphertext = input("Please type in your cipher text:") # Set variable cipertext to the input from user.
shiftNum = input("Please enter the shift you would like to use (Just hit enter if you don't know the shift used):") # Get input
#action = input("Please enter encrypt or decrypt):") # Get input
ciphertext = re.sub (r'([^a-zA-Z]+?)', '', ciphertext) # Remove all non-letters.
ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/ ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/
letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet
@ -51,7 +49,7 @@ def encrypt(ciphertext, shiftNum, letters): # Grab parameters
print("\nYour enciphered text is: " + answer) print("\nYour enciphered text is: " + answer)
def decrypt(ciphertext, shiftNum, letters, commonletter): # Grab parameters def decrypt(ciphertext, shiftNum, letters): # Grab parameters
# Create local variables for this function. # Create local variables for this function.
@ -105,13 +103,13 @@ def freq(ciphertext):
letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.) letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.)
decrypt(ciphertext, shiftNum, letters, commonletter) # Decrypt using the frequency found in here. decrypt(ciphertext, shiftNum, letters) # Decrypt using the frequency found in here.
if action == "encrypt": if args.action == "encrypt":
encrypt(ciphertext, shiftNum, letters) # Start the encrypt() function while passing 3 variables as parameters. encrypt(ciphertext, shiftNum, letters) # Start the encrypt() function while passing 3 variables as parameters.
if action == "decrypt": if args.action == "decrypt":
if shiftNum == "": if shiftNum == "0":
freq(ciphertext) freq(ciphertext)
else: else:
decrypt(ciphertext, shiftNum, letters, commonletter) decrypt(ciphertext, shiftNum, letters)