看板 Python 關於我們 聯絡資訊
※ 引述《clsmbstu (missing)》之銘言: : 假設我有一檔案recognize.py如下: : ```python : import argparse : import json : from multiprocessing import Pool : from ocr_tools import Organize : organizer = Organize() : parser = argparse.ArgumentParser(description="Recognize the contents on a la b : parser.add_argument("-i", "--image", nargs="*", required=True, help="path to i : args = parser.parse_args() : if __name__ == "__main__": : with Pool() as p: : report_set = json.dumps({"report_set": p.map(organizer.finalize_repo rt : print(report_set) : ``` : 其中,`ocr_tools`是一個我自己寫的大型模組,我已經幫它準備了requirements.txt : numpy==1.14.3 : regex==2018.2.21 : requests==2.18.4 : pandas==0.23.0 : opencv_python==3.4.1.15 : scikit_learn==0.19.1 : 所以,使用者如果拿到我的程式碼並安裝好所需套件, : 他就可以運行`python recognize.py -i <path_to_image>`。 : 而我好奇的是:有沒有可能更進一步,讓使用者只要透過某些步驟(安裝?), : 完成之後,他就可以做到例如:`ocr -i <path_to_image>`? : 概念上就像git或是homebrew,使用者甚至不需要知道它們是用什麼語言寫的。 : 我在網路上有找到一篇教學: : How Do I Make My Own Command-Line Commands Using Python? : https://dbader.org/blog/how-to-make-command-line-commands-with-python : 但是他這裡面的方法只在本地有效,沒有提到要怎麼散佈。 : 謝謝! 若要寫成cli,可以使用神器click, 這個是寫flask這個有名的team做出來的。 若要寫成可發布的,就是要研究setuptools跟怎麼寫setup.py。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.217.132.142 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1529495818.A.7B5.html
uranusjr: There is no team, only Armin 06/20 20:16
jlhc: click 看起來是比 argparse 來的更加的簡潔好用 06/20 23:02
adrianshum: 感謝 Click 推介 06/22 23:00