From d4bde8f26e9381463c6ea2c476d4287001b0191e Mon Sep 17 00:00:00 2001 From: Josh Mudge Date: Sat, 6 Oct 2018 11:34:30 -0600 Subject: [PATCH] Swap argv for argparse, Fixed #1 More robust help, code cleanup. --- Caeser-Cipher/caeser-cipher.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/Caeser-Cipher/caeser-cipher.py b/Caeser-Cipher/caeser-cipher.py index 23496cd..cec75ba 100644 --- a/Caeser-Cipher/caeser-cipher.py +++ b/Caeser-Cipher/caeser-cipher.py @@ -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 (decrypt): python3 caeser-cipher.py decrypt secretmessage import re # Import Regex tools import string # Import string tools 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 - #print("\nUsage (encrypt): python3 caeser-cipher.py encrypt secretmessage") - #print("Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage\n") +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") +parser.add_argument("action") +parser.add_argument("ciphertext") +parser.add_argument("shiftNum") +args = parser.parse_args() -script, action, ciphertext = argv # Take command line input. - -#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. +shiftNum = args.shiftNum +ciphertext = re.sub (r'([^a-zA-Z]+?)', '', args.ciphertext) # Remove all non-letters. 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 @@ -51,7 +49,7 @@ def encrypt(ciphertext, shiftNum, letters): # Grab parameters 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. @@ -105,13 +103,13 @@ def freq(ciphertext): 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. -if action == "decrypt": - if shiftNum == "": +if args.action == "decrypt": + if shiftNum == "0": freq(ciphertext) else: - decrypt(ciphertext, shiftNum, letters, commonletter) + decrypt(ciphertext, shiftNum, letters)