我希望能夠使用argparse.ArgumentParser對象的全局選項覆蓋/增加子命令的默認值。argparse通過全局選項擴充子命令的默認值
的一個例子是顯示的幫助體現了全局更新,即以下玩具例子:
import argparse
import os
import sys
# Global parser and options.
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--user",
dest="user",
default=os.environ.get("USER"),
help="Override the setting of the $USER variable.")
# Sub-command parser and options.
subparsers = parser.add_subparsers()
command = subparsers.add_parser(
"command",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
command.add_argument("--config",
dest="config",
default="~%s/config" % os.environ.get("USER"),
help="The config file.")
options = parser.parse_args()
理想的時候,當我在幫助模式運行這個我會得到,
> python example.py --user thing command --help
usage: example.py command [-h] [--config CONFIG]
optional arguments:
-h, --help show this help message and exit
--config CONFIG The config file. (default: ~thing/config)
即,配置文件路徑是用戶特定的(東西)。我意識到我可以將默認配置更改爲"~%(user)s/config"
,然後在運行時使用選項命名空間解決此問題,但我希望幫助更加明確。
我收集另一種解決辦法是嘗試分析論證,一旦獲得全局選項,即
if "--help" in sys.argv:
# Parse the command minus the help to obtain the global options.
args = sys.argv[1:]
args.remove("--help")
# Update the defaults with the global options.
options = parser.parse_args(args)
command.set_defaults(config="~%s/config" % options.user)
# Re-parse the options.
parser.parse_args()
雖然這似乎有點哈克。有更好的方法/解決方案嗎?
感謝您的答覆。這種方法肯定比我的方法更簡潔和更簡單,並且更類似於我在沒有argparse的情況下嘗試的方法,即兩次解析參數。我可以查看代碼,看看如何在提供「--help」參數時使parse_known_args()返回選項(而不是退出),而不是按照我的示例移除它。 – John
哦,我忘了那個。你可以捕獲'parse_known_args()'在調用'ArgumentParser.exit'(這是一個相對較薄的'sys.exit'包裝器)時會拋出的'SystemExit'異常。 – chepner
嘿,是的,我已經嘗試過,但是嘗試/除了問題是選項未定義。 – John