Files
Obsidian-Main/03. 資料收集/01. Programming/Python/argparse.ArgumentParser.md
Awin Huang 0a8e11189d vault backup: 2022-09-26 18:49:43
Affected files:
.obsidian/daily-notes.json
.obsidian/plugins/periodic-notes/data.json
.obsidian/templates.json
.obsidian/workspace
01. 個人/02. 專注Study/Android/ADB 取得 APK 的 icon.md
01. 個人/02. 專注Study/Android/ADB.md
01. 個人/02. 專注Study/Android/AOSP.md
01. 個人/02. 專注Study/Android/Android programming.md
01. 個人/02. 專注Study/Android/Ktor.md
01. 個人/02. 專注Study/Android/Service.md
01. 個人/02. 專注Study/Android/Tools.md
01. 個人/02. 專注Study/Android/UI.md
01. 個人/02. 專注Study/C++/C++17.md
01. 個人/02. 專注Study/C++/Class template.md
01. 個人/02. 專注Study/C++/Structured binding declaration.md
01. 個人/02. 專注Study/C++/for_each.md
01. 個人/02. 專注Study/C++/lambda.md
01. 個人/02. 專注Study/C++/lvalue.md
01. 個人/02. 專注Study/C++/move operator.md
01. 個人/02. 專注Study/C++/rvalue.md
01. 個人/02. 專注Study/C++/智慧指標.md
01. 個人/02. 專注Study/RxKotlin/20200207 - Study RxKotlin.md
03. 資料收集/01. Programming/COM/20210726 - COM Interface.md
03. 資料收集/01. Programming/DB/MySQL.md
03. 資料收集/01. Programming/DB/sqlite.md
03. 資料收集/01. Programming/Design Pattern.md
03. 資料收集/01. Programming/FFMPEG/00. Introduction.md
03. 資料收集/01. Programming/FFMPEG/01. Setup.md
03. 資料收集/01. Programming/FFMPEG/FFMpeg.md
03. 資料收集/01. Programming/Flask.md
03. 資料收集/01. Programming/Media Foundation/20210604 - Windows media foundation.md
03. 資料收集/01. Programming/OpenCV.md
03. 資料收集/01. Programming/OpenGL.md
03. 資料收集/01. Programming/Python/argparse.ArgumentParser.md
03. 資料收集/01. Programming/Python/decorator.md
03. 資料收集/01. Programming/Python/logging.md
03. 資料收集/01. Programming/Python/opencv.md
03. 資料收集/01. Programming/Python/subprocess.md
03. 資料收集/01. Programming/Python/threading.md
03. 資料收集/01. Programming/Python/tkinter.md
03. 資料收集/01. Programming/Python/檢測工具.md
03. 資料收集/01. Programming/QT/Dropdown button.md
03. 資料收集/01. Programming/QT/QVariant.md
03. 資料收集/01. Programming/QT/Qt.md
03. 資料收集/01. Programming/UML.md
03. 資料收集/01. Programming/演算法.md
03. 資料收集/99. templates/blogHeader.md
03. 資料收集/99. templates/date.md
03. 資料收集/99. templates/front matter.md
03. 資料收集/99. templates/note.md
03. 資料收集/99. templates/table.md
03. 資料收集/99. templates/thisWeek.md
03. 資料收集/99. templates/日記.md
03. 資料收集/99. templates/讀書筆記.md
03. 資料收集/Hobby/RC.md
03. 資料收集/Hobby/RC/Traxxas Sledge.md
03. 資料收集/Hobby/RC/好盈電變調整中立點.md
03. 資料收集/Hobby/RC/差速器調教教學.md
03. 資料收集/Linux/CLI/cut.md
03. 資料收集/Linux/CLI/scp.md
03. 資料收集/Linux/CLI/timedatectl.md
03. 資料收集/Programming/Qt.md
03. 資料收集/Tool Setup/Hardware/RaspberryPi.md
03. 資料收集/Tool Setup/Software/Chrome.md
03. 資料收集/Tool Setup/Software/Obisidian.md
03. 資料收集/Tool Setup/Software/SublimeText.md
03. 資料收集/Tool Setup/Software/VirtualBox.md
03. 資料收集/Tool Setup/Software/Visual Studio Code.md
03. 資料收集/Tool Setup/Software/Windows Setup.md
03. 資料收集/Tool Setup/Software/Windows Terminal.md
03. 資料收集/Tool Setup/Software/freefilesync.md
03. 資料收集/Tool Setup/Software/vim.md
03. 資料收集/翻牆/V2Ray.md
03. 資料收集/翻牆/Wireguard.md
03. 資料收集/軟體工具/youtube-dl.md
2022-09-26 18:49:43 +08:00

56 lines
2.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
一個範例:
```python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--update_tool", default=WIN_FW_UPDATER_PATH, help="The path of win_fw_updater.exe.")
parser.add_argument("-f", "--firmware", required=True, help="The path of ITB file.")
parser.add_argument("-w", "--waittime_update_firmware", default=600, type=int, help="Wait time for update the firmware.")
parser.add_argument("-g", "--ignore_check_file_path", action='store_true', help="Skip check the existence of file.")
args = parser.parse_args()
```
#### 要求user一定要設定的參數
使用`required=True`
例如:
```python
parser.add_argument("-f", "--firmware", required=True, help="The path of ITB file.")
```
如果使用者沒有下`-f`(或者`--firmware=XXX`)就會報錯,如下:
```bash
FwUpdateCheck.py: error: the following arguments are required: -f/--firmware
```
#### 有設定才會產生的參數
使用`action='store_true'``action='store_false'`
例如:
```python
parser.add_argument("-g", "--ignore_check_file_path", action='store_true', help="Skip check the existence of file.")
```
當使用者沒有設置`-g`時,`args.ignore_check_file_path``False`,當設置時,`args.ignore_check_file_path``True`
#### 使用預設值
例如:
```python
parser.add_argument("-u", "--update_tool", default="C:\\tool.exe", help="The path of win_fw_updater.exe.")
```
`default=<Something>`來設定參數的預設值,上面的例子中,`args.update_tool`的預設值為`C:\tool.exe`
另外可以用`type=<Object type>`來指定預設值的型別。例如:
```python
parser.add_argument("-n", "--number", default=50, type=int, help="Assign a number")
```
上例中,`args.number`的預設值是50型別是`int`,所以可以直接運算,不需要再經過`int(args.number)`這樣的轉換。
#### 限制使用者的選擇
Example:
```python
parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
```
使用`choices=<list>`來限定輸入的選項,上例中,使用者只能輸入'rock'、'paper'、'scissors'這三個字串中的其中一個,否則會報錯:
```bash
error: argument move: invalid choice: 'fire' (choose from 'rock', 'paper', 'scissors')
```
-----
- https://docs.python.org/zh-tw/3/library/argparse.html