vault backup: 2022-09-26 18:39:43

Affected files:
.obsidian/workspace
03. 資料收集/HTTP Server/Nginx.md
03. 資料收集/Hobby/RC.md
03. 資料收集/Hobby/模型/Traxxas Sledge.md
03. 資料收集/Hobby/模型/舊化作例.md
03. 資料收集/Hobby/軍武/虎式.md
03. 資料收集/Programming/COM/20210726 - COM Interface.md
03. 資料收集/Programming/DB/MySQL.md
03. 資料收集/Programming/DB/sqlite.md
03. 資料收集/Programming/Design Pattern.md
03. 資料收集/Programming/FFMPEG/00. Introduction.md
03. 資料收集/Programming/FFMPEG/01. Setup.md
03. 資料收集/Programming/FFMpeg.md
03. 資料收集/Programming/Flask.md
03. 資料收集/Programming/Media Foundation/20210604 - Windows media foundation.md
03. 資料收集/Programming/OpenCV.md
03. 資料收集/Programming/OpenGL.md
03. 資料收集/Programming/Python/argparse.ArgumentParser.md
03. 資料收集/Programming/Python/decorator.md
03. 資料收集/Programming/Python/logging.md
03. 資料收集/Programming/Python/opencv.md
03. 資料收集/Programming/Python/subprocess.md
03. 資料收集/Programming/Python/threading.md
03. 資料收集/Programming/Python/tkinter.md
03. 資料收集/Programming/Python/檢測工具.md
03. 資料收集/Programming/QT/Dropdown button.md
03. 資料收集/Programming/QT/QVariant.md
03. 資料收集/Programming/QT/Qt.md
03. 資料收集/Programming/Qt.md
03. 資料收集/Programming/UML.md
03. 資料收集/Programming/演算法.md
03. 資料收集/架站/03. Trojan.md
03. 資料收集/架站/Gitea.md
03. 資料收集/架站/HTTP Server/Apache.md
03. 資料收集/架站/HTTP Server/Nginx/Reverse Proxy(Layer4).md
03. 資料收集/架站/Pelican blog.md
03. 資料收集/架站/Proxmox VE.md
03. 資料收集/架站/SWAG Reverse proxy.md
03. 資料收集/架站/Storj.md
03. 資料收集/架站/Trojan.md
03. 資料收集/科技/802.11.md
03. 資料收集/科技/HDR Sensor.md
03. 資料收集/科技/量子電腦.md
03. 資料收集/科技/鋰電池.md
03. 資料收集/軟體工具/IPFS.md
03. 資料收集/軟體工具/MkDocs.md
03. 資料收集/軟體工具/Obsidian.md
03. 資料收集/軟體工具/docker.md
03. 資料收集/軟體工具/git/apply.md
03. 資料收集/軟體工具/git/submodule.md
This commit is contained in:
2022-09-26 18:39:43 +08:00
parent 95a804e0da
commit 2f312a4575
50 changed files with 15 additions and 440 deletions

View File

@@ -0,0 +1,56 @@
一個範例:
```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