简体   繁体   English

类中的Python命令行参数不起作用

[英]Python command line arguments in class not working

I am accepting two strings word1 and word2 , through command line which should contain alphabets only. 我通过命令行接受两个字符串word1word2 ,该字符串应仅包含字母。

The check_conditions function will make sure strings are for alphabets only. check_conditions函数将确保字符串仅用于字母。

When I run this program as python myprogram.py 'word1' 'word2' , it does not show anything, no error, no output. 当我以python myprogram.py 'word1' 'word2'运行该程序时,它不显示任何内容,没有错误,没有输出。

When I run this program without using class, it works as excepted. 在不使用类的情况下运行该程序时,它的工作原理与众不同。

 class FindWinner:
    def __init__(self, word1, word2):
        self.word1 = word1
        self.word2 = word2

def check_conditions(self):
    special_chars = set('[~!@#$%^&*.-()_+{}":;01234567989\']+$')
    if special_chars.intersection(self.word1) or special_chars.intersection(self.word1) or (len(self.word1) != len(self.word2) 
or ('no' in self.word1) or ('no' in self.word2)):
        print('Invalid string.')
    else:
        print("String is valid")
        print(list(self.word1))
        print(list(self.word2))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("word1", help="First word")
    parser.add_argument("word2", help="Second word")
    args = parser.parse_args()

    c1 = FindWinner(args.number1, args.number2)
    c1.check_conditions()

What am I missing here? 我在这里想念什么?

EDIT I missed to call main() 编辑我错过了打电话给main()

if __name__ == "__main__":
    main()

Now, I am getting another error: 现在,我收到另一个错误:

if special_chars.intersection(self.word1) or special_chars.intersection(self.word1) or (len(self.word1) != len(self.word2) or ('no' in self.word1) or ('no' in self.word2)):

When I run python myprogram.py my_string my_long_string , it gives me string is valid output(else part). 当我运行python myprogram.py my_string my_long_string ,它给我string is valid输出(其他部分)。 As per above condition, both strings should not contain any special char & len should be same. 根据上述条件,两个字符串均不应包含任何特殊字符,并且len应该相同。

You should call the main function from your code since you have only defined it not called it. 您应该从代码中调用main函数,因为您只定义了main函数而没有调用它。 The usual way to get this done by adding the following lines at bottom of your code. 通过在代码底部添加以下行来完成此操作的常用方法。

if __name__ == '__main__':
    main()

To expound on @sdvd's answer, the lines 为了阐明@sdvd的答案,这些行

if __name__ == “__main__”:
    main()

Checks if the script is being invoked directly by the interpreter or if it is being imported. 检查脚本是由解释程序直接调用还是被导入。 If the script is being imported, the main function is not run, because chances are you don't want to run the script immediately. 如果正在导入脚本,则不会运行main函数,因为您可能不想立即运行该脚本。 If the script is being called directly by the interpreter, you want to run the main function immediately, which is what happens when that expression evaluated to true. 如果脚本是由解释器直接调用的,则要立即运行main函数,这是在该表达式的值为true时发生的情况。

In your script now, there is no call to the main function, so nothing happens. 现在,在您的脚本中,没有对main函数的调用,因此什么也没有发生。 Add the above lines to the bottom of your script, and things should proceed as you want them to. 将以上各行添加到脚本的底部,一切应该按照您希望的顺序进行。

To access your arguments, use args.word1 and args.word2 instead of args.number1 and args.number2 because that's what you named them. 要访问您的参数,请使用args.word1args.word2而不是args.number1args.number2因为这是您为它们命名的名称。

You name the argument one thing and use it later with another name. 您将参数命名为一件事,以后再使用另一名称。

Try using args.word1 and args.word2 , or call them number1 and number2 when you add them (using add_argument ). 尝试使用args.word1args.word2 ,或者在添加它们时使用number1number2 (使用add_argument )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM