以下是我應該如何構建程式的說明:
使用 argparse 來實作程式的 CLI 部分,使其按此處所示作業。它是本課程中為數不多的實際列印輸出的程式之一。
當您使用 -h 幫助標志時,程式的輸出應該完全像這樣。提示:您可以通過 argparse 免費獲得 -h 幫助標志。您不必自己實作 -h 標志。
$ python HW3_cli.py -h 用法:HW3_cli.py [-h] [-p] [-c] [-l] texts [texts ...]
位置引數:文本輸入字串進行操作
可選引數:-h, --help 顯示此幫助資訊并退出 -p, --print 只列印輸入字串 -c, --combine 列印組合成連續字串的輸入字串
-l, --len 列印輸入字串的長度
列印,合并,然后 len。如果沒有給出標志,則什么都不做。
看到最后的描述行告訴它有什么作用?它是用結語創建的。
如果根本沒有給出引數,它應該給出需要 texts 引數的錯誤。提示:如果您對 texts 引數進行正確編程,您將免費獲得此錯誤。
$ python HW3_cli.py 用法:HW3_cli.py [-h] [-p] [-c] [-l] texts [texts ...]
HW3_cli.py:錯誤:需要以下引數:文本
下面解釋了三個標志的行為。如果至少給出了一個輸入字串,但沒有給出標志,則程式應該什么都不做。由于講座示例中沒有使用標志,因此您需要查看 argparse 檔案(查看此處的教程)以了解如何實作標志引數。它們在檔案中稱為“短選項”。查找實作 -v "verbose" 選項的示例。
引數
標志引數控制程式做什么。這些標志可以在命令列上以任何順序或組合給出,但是,這些是實作規則:它們應該按照下面顯示的順序實作 - 換句話說,首先執行列印標志(如果給出),然后組合標志(如果給定),然后是 len 標志(如果給定)。請在使用幫助的輸出中完全按照上面顯示的方式制作所有幫助字串,如上所示。隨意復制/粘貼本檔案。
-p 或 --print 標志將列印出輸入字串,每個字串之間有空格。我們知道如何使用 str 方法之一從字串串列中生成一個字串。
-c 或 --combine 標志將列印連接在一起的所有輸入字串。同樣,我們知道如何使用字串方法來做到這一點。
The -l or --len flag prints out the lengths of each of the input strings. No, you don't need to use len_safe, since they are always strings. This is a bit more challenging, but you can figure it out! Options for implementation of -l or --len include generator expressions, special arguments for the print function, and others.
Here is how my code should look like when it runs:
$ python HW3_cli.py -c These Strings Get Concatenated TheseStringsGetConcatenated $ python HW3_cli.py -c -p These Strings Get Printed And Concatenated These Strings Get Printed And Concatenated TheseStringsGetPrintedAndConcatenated $ python HW3_cli.py -l -c -p These Strings Get Printed And Concatenated These Strings Get Printed And Concatenated TheseStringsGetPrintedAndConcatenated 5 7 3 7 3 12 $ python HW3_cli.py -c -l --print These Strings Get Printed And Concatenated These Strings Get Printed And Concatenated TheseStringsGetPrintedAndConcatenated 5 7 3 7 3 12 $ python HW3_cli.py --len --combine --print These Strings Get Printed And Concatenated These Strings Get Printed And Concatenated TheseStringsGetPrintedAndConcatenated 5 7 3 7 3 12 $ python HW3_cli.py --len --combine a b c d e f g abcdefg 1 1 1 1 1 1 1 $ python HW3_cli.py testing $ python HW3_cli.py -l testing 7$ python HW3_cli.py -p testing testing $ python HW3_cli.py -c testing testing
最后,這是我的代碼,然后我將分享我得到的錯誤:
import argparse
if __name__ == "__main__":
# Set up argparse parser and arguments here
parser = argparse.ArgumentParser(epilog="Does print, combine, then len. If no flags given, does nothing.")
# Add the arguments here
parser.add_argument('texts', help='Input strings to operate on')
parser.add_argument('-p', '--print', action='store_true', help='Just print the input strings')
parser.add_argument('-c', '--combine', action='store_true', help='Print input strings combined in a continuous string')
parser.add_argument('-l', '--len', action='store_true', help='Print the lengths of the input strings')
args = parser.parse_args()
if args.print:
words = split(str(args.texts))
print(" ".join(words))
if args.combine:
words2 = split(str(args.texts))
print("".join(words2))
if args.len:
for text in split(str(args.texts)):
print(len(text))
最后,我得到的錯誤是:$ python HW3_cli.py -c 這些字串獲取連接用法:HW3_cli.py [-h] [-p] [-c] [-l] 文本 HW3_cli.py:錯誤:無法識別的引數:字串連接起來
uj5u.com熱心網友回復:
正如評論中提到的,如果你想為 傳遞一個多詞輸入texts,那么它們需要用引號括起來,如./script "a b c" -p.
但是,這里有一種方法,您無需將其用引號括起來。這指定nargs='*'了位置引數texts,這是一個貪婪的量詞,匹配與您傳遞給腳本的引數一樣多。決議后的結果將作為單詞串列傳入,因此我們也不需要拆分輸入字串。
import argparse
if __name__ == "__main__":
# Set up argparse parser and arguments here
parser = argparse.ArgumentParser(epilog="Does print, combine, then len. If no flags given, does nothing.")
# Add the arguments here
parser.add_argument('texts', help='Input strings to operate on',
# Add `nargs=*` so we capture as many positional arguments as possible.
# The parsed result will be passed in as a list.
nargs='*')
parser.add_argument('-p', '--print', action='store_true', help='Just print the input strings')
parser.add_argument('-c', '--combine', action='store_true',
help='Print input strings combined in a continuous string')
parser.add_argument('-l', '--len', action='store_true', help='Print the lengths of the input strings')
args = parser.parse_args()
if args.print:
print(" ".join(args.texts))
if args.combine:
print("".join(args.texts))
if args.len:
# prints output on a single line, separated by spaces
print(*map(len, args.texts))
# alternatively, if you want output on each line:
# for text in args.texts:
# print(len(text))
用法示例:
$ python script.py -lcp These Strings Get Printed And Concatenated
These Strings Get Printed And Concatenated
TheseStringsGetPrintedAndConcatenated
5 7 3 7 3 12
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317680.html
上一篇:在python中洗掉字串串列元素內的“索引”的最佳方法是什么?
下一篇:如何在不使用replace()方法、StringBuilder、StringBuffer或陣列的情況下替換字串中的所有字符?
