首页 > 解决方案 > 如何将 linux 命令行参数添加到 python 代码?

问题描述

我对 Python 很陌生,正在尝试创建一种受 ROT13 启发的加密方法。

我有点完成了代码,但我想在 Linux 终端中使用它,并带有诸如-e- 之类的参数(我想-e代表“加密”,-h这显然代表帮助。)

我做了一些谷歌搜索,发现了有关导入“argparse”的信息,我尝试了这样的事情:

import argparse
parser =argparse.ArgumentParser(description='An 
encryption method inspired from Rot13.')
args = parser.parse_args()

它没有用。

任何阅读材料或代码修复都会非常有帮助。

这是我的python代码:

import argparse
parser = argparse.ArgumentParser(description='An encryption method inspired from Rot13.')
args = parser.parse_args()

import  sys

# Dictionary to lookup the index of alphabets
alphabet = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5,
    'F' : 6, 'G' : 7, 'H' : 8, 'I' : 9, 'J' : 10,
    'K' : 11, 'L' : 12, 'M' : 13, 'N' : 14, 'O' : 15,
    'P' : 16, 'Q' : 17, 'R' : 18, 'S' : 19, 'T' : 20,
    'U' : 21, 'V' : 22, 'W' : 23, 'X' : 24, 'Y' : 25, 'Z' : 26}

# Dictionary to lookup alphabets corresponding to the index after inc
enchabet = {0 : 'Z', 1 : 'A', 2 : 'B', 3 : 'C', 4 : 'D', 5 : 'E',
    6 : 'F', 7 : 'G', 8 : 'H', 9 : 'I', 10 : 'J',
    11 : 'K', 12 : 'L', 13 : 'M', 14 : 'N', 15 : 'O',
    16 : 'P', 17 : 'Q', 18 : 'R', 19 : 'S', 20 : 'T',
    21 : 'U', 22 : 'V', 23 : 'W', 24 : 'X', 25 : 'Y'}

# Function to rotLPWen the string
# according to the inc provided
def rotLPWen(msg, inc):
calc = ''
for char in msg:
    # checking for space
    if(char != ' '):
        # looks up the dictionary and
        # adds the inc to the index
        num = ( alphabet[char] + inc ) % 26
        # looks up the second dictionary for
        # the inced alphabets and adds them
        calc += enchabet[num]
    else:
        # adds space
        calc += ' '
        #print("You can only type one word at a time!")
        #sys.exit(1)

return calc

# Function to rotLPWde the string
# according to the inc provided
def rotLPWde(msg, inc):
calced = ''
for char in msg:
    # checks for space
    if(char != ' '):
        # looks up the dictionary and
        # subtracts the inc to the index
        num = ( alphabet[char] - inc + 26) % 26
        # looks up the second dictionary for the
        # inced alphabets and adds them
        calced += enchabet[num]
    else:
        # adds space
        calced += ' '
        #print("You can only type one word at a time!")
        #sys.exit(1)
return calced

# driver function to run the program
def main():
# use 'upper()' function to convert any lowercase characters to uppercase
msg = input("Plase type something!")
msg = msg.strip()
if ' ' in msg.strip():
  words = msg.split(' ')

  encrypted = ''
  increments = []
  for word in words:
  # Create an increment for the number of characters in the sentence
    increment = len(word)
    inc = increment
    result = rotLPWen(word.upper(), inc)
    encrypted += result + ' '
    increments.append(inc)

else:
  increments = len(msg)
  inc = increments
  encrypted = rotLPWen(msg.upper(), inc)


#Creates a list from the sentence
#list1 = list(map(len,msg.split()))
print(encrypted)
print(increments)
# Executes the main function
if __name__ == '__main__':
main()

标签: pythonlinux

解决方案


推荐阅读