The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments.
意思就是我們使用cmd,來執行程式命令時,跟隨在後面的參數,就叫做Command Line Arguments:
Python 提供了多種方法來處理這些類型的參數。 最常見的三種是:
- Using sys.argv
- Using getopt module
- Using argparse module
Using sys.argv
使用方法也很簡單,一開始先import sys,就可以使用了。
Using getopt module
Python 的 getopt module其實跟 c 的 getopt()很類似。
與 sys module 不同的是,getopt 通過參數驗證擴展了輸入字符串的分離。 它允許短選項和長選項,包括賦值。
Using argparse module
最後一個,也是我最常用的! 它比前面兩個有更多功能,且更好理解。