2
試圖學習如何使用outparse。所以這裏是情況,我認爲我的設置正確,它的設置只是讓我感到困惑。基本上我只想檢查我的文件名,看看是否有特定的字符串。optparse和字符串
例如:
python script.py -f filename.txt -a hello simple
我希望它返回類似...
Reading filename.txt....
The word, Hello, was found at: Line 10
The word, simple, was found at: Line 15
這是我到目前爲止,我只是不知道如何設置它正確。對於提出愚蠢的問題感到抱歉:P。提前致謝。
這是迄今爲止代碼:
from optparse import OptionParser
def main():
usage = "useage: %prog [options] arg1 arg2"
parser = OptionParser(usage)
parser.add_option_group("-a", "--all", action="store", type="string", dest="search_and", help="find ALL lines in the file for the word1 AND word2")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("not enough number of arguments")
#Not sure how to set the options...
if __name__ == "__main__":
main()
如果我想添加另一種選擇的劇本?比如,讓我們說...一個能夠找到text1或者text1的人?你認爲這是明智的嗎?如果聲明帶有選項?像if(options.search_and是true):[...]?等等? – inoobdotcom
聽起來不錯。我會試一試。之後我會發布代碼! Python很有趣哈哈。使我能夠快速搜索文件。 :D – inoobdotcom
現在我想起來了,從技術上講,你已經在用現有的例子進行OR搜索...... –