[英]Combining flags in argparse
我有以下擴展argparse.ArgumentParser的子類,其中包含一些用於添加常見參數類型的輔助方法: 我還使用這個 class 設置了一個REPL ,以便於運行。 add_bool_arg方法為 boolean 參數添加標志,其功能在文檔字符串中描述: 但是, argparse.Argu ...
[英]Combining flags in argparse
我有以下擴展argparse.ArgumentParser的子類,其中包含一些用於添加常見參數類型的輔助方法: 我還使用這個 class 設置了一個REPL ,以便於運行。 add_bool_arg方法為 boolean 參數添加標志,其功能在文檔字符串中描述: 但是, argparse.Argu ...
[英]parse whole section of property file into a python dictionary
我有一個 python 腳本使用的屬性文件,如下所示: 在我的 python 腳本中,我想解析 section section中的所有變量。 現在我使用以下代碼片段手動執行此操作: 最終我需要解析 section section中的所有選項,所以我想知道是否有辦法將整個 section sectio ...
[英]How do I send argparse variables from one script to another while the first script is running?
我目前正在開展一個 Capstone 項目,我的團隊將在該項目中使用 Python 構建網絡流量分析器。但在我們完成所有工作之前,我必須為命令行界面構建基線。 我的目標是創建一個接受 arguments 進行初始化的接口,但稍后可以在腳本仍在運行時從命令行獲取不同的 arguments。 所以我遇 ...
[英]Show subparser help and not main parser help when non-existent arguments used?
我有一個像這樣定義的小型 CLI 應用程序 ( myscript.py )。import sys import argparse class MyParser(argparse.ArgumentParser): ''' Overriden to show help on defa ...
[英]Passing arguments through Docker to argparse not working
設置我有一個帶有以下 Dockerfile 的 dockercontainer:FROM python:3.10 WORKDIR /usr/src/app ENV ARGS="" COPY requirements.txt ./ COPY main.py ./ COPY ... COPY .. ...
[英]Python import function from another file via argparse
我正在編寫一個小實用程序 function,它接收 Python 文件位置的輸入 arguments,以及在 Python 文件中調用的 function 例如src/path/to/file_a.py 在實用程序 function 中,我像這樣解析 arguments: foo functio ...
[英]Argument Parser error: "the following arguments are required:"
我是編碼新手,無法弄清楚要修復我的代碼以使其運行。 我不斷收到的錯誤是: 下面是錯誤所指的程序片段,即行 parser.add_argument('assign', help=next(ihelp)) 我嘗試了一些來自 inte.net 的東西,比如將行更改為:args = parser.pars ...
[英]python argparser help in case of positional + REMAINDER
有如下代碼 並顯示幫助將顯示如下內容: 這顯然具有誤導性,因為按照幫助中的順序傳遞 arguments 會導致錯誤: 這當然聽起來不錯。 為了讓它工作,我們必須在使用 REMAINDER 的參數之前傳遞強制位置參數: 甚至可以修復幫助消息嗎? ...
[英]Any "fromfile_prefix_chars" equivalent for Python-Click
對於 argparse 庫中可用的fromfile_prefix_chars選項,在 Click ( https://click.palletsprojects.com/ ) 中是否有任何等效項? 有時我有很多 arguments 需要移交給基於 Click 的應用程序,並且 - 特別是在使用文件 ...
[英]Making a flag in argparse require 2 arguments with different types
我想為如下程序制作一個解析器program --serve some/path /file/to/serve.html 查看 argparse 文檔https://docs.python.org/3/library/argparse.html#type 我終其一生都無法弄清楚如何將--serve的第 ...
[英]How to pass an argument calling a function using ArgParse?
我正在嘗試使用參數調用 function_3,但收到無法識別的錯誤 arguments。我是這樣調用的: python script.py --pass test 錯誤 ...
[英]Python argparse with optional positional and default None
使用 Python 和argparse ,試圖完成以下任務:$> my_app Namespace(positional=None) $> my_app file.txt somedir Namespace(positional=['file.txt', 'somedir']) 即, ...
[英]How would I create a python program that puts the shell into a certain state?
例如,如果我將 setstate.py 運行到 shell,則 go 從 ~/Desktop $ 到 (customstate) ~/Desktop $ 激活環境時有點像 anaconda 例如: ...
[英]Is there an easy way to create a CLI "shortcut" arg that implements other args using Python's `argparse`?
有沒有一種簡單的方法可以使用argparse制作 CLI arg“快捷方式”(因為缺少更好的術語)? 我也想不出一個更好的術語來嘗試和搜索實現...... 基本上我正在嘗試制作類似於 rsync 的--archive選項的東西: 例子假設我有一個使用 argparse 進行 CLI 解析的 py ...
[英]how to pass file as argument & modify it in python script using argparse module?
我的要求是從命令行界面讀取文本文件,獲取其中的數據並在進一步的代碼中使用它。 但是,我做不到。 如果我使用第 1 行,則會拋出錯誤 1 如果第 1 行被注釋,則會拋出錯誤 2 我還在第 1 行中嘗試使用 args.file。 你能幫我把文件作為輸入並使用文件方法來修改它嗎? 錯誤一: 錯誤 2: ...
[英]Write tests for the argparse module in Python but 'TypeError: create() takes 1 positional argument but 2 were given'
我想在我的程序中使用“argparse”庫從命令行獲取 arguments。 當我運行腳本時,一切都運行良好,但我無法為那段代碼編寫適當的單元測試以證明一切正常。 當我測試我的解析器時,我得到了我不明白為什么會出現的 TypeError: 我寫了我的代碼,基於以下內容: How do you wr ...
[英]Python argparse - make arguments required or optional based on another argument
程序如何根據先前的參數/選項接受/驗證一組參數? 例如: 因此,如果使用其中一個actionX參數(只能使用一個),則可能需要其他參數。 例如對於action2 , -v和-name是必需的。 有效輸入: 輸入無效: argparse可以驗證這一點,還是將它們全部添加為可選的並在以后驗證它們更好? ...
[英]Wants to print only udp packets using pyshark and argparse python modules
我一直在嘗試僅在運行命令 -I eth0 -u 時打印 udp 數據包。 但我收到了所有的數據包。 請給一些建議 ...
[英]Is there a way to disable positional arguments given that a flag is true with Python argparse?
我正在構建一個命令行工具,它應該按如下方式工作: 我正在使用argparse來解析命令行,我面臨着似乎是庫的限制。 讓我通過一些用例來解釋。 用例 1 mytool的所有上述調用都應打印幫助消息並退出,正如上面所示,更重要的是顯示ACTIONS是強制性的。 用例 2 上述所有調用都必須根據配置文 ...
[英]How to use the settings package's `--settings` when using ArgParse?
背景: Python 有一個simple_settings [包][1],它允許從外部文件輕松導入程序設置。 某人編寫的程序,用於從命令行為程序提供設置,如python prog.py --settings=someFolder.settings , settings.py文件將位於./some ...