From 13344bd2b87788c949c3446aebb44e12a242e703 Mon Sep 17 00:00:00 2001 From: "Matt.Wang" Date: Mon, 2 Feb 2026 02:32:02 +0700 Subject: [PATCH] translate `library/argparse.po` --- library/argparse.po | 757 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 662 insertions(+), 95 deletions(-) diff --git a/library/argparse.po b/library/argparse.po index f31276b955..ecd33ee32f 100644 --- a/library/argparse.po +++ b/library/argparse.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: Python 3.14\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-01-07 00:14+0000\n" +"POT-Creation-Date: 2026-02-02 02:24+0700\n" "PO-Revision-Date: 2018-05-23 14:38+0000\n" "Last-Translator: Adrian Liaw \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" @@ -39,8 +39,13 @@ msgid "" "option parameter values that start with ``-`` even when they correspond to " "another defined option)." msgstr "" +"雖然 :mod:`argparse` 是實作基本命令列應用程式時預設推薦的標準函式庫模組,但如" +"果開發者對命令列應用程式的行為有更精確的要求,可能會發現它無法提供足夠的控制" +"程度。當 ``argparse`` 不支援應用程式所需的行為時(例如完全停用交錯選項與位置" +"引數的支援,或是允許以 ``-`` 開頭但可能與其他已定義選項相同的選項參數值),請" +"參閱\\ :ref:`choosing-an-argument-parser`\\ 以了解替代方案。" -#: ../../library/argparse.rst:28 +#: ../../library/argparse.rst:-1 msgid "Tutorial" msgstr "教學" @@ -50,6 +55,8 @@ msgid "" "introduction to Python command-line parsing, have a look at the :ref:" "`argparse tutorial `." msgstr "" +"本頁包含 API 參考資訊。如需更淺顯的 Python 命令列剖析介紹,請參" +"閱 :ref:`argparse 教學 `。" #: ../../library/argparse.rst:34 msgid "" @@ -60,6 +67,10 @@ msgid "" "messages. The module will also issue errors when users give the program " "invalid arguments." msgstr "" +":mod:`!argparse` 模組使得撰寫使用者友善的命令列介面變得容易。程式定義它需要什" +"麼引數,而 :mod:`!argparse` 會找出如何從 :data:`sys.argv` 中剖析這些引" +"數。:mod:`!argparse` 模組也會自動產生說明和用法訊息。當使用者向程式提供無效引" +"數時,此模組也會發出錯誤。" #: ../../library/argparse.rst:40 msgid "" @@ -68,6 +79,9 @@ msgid "" "for argument specifications and has options that apply to the parser as " "whole::" msgstr "" +":mod:`!argparse` 模組對命令列介面的支援是圍" +"繞 :class:`argparse.ArgumentParser` 的實例建構的。它是引數規格的容器,並具有" +"適用於整個剖析器的選項: ::" #: ../../library/argparse.rst:44 msgid "" @@ -87,6 +101,8 @@ msgid "" "specifications to the parser. It supports positional arguments, options " "that accept values, and on/off flags::" msgstr "" +":meth:`ArgumentParser.add_argument` 方法會將個別的引數規格附加到剖析器。它支" +"援位置引數、接受值的選項以及開關旗標:::" #: ../../library/argparse.rst:53 msgid "" @@ -105,6 +121,8 @@ msgid "" "The :meth:`ArgumentParser.parse_args` method runs the parser and places the " "extracted data in a :class:`argparse.Namespace` object::" msgstr "" +":meth:`ArgumentParser.parse_args` 方法會執行剖析器,並將擷取的資料放入一" +"個 :class:`argparse.Namespace` 物件中:::" #: ../../library/argparse.rst:61 msgid "" @@ -120,6 +138,8 @@ msgid "" "mod:`!argparse`, see :ref:`Upgrading Optparse Code `." msgstr "" +"如果你在尋找如何將 :mod:`optparse` 程式碼升級到 :mod:`!argparse` 的指南,請參" +"閱\\ :ref:`升級 Optparse 程式碼 `。" #: ../../library/argparse.rst:69 msgid "ArgumentParser objects" @@ -131,6 +151,8 @@ msgid "" "as keyword arguments. Each parameter has its own more detailed description " "below, but in short they are:" msgstr "" +"建立一個新的 :class:`ArgumentParser` 物件。所有參數都應該作為關鍵字引數傳入。" +"每個參數在下面都有更詳細的描述,簡而言之它們是:" #: ../../library/argparse.rst:83 msgid "" @@ -162,7 +184,7 @@ msgstr "parents_ - 一個 :class:`ArgumentParser` 物件的串列,其引數也 #: ../../library/argparse.rst:97 msgid "formatter_class_ - A class for customizing the help output" -msgstr "formatter_class_ - 用於自訂說明輸出的類別" +msgstr "formatter_class_ - 用於自訂幫助說明輸出的類別" #: ../../library/argparse.rst:99 msgid "" @@ -175,45 +197,52 @@ msgid "" "fromfile_prefix_chars_ - The set of characters that prefix files from which " "additional arguments should be read (default: ``None``)" msgstr "" +"fromfile_prefix_chars_ - 前綴檔案的字元集合,額外引數將從這些檔案中讀取(預設" +"值:``None``)" #: ../../library/argparse.rst:105 msgid "" "argument_default_ - The global default value for arguments (default: " "``None``)" -msgstr "" +msgstr "argument_default_ - 引數的全域預設值(預設值:``None``)" #: ../../library/argparse.rst:108 msgid "" "conflict_handler_ - The strategy for resolving conflicting optionals " "(usually unnecessary)" -msgstr "" +msgstr "conflict_handler_ - 解決衝突可選引數的策略(通常不需要)" #: ../../library/argparse.rst:111 msgid "" "add_help_ - Add a ``-h/--help`` option to the parser (default: ``True``)" -msgstr "" +msgstr "add_help_ - 加入一個 ``-h/--help`` 選項到剖析器(預設值:``True``)" #: ../../library/argparse.rst:113 msgid "" "allow_abbrev_ - Allows long options to be abbreviated if the abbreviation is " "unambiguous (default: ``True``)" msgstr "" +"allow_abbrev_ - 允許長選項被縮寫,只要縮寫是無歧義的(預設值:``True``)" #: ../../library/argparse.rst:116 msgid "" "exit_on_error_ - Determines whether or not :class:`!ArgumentParser` exits " "with error info when an error occurs. (default: ``True``)" msgstr "" +"exit_on_error_ - 決定 :class:`!ArgumentParser` 在發生錯誤時是否帶著錯誤資訊退" +"出。(預設值:``True``)" #: ../../library/argparse.rst:119 msgid "" "suggest_on_error_ - Enables suggestions for mistyped argument choices and " "subparser names (default: ``False``)" msgstr "" +"suggest_on_error_ - 啟用對拼錯的引數選擇和子剖析器名稱的建議(預設值:" +"``False``)" #: ../../library/argparse.rst:122 msgid "color_ - Allow color output (default: ``True``)" -msgstr "" +msgstr "color_ - 允許彩色輸出(預設值:``True``)" #: ../../library/argparse.rst:124 msgid "*allow_abbrev* parameter was added." @@ -224,6 +253,8 @@ msgid "" "In previous versions, *allow_abbrev* also disabled grouping of short flags " "such as ``-vv`` to mean ``-v -v``." msgstr "" +"在之前的版本中,*allow_abbrev* 也會停用短旗標的分組,例如以 ``-vv`` 表示 ``-" +"v -v``。" #: ../../library/argparse.rst:131 msgid "*exit_on_error* parameter was added." @@ -235,7 +266,7 @@ msgstr "新增 *suggest_on_error* 和 *color* 參數。" #: ../../library/argparse.rst:137 ../../library/argparse.rst:696 msgid "The following sections describe how each of these are used." -msgstr "" +msgstr "以下各節描述了這些參數的使用方式。" #: ../../library/argparse.rst:143 msgid "prog" @@ -246,24 +277,32 @@ msgid "" "By default, :class:`ArgumentParser` calculates the name of the program to " "display in help messages depending on the way the Python interpreter was run:" msgstr "" +"預設情況下,:class:`ArgumentParser` 會根據 Python 直譯器的執行方式計算要在說" +"明訊息中顯示的程式名稱:" #: ../../library/argparse.rst:149 msgid "" "The :func:`base name ` of ``sys.argv[0]`` if a file was " "passed as argument." msgstr "" +"如果傳入的引數是一個檔案,則使用 ``sys.argv[0]`` 的\\ :func:`基底名稱 " +"`。" #: ../../library/argparse.rst:151 msgid "" "The Python interpreter name followed by ``sys.argv[0]`` if a directory or a " "zipfile was passed as argument." msgstr "" +"如果傳入的引數是一個目錄或 zip 檔案,則使用 Python 直譯器名稱加上 " +"``sys.argv[0]``。" #: ../../library/argparse.rst:153 msgid "" "The Python interpreter name followed by ``-m`` followed by the module or " "package name if the :option:`-m` option was used." msgstr "" +"如果使用了 :option:`-m` 選項,則使用 Python 直譯器名稱加上 ``-m`` 再加上模組" +"或套件名稱。" #: ../../library/argparse.rst:156 msgid "" @@ -272,6 +311,9 @@ msgid "" "line. However, to change this default behavior, another value can be " "supplied using the ``prog=`` argument to :class:`ArgumentParser`::" msgstr "" +"這個預設值幾乎都不會錯,因為它會使說明訊息與命令列上呼叫程式時所用的字串一" +"致。然而,若要更改此預設行為,可以使用 :class:`ArgumentParser` 的 ``prog=`` " +"引數提供另一個值: ::" #: ../../library/argparse.rst:161 msgid "" @@ -295,6 +337,8 @@ msgid "" "the ``__main__`` module attributes or from the ``prog=`` argument, is " "available to help messages using the ``%(prog)s`` format specifier." msgstr "" +"請注意,無論程式名稱是從 ``sys.argv[0]``、從 ``__main__`` 模組屬性還是從 " +"``prog=`` 引數決定的,都可以在說明訊息中透過 ``%(prog)s`` 格式說明符號使用。" #: ../../library/argparse.rst:175 msgid "" @@ -321,6 +365,8 @@ msgid "" "The default ``prog`` value now reflects how ``__main__`` was actually " "executed, rather than always being ``os.path.basename(sys.argv[0])``." msgstr "" +"預設的 ``prog`` 值現在會反映 ``__main__`` 實際的執行方式,而非總是使用 " +"``os.path.basename(sys.argv[0])``。" #: ../../library/argparse.rst:189 msgid "usage" @@ -332,6 +378,8 @@ msgid "" "arguments it contains. The default message can be overridden with the " "``usage=`` keyword argument::" msgstr "" +"預設情況下,:class:`ArgumentParser` 會根據它包含的引數計算出用法訊息。可以使" +"用 ``usage=`` 關鍵字引數覆寫預設訊息: ::" #: ../../library/argparse.rst:195 msgid "" @@ -367,7 +415,7 @@ msgstr "" msgid "" "The ``%(prog)s`` format specifier is available to fill in the program name " "in your usage messages." -msgstr "" +msgstr "``%(prog)s`` 格式說明符號可用於在你的用法訊息中填入程式名稱。" #: ../../library/argparse.rst:211 msgid "" @@ -377,6 +425,10 @@ msgid "" "`~_SubParsersAction.add_parser`, to ensure consistent command prefixes and " "usage information across subparsers." msgstr "" +"當為主剖析器指定了自訂用法訊息時,你可能也會想考慮將 ``prog`` 引數傳" +"給 :meth:`~ArgumentParser.add_subparsers`,或是將 ``prog`` 和 ``usage`` 引數" +"傳給 :meth:`~_SubParsersAction.add_parser` 以確保子剖析器之間的命令前綴和用法" +"資訊一致。" #: ../../library/argparse.rst:221 msgid "description" @@ -390,12 +442,17 @@ msgid "" "description is displayed between the command-line usage string and the help " "messages for the various arguments." msgstr "" +"大多數對 :class:`ArgumentParser` 建構函式的呼叫會使用 ``description=`` 關鍵字" +"引數。此引數提供程式功能和運作方式的簡短描述。在說明訊息中,這個描述會顯示在" +"命令列用法字串和各引數的說明訊息之間。" #: ../../library/argparse.rst:229 msgid "" "By default, the description will be line-wrapped so that it fits within the " "given space. To change this behavior, see the formatter_class_ argument." msgstr "" +"預設情況下,該描述會被自動斷行來配合給定的空間。若要更改此行為,請參閱 " +"formatter_class_ 引數。" #: ../../library/argparse.rst:234 msgid "epilog" @@ -407,6 +464,8 @@ msgid "" "the description of the arguments. Such text can be specified using the " "``epilog=`` argument to :class:`ArgumentParser`::" msgstr "" +"有些程式喜歡在引數描述之後顯示程式的額外說明。可以使" +"用 :class:`ArgumentParser` 的 ``epilog=`` 引數來指定這類文字: ::" #: ../../library/argparse.rst:240 msgid "" @@ -442,6 +501,8 @@ msgid "" "wrapped, but this behavior can be adjusted with the formatter_class_ " "argument to :class:`ArgumentParser`." msgstr "" +"與 description_ 引數一樣,``epilog=`` 文字預設會被自動斷行,但可以透" +"過 :class:`ArgumentParser` 的 formatter_class_ 引數調整此行為。" #: ../../library/argparse.rst:259 msgid "parents" @@ -457,6 +518,11 @@ msgid "" "actions from them, and adds these actions to the :class:`ArgumentParser` " "object being constructed::" msgstr "" +"有時候多個剖析器會共用一組共同的引數。與其重複定義這些引數,不如使用一個包含" +"所有共用引數的剖析器,並將其傳給 :class:`ArgumentParser` 的 ``parents=`` 引" +"數。``parents=`` 引數接受一個 :class:`ArgumentParser` 物件的串列,收集它們的" +"所有位置 action 和可選 action,並將這些 action 加入正在建構" +"的 :class:`ArgumentParser` 物件中: ::" #: ../../library/argparse.rst:268 msgid "" @@ -492,6 +558,9 @@ msgid "" "the :class:`ArgumentParser` will see two ``-h/--help`` options (one in the " "parent and one in the child) and raise an error." msgstr "" +"請注意,大多數父剖析器會指定 ``add_help=False``。否" +"則 :class:`ArgumentParser` 會看到兩個 ``-h/--help`` 選項(一個在父剖析器中," +"一個在子剖析器中)並引發錯誤。" #: ../../library/argparse.rst:286 msgid "" @@ -499,6 +568,8 @@ msgid "" "If you change the parent parsers after the child parser, those changes will " "not be reflected in the child." msgstr "" +"你必須在透過 ``parents=`` 傳入剖析器之前完全初始化它們。如果你在子剖析器之後" +"更改父剖析器,那些更改將不會反映在子剖析器中。" #: ../../library/argparse.rst:294 msgid "formatter_class" @@ -510,6 +581,8 @@ msgid "" "by specifying an alternate formatting class. Currently, there are four such " "classes:" msgstr "" +":class:`ArgumentParser` 物件允許透過指定替代的格式化類別來自訂說明格式。目前" +"有四個這樣的類別:" #: ../../library/argparse.rst:305 msgid "" @@ -518,6 +591,9 @@ msgid "" "`ArgumentParser` objects line-wrap the description_ and epilog_ texts in " "command-line help messages::" msgstr "" +":class:`RawDescriptionHelpFormatter` 和 :class:`RawTextHelpFormatter` 提供了" +"對文字描述顯示方式的更多控制。預設情況下,:class:`ArgumentParser` 物件會在命" +"令列說明訊息中為 description_ 和 epilog_ 文字自動斷行: ::" #: ../../library/argparse.rst:310 msgid "" @@ -569,6 +645,8 @@ msgid "" "indicates that description_ and epilog_ are already correctly formatted and " "should not be line-wrapped::" msgstr "" +"將 :class:`RawDescriptionHelpFormatter` 作為 ``formatter_class=`` 傳入,表示 " +"description_ 和 epilog_ 已經正確格式化,不應自動斷行: ::" #: ../../library/argparse.rst:334 msgid "" @@ -623,12 +701,17 @@ msgid "" "replaced with one. If you wish to preserve multiple blank lines, add spaces " "between the newlines." msgstr "" +":class:`RawTextHelpFormatter` 會為所有種類的說明文字保留空白,包括引數描述。" +"然而多個換行符號會被替換為一個。如果你希望保留多個空白行,請在換行符號之間加" +"入空格。" #: ../../library/argparse.rst:361 msgid "" ":class:`ArgumentDefaultsHelpFormatter` automatically adds information about " "default values to each of the argument help messages::" msgstr "" +":class:`ArgumentDefaultsHelpFormatter` 會自動將預設值的資訊加入每個引數的說明" +"訊息中: ::" #: ../../library/argparse.rst:364 msgid "" @@ -668,6 +751,8 @@ msgid "" "each argument as the display name for its values (rather than using the " "dest_ as the regular formatter does)::" msgstr "" +":class:`MetavarTypeHelpFormatter` 使用每個引數的 type_ 引數名稱作為其值的顯示" +"名稱(而非像一般格式化器使用 dest_): ::" #: ../../library/argparse.rst:383 msgid "" @@ -712,6 +797,9 @@ msgid "" "for options like ``+f`` or ``/foo``, may specify them using the " "``prefix_chars=`` argument to the :class:`ArgumentParser` constructor::" msgstr "" +"大多數命令列選項會使用 ``-`` 作為前綴,例如 ``-f/--foo``。需要支援不同或額外" +"前綴字元的剖析器,例如 ``+f`` 或 ``/foo`` 之類的選項,可以使" +"用 :class:`ArgumentParser` 建構函式的 ``prefix_chars=`` 引數來指定: ::" #: ../../library/argparse.rst:408 msgid "" @@ -733,6 +821,8 @@ msgid "" "characters that does not include ``-`` will cause ``-f/--foo`` options to be " "disallowed." msgstr "" +"``prefix_chars=`` 引數預設值為 ``'-'``。提供一個不包含 ``-`` 的字元集合會導" +"致 ``-f/--foo`` 選項被禁止。" #: ../../library/argparse.rst:420 msgid "fromfile_prefix_chars" @@ -747,6 +837,10 @@ msgid "" "of the specified characters will be treated as files, and will be replaced " "by the arguments they contain. For example::" msgstr "" +"當處理特別長的引數串列時,有時候將引數串列保存在檔案中可能比在命令列上逐一輸" +"入更合理。如果將 ``fromfile_prefix_chars=`` 引數傳給 :class:`ArgumentParser` " +"建構函式,那麼剖析器會將以任何指定字元開頭的引數視為檔案,並以檔案包含的引數" +"取代之。例如: ::" #: ../../library/argparse.rst:429 msgid "" @@ -775,6 +869,10 @@ msgid "" "'@args.txt']`` is considered equivalent to the expression ``['-f', 'foo', '-" "f', 'bar']``." msgstr "" +"從檔案讀取的引數預設必須每行一個(但也請參" +"閱 :meth:`~ArgumentParser.convert_arg_line_to_args`),且剖析器會將其視為與命" +"令列上引用檔案的原始引數位於相同位置。因此在上面的範例中,運算式 ``['-f', " +"'foo', '@args.txt']`` 等同於運算式 ``['-f', 'foo', '-f', 'bar']``。" #: ../../library/argparse.rst:445 msgid "" @@ -782,18 +880,24 @@ msgid "" "values but not as arguments. Empty lines that are read as arguments will " "result in an \"unrecognized arguments\" error." msgstr "" +"空行會被視為空字串 (``''``),可以作為值但不能作為引數。若空行被讀取為引數,會" +"導致 \"unrecognized arguments\" 錯誤。" #: ../../library/argparse.rst:449 msgid "" ":class:`ArgumentParser` uses :term:`filesystem encoding and error handler` " "to read the file containing arguments." msgstr "" +":class:`ArgumentParser` 使用\\ :term:`檔案系統編碼和錯誤處理函式 `\\ 來讀取包含引數的檔案。" #: ../../library/argparse.rst:452 msgid "" "The ``fromfile_prefix_chars=`` argument defaults to ``None``, meaning that " "arguments will never be treated as file references." msgstr "" +"``fromfile_prefix_chars=`` 引數預設值為 ``None``,意味著引數永遠不會被視為檔" +"案參照。" #: ../../library/argparse.rst:455 msgid "" @@ -803,6 +907,11 @@ msgid "" "and error handler`. Arguments file should be encoded in UTF-8 instead of " "ANSI Codepage on Windows." msgstr "" +":class:`ArgumentParser` 將讀取引數檔案的編碼和錯誤處理從預設值(例" +"如 :func:`locale.getpreferredencoding(False) ` " +"和 ``\"strict\"``)改為\\ :term:`檔案系統編碼和錯誤處理函式 `。在 Windows 上引數檔案應使用 UTF-8 而非 ANSI 字" +"碼頁編碼。" #: ../../library/argparse.rst:463 msgid "argument_default" @@ -819,6 +928,13 @@ msgid "" "suppress attribute creation on :meth:`~ArgumentParser.parse_args` calls, we " "supply ``argument_default=SUPPRESS``::" msgstr "" +"一般而言,引數預設值可以透過將預設值傳" +"給 :meth:`~ArgumentParser.add_argument` 或是透過呼" +"叫 :meth:`~ArgumentParser.set_defaults` 方法並指定一組名稱—值對 (name-value " +"pair) 來設定。然而有時候為引數指定一個剖析器層級的單一預設值可能很有用。這可" +"以透過將 ``argument_default=`` 關鍵字引數傳給 :class:`ArgumentParser` 來完" +"成。例如若要全域地抑制 :meth:`~ArgumentParser.parse_args` 呼叫時的屬性建立," +"我們可以提供 ``argument_default=SUPPRESS``: ::" #: ../../library/argparse.rst:474 msgid "" @@ -848,10 +964,13 @@ msgid "" "parse_args` method of an :class:`ArgumentParser`, it :ref:`recognizes " "abbreviations ` of long options." msgstr "" +"當你將引數串列傳給 :class:`ArgumentParser` " +"的 :meth:`~ArgumentParser.parse_args` 方法時,它會通常會\\ :ref:`辨識為長選項" +"的縮寫 `。" #: ../../library/argparse.rst:491 msgid "This feature can be disabled by setting ``allow_abbrev`` to ``False``::" -msgstr "" +msgstr "可以透過將 ``allow_abbrev`` 設為 ``False`` 來停用此功能: ::" #: ../../library/argparse.rst:493 msgid "" @@ -880,6 +999,9 @@ msgid "" "exception if an attempt is made to create an argument with an option string " "that is already in use::" msgstr "" +":class:`ArgumentParser` 物件不允許兩個 action 擁有相同的選項字串。預設情況" +"下,如果嘗試建立一個使用已存在選項字串的引數,:class:`ArgumentParser` 物件會" +"引發例外: ::" #: ../../library/argparse.rst:511 msgid "" @@ -904,6 +1026,9 @@ msgid "" "value ``'resolve'`` can be supplied to the ``conflict_handler=`` argument " "of :class:`ArgumentParser`::" msgstr "" +"有時候(例如使用 parents_ 時)直接覆寫具有相同選項字串的舊引數可能很有用。若" +"要得到此行為,可以將值 ``'resolve'`` 提供給 :class:`ArgumentParser` 的 " +"``conflict_handler=`` 引數: ::" #: ../../library/argparse.rst:523 msgid "" @@ -938,6 +1063,9 @@ msgid "" "foo`` action is retained as the ``-f`` action, because only the ``--foo`` " "option string was overridden." msgstr "" +"請注意,只有當一個 action 的所有選項字串都被覆寫時,:class:`ArgumentParser` " +"物件才會移除該 action。因此在上面的範例中,舊的 ``-f/--foo`` action 會保留為 " +"``-f`` action,因為只有 ``--foo`` 選項字串被覆寫。" #: ../../library/argparse.rst:541 msgid "add_help" @@ -949,6 +1077,9 @@ msgid "" "displays the parser's help message. If ``-h`` or ``--help`` is supplied at " "the command line, the :class:`!ArgumentParser` help will be printed." msgstr "" +"預設情況下,:class:`ArgumentParser` 物件會加入一個選項來簡單地顯示剖析器的說" +"明訊息。如果在命令列上提供了 ``-h`` 或 ``--help``,將會印出 :class:`!" +"ArgumentParser` 的說明。" #: ../../library/argparse.rst:547 msgid "" @@ -956,6 +1087,8 @@ msgid "" "This can be achieved by passing ``False`` as the ``add_help=`` argument to :" "class:`ArgumentParser`::" msgstr "" +"停用自動加入的幫助說明選項 (help option) 有時可能很有用。可以透過將 " +"``False`` 作為 ``add_help=`` 引數傳給 :class:`ArgumentParser` 來達成: ::" #: ../../library/argparse.rst:551 msgid "" @@ -982,6 +1115,9 @@ msgid "" "h`` and ``--help`` are not valid options. In this case, the first character " "in ``prefix_chars`` is used to prefix the help options::" msgstr "" +"幫助說明選項通常是 ``-h/--help``。例外情況是如果指定了 ``prefix_chars=`` 且不" +"包含 ``-``,此時 ``-h`` 和 ``--help`` 不是有效的選項。在這種情況下," +"``prefix_chars`` 中的第一個字元會用作幫助說明選項的前綴: ::" #: ../../library/argparse.rst:565 msgid "" @@ -1009,12 +1145,17 @@ msgid "" "`~ArgumentParser.parse_args` method of an :class:`ArgumentParser`, it will " "print a *message* to :data:`sys.stderr` and exit with a status code of 2." msgstr "" +"當你將無效的引數串列傳給 :class:`ArgumentParser` " +"的 :meth:`~ArgumentParser.parse_args` 方法時,它通常會向 :data:`sys.stderr` " +"印出一個\\ *訊息*\\ 並以狀態碼 2 退出。" #: ../../library/argparse.rst:580 msgid "" "If the user would like to catch errors manually, the feature can be enabled " "by setting ``exit_on_error`` to ``False``::" msgstr "" +"如果使用者想要手動捕捉錯誤,可以透過將 ``exit_on_error`` 設為 ``False`` 來啟" +"用此功能: ::" #: ../../library/argparse.rst:583 msgid "" @@ -1053,6 +1194,9 @@ msgid "" "permissible argument choices (if specified) or subparser names as part of " "the error message." msgstr "" +"預設情況下,當使用者傳入無效的引數選擇或子剖析器名稱" +"時,:class:`ArgumentParser` 會帶著錯誤資訊退出,並將允許的引數選擇(如果有指" +"定的話)或子剖析器名稱列為錯誤訊息的一部分。" #: ../../library/argparse.rst:602 msgid "" @@ -1061,6 +1205,9 @@ msgid "" "``suggest_on_error`` to ``True``. Note that this only applies for arguments " "when the choices specified are strings::" msgstr "" +"如果使用者想要啟用對拼錯的引數選擇和子剖析器名稱的建議,可以透過將 " +"``suggest_on_error`` 設為 ``True`` 來啟用此功能。請注意,這僅適用於指定的選擇" +"皆為字串的引數: ::" #: ../../library/argparse.rst:607 msgid "" @@ -1089,6 +1236,9 @@ msgid "" "available, you can set it as an attribute after initializing the parser " "instead of using the keyword argument::" msgstr "" +"如果你正在撰寫需要與舊版 Python 相容的程式碼,並且想要在 " +"``suggest_on_error`` 可用時伺機使用它,你可以在初始化剖析器後將其設為屬性,而" +"非使用關鍵字引數: ::" #: ../../library/argparse.rst:620 msgid "" @@ -1100,7 +1250,7 @@ msgstr "" #: ../../library/argparse.rst:627 msgid "color" -msgstr "" +msgstr "color" #: ../../library/argparse.rst:629 msgid "" @@ -1110,6 +1260,10 @@ msgid "" "environment `, or in the argument parser itself " "by setting ``color`` to ``False``::" msgstr "" +"預設情況下,說明訊息會使用 `ANSI 跳脫序列 `__\\ 以彩色印出。如果你想要純文字說明訊息,可以\\ :ref:`在" +"你的本地環境中 `\\ 停用它,或是透過將 ``color`` " +"設為 ``False`` 在引數剖析器本身停用: ::" #: ../../library/argparse.rst:635 msgid "" @@ -1134,6 +1288,9 @@ msgid "" "output is always disabled, even if environment variables like " "``FORCE_COLOR`` are set." msgstr "" +"請注意,當 ``color=True`` 時,彩色輸出取決於環境變數和終端機能力。然而,如果 " +"``color=False``,即使設定了 ``FORCE_COLOR`` 之類的環境變數,彩色輸出也是會被" +"停用。" #: ../../library/argparse.rst:649 msgid "" @@ -1141,6 +1298,9 @@ msgid "" "To avoid this, set the |NO_COLOR|_ or :envvar:`PYTHON_COLORS` environment " "variable (for example, ``NO_COLOR=1 python script.py 2> errors.txt``)." msgstr "" +"將 stderr 重新導向到檔案時,錯誤訊息會包含顏色代碼。若要避免此情況,請設定 |" +"NO_COLOR|_ 或 :envvar:`PYTHON_COLORS` 環境變數(例如 ``NO_COLOR=1 python " +"script.py 2> errors.txt``)。" #: ../../library/argparse.rst:658 msgid "The add_argument() method" @@ -1151,66 +1311,70 @@ msgid "" "Define how a single command-line argument should be parsed. Each parameter " "has its own more detailed description below, but in short they are:" msgstr "" +"定義單個命令列引數應如何被剖析。每個參數在下面都有更詳細的描述,簡而言之它們" +"是:" #: ../../library/argparse.rst:667 msgid "" "`name or flags`_ - Either a name or a list of option strings, e.g. ``'foo'`` " "or ``'-f', '--foo'``." msgstr "" +"`name or flags`_ - 一個名稱或選項字串的串列,例如 ``'foo'`` 或 ``'-f', '--" +"foo'``。" #: ../../library/argparse.rst:670 msgid "" "action_ - The basic type of action to be taken when this argument is " "encountered at the command line." -msgstr "" +msgstr "action_ - 在命令列遇到此引數時要執行的基本 action 類型。" #: ../../library/argparse.rst:673 msgid "nargs_ - The number of command-line arguments that should be consumed." -msgstr "" +msgstr "nargs_ - 應消耗的命令列引數數量。" #: ../../library/argparse.rst:675 msgid "" "const_ - A constant value required by some action_ and nargs_ selections." -msgstr "" +msgstr "const_ - 某些 action_ 和 nargs_ 選擇所需的常數值。" #: ../../library/argparse.rst:677 msgid "" "default_ - The value produced if the argument is absent from the command " "line and if it is absent from the namespace object." -msgstr "" +msgstr "default_ - 如果引數不在命令列中且不在命名空間物件中時所產生的值。" #: ../../library/argparse.rst:680 msgid "" "type_ - The type to which the command-line argument should be converted." -msgstr "" +msgstr "type_ - 命令列引數應轉換成的型別。" #: ../../library/argparse.rst:682 msgid "choices_ - A sequence of the allowable values for the argument." -msgstr "" +msgstr "choices_ - 引數允許值的序列。" #: ../../library/argparse.rst:684 msgid "" "required_ - Whether or not the command-line option may be omitted (optionals " "only)." -msgstr "" +msgstr "required_ - 命令列選項是否可省略(僅限可選引數)。" #: ../../library/argparse.rst:687 msgid "help_ - A brief description of what the argument does." -msgstr "" +msgstr "help_ - 引數功能的簡短描述。" #: ../../library/argparse.rst:689 msgid "metavar_ - A name for the argument in usage messages." -msgstr "" +msgstr "metavar_ - 引數在用法訊息中的名稱。" #: ../../library/argparse.rst:691 msgid "" -"dest_ - The name of the attribute to be added to the object returned by :" -"meth:`parse_args`." -msgstr "" +"dest_ - The name of the attribute to be added to the object returned " +"by :meth:`parse_args`." +msgstr "dest_ - 要加入到 :meth:`parse_args` 回傳物件中的屬性名稱。" #: ../../library/argparse.rst:694 msgid "deprecated_ - Whether or not use of the argument is deprecated." -msgstr "" +msgstr "deprecated_ - 引數的使用是否已被棄用。" #: ../../library/argparse.rst:702 msgid "name or flags" @@ -1224,10 +1388,14 @@ msgid "" "`~ArgumentParser.add_argument` must therefore be either a series of flags, " "or a simple argument name." msgstr "" +":meth:`~ArgumentParser.add_argument` 方法必須知道預期的是可選引數(如 ``-f`` " +"或 ``--foo``)還是位置引數(如檔案名稱的串列)。因此,傳" +"給 :meth:`~ArgumentParser.add_argument` 的第一個引數必須是一系列旗標或一個簡" +"單的引數名稱。" #: ../../library/argparse.rst:710 msgid "For example, an optional argument could be created like::" -msgstr "" +msgstr "例如,可選引數可以像這樣建立: ::" #: ../../library/argparse.rst:712 msgid ">>> parser.add_argument('-f', '--foo')" @@ -1235,7 +1403,7 @@ msgstr ">>> parser.add_argument('-f', '--foo')" #: ../../library/argparse.rst:714 msgid "while a positional argument could be created like::" -msgstr "" +msgstr "而位置引數可以像這樣建立: ::" #: ../../library/argparse.rst:716 msgid ">>> parser.add_argument('bar')" @@ -1247,6 +1415,8 @@ msgid "" "be identified by the ``-`` prefix, and the remaining arguments will be " "assumed to be positional::" msgstr "" +"當呼叫 :meth:`~ArgumentParser.parse_args` 時,可選引數會透過 ``-`` 前綴來辨" +"識,其餘的引數則假設為位置引數: ::" #: ../../library/argparse.rst:722 msgid "" @@ -1281,6 +1451,9 @@ msgid "" "underscores ``_`` replacing hyphens ``-`` . The metavar_ parameter defaults " "to the upper-cased name. For example::" msgstr "" +"預設情況下,:mod:`!argparse` 會自動處理引數的內部命名和顯示名稱,簡化流程而不" +"需要額外設定。因此,你不需要指定 dest_ 和 metavar_ 參數。dest_ 參數預設為引數" +"名稱,以底線 ``_`` 取代連字號 ``-``。metavar_ 參數預設為大寫的名稱。例如: ::" #: ../../library/argparse.rst:741 msgid "" @@ -1319,12 +1492,16 @@ msgid "" "``action`` keyword argument specifies how the command-line arguments should " "be handled. The supplied actions are:" msgstr "" +":class:`ArgumentParser` 物件會將命令列引數與 action 關聯起來。這些 action 幾" +"乎可以對與其關聯的命令列引數做任何事情,但大多數 action 只是將屬性加入" +"到 :meth:`~ArgumentParser.parse_args` 回傳的物件中。``action`` 關鍵字引數指定" +"命令列引數應如何被處理。提供的 action 有:" #: ../../library/argparse.rst:764 msgid "" "``'store'`` - This just stores the argument's value. This is the default " "action." -msgstr "" +msgstr "``'store'`` - 這只是儲存引數的值。這是預設的 action。" #: ../../library/argparse.rst:767 msgid "" @@ -1333,6 +1510,9 @@ msgid "" "``'store_const'`` action is most commonly used with optional arguments that " "specify some sort of flag. For example::" msgstr "" +"``'store_const'`` - 這會儲存 const_ 關鍵字引數指定的值;請注意 const_ 關鍵字" +"引數預設為 ``None``。``'store_const'`` action 最常與指定某種旗標的可選引數一" +"起使用。例如: ::" #: ../../library/argparse.rst:772 msgid "" @@ -1352,6 +1532,8 @@ msgid "" "``'store_const'`` that respectively store the values ``True`` and ``False`` " "with default values of ``False`` and ``True``::" msgstr "" +"``'store_true'`` 和 ``'store_false'`` - 這些是 ``'store_const'`` 的特殊情況," +"分別儲存值 ``True`` 和 ``False``,預設值為 ``False`` 和 ``True``: ::" #: ../../library/argparse.rst:782 msgid "" @@ -1377,6 +1559,9 @@ msgid "" "and any values from the command line will be appended after those default " "values. Example usage::" msgstr "" +"``'append'`` - 這會將每個引數值附加到一個串列中。這對於允許一個選項被多次指定" +"很有用。如果預設值是一個非空的串列,剖析後的值會以預設串列的元素開始,命令列" +"的任何值則會附加在那些預設值之後。使用範例: ::" #: ../../library/argparse.rst:795 msgid "" @@ -1397,6 +1582,9 @@ msgid "" "``None``. The ``'append_const'`` action is typically useful when multiple " "arguments need to store constants to the same list. For example::" msgstr "" +"``'append_const'`` - 這會將 const_ 關鍵字引數指定的值附加到一個串列中;請注" +"意 const_ 關鍵字引數預設為 ``None``。``'append_const'`` action 通常在多個引數" +"需要將常數儲存到同一個串列時很有用。例如: ::" #: ../../library/argparse.rst:806 msgid "" @@ -1424,6 +1612,10 @@ msgid "" "``'?'``, each character of the argument string will be appended to the list. " "Example usage::" msgstr "" +"``'extend'`` - 這會將多值引數的每個項目附加到一個串列中。``'extend'`` action " +"通常與 nargs_ 關鍵字引數值 ``'+'`` 或 ``'*'`` 一起使用。請注意,當 nargs_ 為 " +"``None``\\ (預設值)或 ``'?'`` 時,引數字串的每個字元都會被附加到串列中。使" +"用範例: ::" #: ../../library/argparse.rst:820 msgid "" @@ -1446,6 +1638,7 @@ msgid "" "``'count'`` - This counts the number of times an argument occurs. For " "example, this is useful for increasing verbosity levels::" msgstr "" +"``'count'`` - 這會計算引數出現的次數。例如,這對於增加詳細程度等級很有用: ::" #: ../../library/argparse.rst:830 msgid "" @@ -1461,7 +1654,7 @@ msgstr "" #: ../../library/argparse.rst:835 msgid "Note, the *default* will be ``None`` unless explicitly set to *0*." -msgstr "" +msgstr "請注意,除非明確設為 *0*,否則 *default* 將為 ``None``。" #: ../../library/argparse.rst:837 msgid "" @@ -1470,6 +1663,9 @@ msgid "" "added to the parser. See :class:`ArgumentParser` for details of how the " "output is created." msgstr "" +"``'help'`` - 這會印出目前剖析器中所有選項的完整說明訊息,然後退出。預設情況" +"下,help action 會自動加入到剖析器。有關輸出如何建立的詳情,請參" +"閱 :class:`ArgumentParser`。" #: ../../library/argparse.rst:842 msgid "" @@ -1477,6 +1673,8 @@ msgid "" "`~ArgumentParser.add_argument` call, and prints version information and " "exits when invoked::" msgstr "" +"``'version'`` - 這預期在 :meth:`~ArgumentParser.add_argument` 呼叫中有一個 " +"``version=`` 關鍵字引數,並在被呼叫時印出版本資訊並退出: ::" #: ../../library/argparse.rst:846 msgid "" @@ -1502,10 +1700,10 @@ msgid "" "arguments (e.g. ``'store'``, ``'append'``, ``'extend'``, or custom actions " "with non-zero ``nargs``) can be used with positional arguments." msgstr "" -"你也可以傳遞一個 :class:`Action` 子類別(例如 :class:" -"`BooleanOptionalAction`)或實作相同介面的其他物件。只有會消耗命令列引數的 " -"action(例如 ``'store'``、``'append'``、``'extend'`` 或 ``nargs`` 不為零的自" -"定義 action)可以被用於位置引數。" +"你也可以傳遞一個 :class:`Action` 子類別(例" +"如 :class:`BooleanOptionalAction`)或實作相同介面的其他物件。只有會消耗命令列" +"引數的 action(例如 ``'store'``、``'append'``、``'extend'`` 或 ``nargs`` 不為" +"零的自訂 action)可用於位置引數。" #: ../../library/argparse.rst:858 msgid "" @@ -1515,6 +1713,10 @@ msgid "" "using the :meth:`~ArgumentParser.register` method and reference them by " "their registered name." msgstr "" +"建立自訂 action 的建議方式是擴充 :class:`Action`,覆寫 :meth:`!__call__` 方" +"法,並選擇性地覆寫 :meth:`!__init__` 和 :meth:`!format_usage` 方法。你也可以" +"使用 :meth:`~ArgumentParser.register` 方法註冊自訂 action,並透過它們的註冊名" +"稱來參照。" #: ../../library/argparse.rst:863 msgid "An example of a custom action::" @@ -1574,12 +1776,16 @@ msgid "" "action. See also :ref:`specifying-ambiguous-arguments`. The supported values " "are:" msgstr "" +":class:`ArgumentParser` 物件通常將單個命令列引數與單個要執行的 action 關聯起" +"來。``nargs`` 關鍵字引數會將不同數量的命令列引數與單個 action 關聯。另請參閱" +"\\ :ref:`specifying-ambiguous-arguments`。支援的值有:" #: ../../library/argparse.rst:896 msgid "" "``N`` (an integer). ``N`` arguments from the command line will be gathered " "together into a list. For example::" msgstr "" +"``N``\\ (一個整數)。命令列中的 ``N`` 個引數會被收集到一個串列中。例如: ::" #: ../../library/argparse.rst:899 msgid "" @@ -1600,6 +1806,8 @@ msgid "" "Note that ``nargs=1`` produces a list of one item. This is different from " "the default, in which the item is produced by itself." msgstr "" +"請注意 ``nargs=1`` 會產生一個只有一個項目的串列。這與預設情況不同,預設情況下" +"項目會單獨產生。" #: ../../library/argparse.rst:910 msgid "" @@ -1610,6 +1818,10 @@ msgid "" "by a command-line argument. In this case the value from const_ will be " "produced. Some examples to illustrate this::" msgstr "" +"``'?'``。如果可能的話,會從命令列消耗一個引數,並作為單個項目產生。如果沒有命" +"令列引數存在,會產生 default_ 的值。請注意,對於可選引數有一個額外的情況 —— " +"選項字串存在但後面沒有跟著命令列引數,在這種情況下會產生 const_ 的值。以下是" +"這個情況的範例: ::" #: ../../library/argparse.rst:917 msgid "" @@ -1637,7 +1849,7 @@ msgstr "" msgid "" "One of the more common uses of ``nargs='?'`` is to allow optional input and " "output files::" -msgstr "" +msgstr "``nargs='?'`` 比較常見的用途之一是允許可選的輸入和輸出檔案: ::" #: ../../library/argparse.rst:930 msgid "" @@ -1668,6 +1880,9 @@ msgid "" "argument with ``nargs='*'``, but multiple optional arguments with " "``nargs='*'`` is possible. For example::" msgstr "" +"``'*'``。所有存在的命令列引數都會被收集到一個串列中。請注意,有多個 " +"``nargs='*'`` 的位置引數通常沒有太大意義,但多個 ``nargs='*'`` 的可選引數是可" +"能的。例如: ::" #: ../../library/argparse.rst:947 msgid "" @@ -1691,6 +1906,8 @@ msgid "" "into a list. Additionally, an error message will be generated if there " "wasn't at least one command-line argument present. For example::" msgstr "" +"``'+'``。與 ``'*'`` 一樣,所有存在的命令列引數都會被收集到一個串列中。此外," +"如果沒有至少一個命令列引數存在就會產生一個錯誤訊息。例如: ::" #: ../../library/argparse.rst:960 msgid "" @@ -1718,6 +1935,9 @@ msgid "" "be produced. Actions that do not consume command-line arguments (e.g. " "``'store_const'``) set ``nargs=0``." msgstr "" +"如果未提供 ``nargs`` 關鍵字引數,消耗的引數數量由 action_ 決定。一般而言,這" +"意味著會消耗單個命令列引數並產生單個項目(而非串列)。不消耗命令列引數的 " +"action(例如 ``'store_const'``)會設定 ``nargs=0``。" #: ../../library/argparse.rst:978 msgid "const" @@ -1730,6 +1950,8 @@ msgid "" "required for the various :class:`ArgumentParser` actions. The two most " "common uses of it are:" msgstr "" +":meth:`~ArgumentParser.add_argument` 的 ``const`` 引數用於保存不從命令列讀取" +"但為各種 :class:`ArgumentParser` action 所需的常數值。它最常見的兩種用途是:" #: ../../library/argparse.rst:984 msgid "" @@ -1740,6 +1962,11 @@ msgid "" "``const`` is not provided to :meth:`~ArgumentParser.add_argument`, it will " "receive a default value of ``None``." msgstr "" +"當 :meth:`~ArgumentParser.add_argument` 以 ``action='store_const'`` 或 " +"``action='append_const'`` 呼叫時。這些 action 會將 ``const`` 值加入" +"到 :meth:`~ArgumentParser.parse_args` 回傳物件的其中一個屬性中。範例請參閱 " +"action_ 描述。如果未向 :meth:`~ArgumentParser.add_argument` 提供 ``const``," +"它會接收預設值 ``None``。" #: ../../library/argparse.rst:992 msgid "" @@ -1750,12 +1977,18 @@ msgid "" "command-line argument following it, the value from ``const`` will be used. " "See the nargs_ description for examples." msgstr "" +"當 :meth:`~ArgumentParser.add_argument` 以選項字串(如 ``-f`` 或 ``--foo``)" +"和 ``nargs='?'`` 呼叫時。這會建立一個可選引數,後面可以跟著零個或一個命令列引" +"數。剖析命令列時,如果遇到選項字串但後面沒有命令列引數,會使用 ``const`` 的" +"值。範例請參閱 nargs_ 描述。" #: ../../library/argparse.rst:999 msgid "" "``const=None`` by default, including when ``action='append_const'`` or " "``action='store_const'``." msgstr "" +"``const=None`` 為預設值,包括當 ``action='append_const'`` 或 " +"``action='store_const'`` 時。" #: ../../library/argparse.rst:1006 msgid "default" @@ -1770,6 +2003,10 @@ msgid "" "the ``default`` value is used when the option string was not present at the " "command line::" msgstr "" +"所有可選引數和部分位置引數都可以在命令列上被省" +"略。:meth:`~ArgumentParser.add_argument` 的 ``default`` 關鍵字引數(其值預設" +"為 ``None``)指定在命令列引數不存在時應使用的值。對於可選引數,當選項字串不在" +"命令列上時會使用 ``default`` 值: ::" #: ../../library/argparse.rst:1015 msgid "" @@ -1792,6 +2029,7 @@ msgid "" "If the target namespace already has an attribute set, the action *default* " "will not overwrite it::" msgstr "" +"如果目標命名空間已經設定了一個屬性,action 的 *default* 不會覆寫它: ::" #: ../../library/argparse.rst:1025 msgid "" @@ -1812,6 +2050,9 @@ msgid "" "conversion argument, if provided, before setting the attribute on the :class:" "`Namespace` return value. Otherwise, the parser uses the value as is::" msgstr "" +"如果 ``default`` 值是一個字串,剖析器會像剖析命令列引數一樣剖析該值。特別是如" +"果有提供 type_ 轉換引數,剖析器會在設定 :class:`Namespace` 回傳值的屬性之前先" +"套用它。否則剖析器會直接使用該值: ::" #: ../../library/argparse.rst:1035 msgid "" @@ -1832,6 +2073,8 @@ msgid "" "For positional arguments with nargs_ equal to ``?`` or ``*``, the " "``default`` value is used when no command-line argument was present::" msgstr "" +"對於 nargs_ 等於 ``?`` 或 ``*`` 的位置引數,當沒有命令列引數存在時會使用 " +"``default`` 值: ::" #: ../../library/argparse.rst:1044 msgid "" @@ -1855,12 +2098,16 @@ msgid "" "applies to positional arguments with nargs_ values other than ``?`` or " "``*``, or optional arguments marked as ``required=True``." msgstr "" +"對於 required_ 引數,``default`` 值會被忽略。例如,這適用於 nargs_ 值不是 ``?" +"`` 或 ``*`` 的位置引數,或是標記為 ``required=True`` 的可選引數。" #: ../../library/argparse.rst:1055 msgid "" "Providing ``default=argparse.SUPPRESS`` causes no attribute to be added if " "the command-line argument was not present::" msgstr "" +"若有提供 ``default=argparse.SUPPRESS``,命令列引數不存在時就不會加入任何屬" +"性: ::" #: ../../library/argparse.rst:1058 msgid "" @@ -1890,12 +2137,18 @@ msgid "" "keyword for :meth:`~ArgumentParser.add_argument` allows any necessary type-" "checking and type conversions to be performed." msgstr "" +"預設情況下,剖析器將命令列引數作為簡單字串讀取。然而命令列字串往往應直譯為另" +"一種型別,例如 :class:`float` " +"或 :class:`int`。:meth:`~ArgumentParser.add_argument` 的 ``type`` 關鍵字允許" +"執行任何必要的型別檢查和型別轉換。" #: ../../library/argparse.rst:1077 msgid "" "If the type_ keyword is used with the default_ keyword, the type converter " "is only applied if the default is a string." msgstr "" +"如果 type_ 關鍵字與 default_ 關鍵字一起使用,型別轉換器只會在預設值為字串時套" +"用。" #: ../../library/argparse.rst:1080 msgid "" @@ -1905,10 +2158,14 @@ msgid "" "`ValueError`, the exception is caught and a nicely formatted error message " "is displayed. Other exception types are not handled." msgstr "" +"``type`` 的引數可以是一個接受單個字串的可呼叫物件,或是已註冊型別的名稱(請參" +"閱 :meth:`~ArgumentParser.register`)。如果函式引" +"發 :exc:`ArgumentTypeError`、:exc:`TypeError` 或 :exc:`ValueError`,剖析器會" +"捕捉例外並顯示格式良好的錯誤訊息。其他例外型別則不處理。" #: ../../library/argparse.rst:1086 msgid "Common built-in types and functions can be used as type converters:" -msgstr "" +msgstr "常見的內建型別和函式可以作為型別轉換器使用:" #: ../../library/argparse.rst:1088 msgid "" @@ -1934,7 +2191,7 @@ msgstr "" #: ../../library/argparse.rst:1100 msgid "User defined functions can be used as well:" -msgstr "" +msgstr "也可以用那些使用者定義的函式:" #: ../../library/argparse.rst:1102 msgid "" @@ -1960,6 +2217,8 @@ msgid "" "does is convert empty strings to ``False`` and non-empty strings to " "``True``. This is usually not what is desired." msgstr "" +"不建議使用 :func:`bool` 函式作為型別轉換器。它所做的只是將空字串轉為 " +"``False``,將非空字串轉為 ``True``。這通常不是期望的行為。" #: ../../library/argparse.rst:1116 msgid "" @@ -1968,6 +2227,8 @@ msgid "" "exceptions. Anything with more interesting error-handling or resource " "management should be done downstream after the arguments are parsed." msgstr "" +"一般而言,``type`` 關鍵字是一個便利功能,應只用於只會引發三種支援的例外之一的" +"簡單轉換。任何更複雜的錯誤處理或資源管理都應在引數剖析之後的下游進行。" #: ../../library/argparse.rst:1121 msgid "" @@ -1976,6 +2237,9 @@ msgid "" "JSONDecodeError` would not be well formatted and a :exc:`FileNotFoundError` " "exception would not be handled at all." msgstr "" +"例如,JSON 或 YAML 轉換具有複雜的錯誤情況,需要比 ``type`` 關鍵字所能提供的更" +"好的報告。:exc:`~json.JSONDecodeError` 無法得到良好的格式化," +"而 :exc:`FileNotFoundError` 例外則完全不會處理。" #: ../../library/argparse.rst:1126 msgid "" @@ -1986,12 +2250,16 @@ msgid "" "the parser has run and then use the :keyword:`with`-statement to manage the " "files." msgstr "" +"即使是 :class:`~argparse.FileType` 在與 ``type`` 關鍵字一起使用時也有其限制。" +"如果一個引數使用了 :class:`~argparse.FileType`,然後後續的引數失敗了,雖然會" +"報告錯誤但檔案不會自動關閉。在這種情況下,最好等到剖析器執行完畢後,再使" +"用 :keyword:`with` 陳述式來管理檔案。" #: ../../library/argparse.rst:1133 msgid "" "For type checkers that simply check against a fixed set of values, consider " "using the choices_ keyword instead." -msgstr "" +msgstr "對於僅檢查固定值集合的型別檢查器,請考慮改用 choices_ 關鍵字。" #: ../../library/argparse.rst:1140 msgid "choices" @@ -2005,6 +2273,9 @@ msgid "" "line is parsed, argument values will be checked, and an error message will " "be displayed if the argument was not one of the acceptable values::" msgstr "" +"某些命令列引數應從一組受限的值中選擇。這可以透過將序列物件作為 *choices* 關鍵" +"字引數傳給 :meth:`~ArgumentParser.add_argument` 來處理。剖析命令列時會檢查引" +"數值,如果引數不是可接受的值之一,就會顯示錯誤訊息: ::" #: ../../library/argparse.rst:1148 msgid "" @@ -2031,12 +2302,15 @@ msgid "" "Any sequence can be passed as the *choices* value, so :class:`list` " "objects, :class:`tuple` objects, and custom sequences are all supported." msgstr "" +"任何序列都可以作為 *choices* 值傳入,因此 :class:`list` 物件、:class:`tuple` " +"物件和自訂序列都有被支援。" #: ../../library/argparse.rst:1160 msgid "" "Use of :class:`enum.Enum` is not recommended because it is difficult to " "control its appearance in usage, help, and error messages." msgstr "" +"不建議使用 :class:`enum.Enum`,因為很難控制它在用法、說明和錯誤訊息中的長相。" #: ../../library/argparse.rst:1163 msgid "" @@ -2044,6 +2318,9 @@ msgid "" "performed, so objects in *choices* should match the type_ specified. This " "can make *choices* appear unfamiliar in usage, help, or error messages." msgstr "" +"請注意,*choices* 是在任何 type_ 轉換執行之後才被檢查的,因此 *choices* 中的" +"物件應符合指定的 type_。這可能使 *choices* 在用法、說明或錯誤訊息中的長相看起" +"來不尋常。" #: ../../library/argparse.rst:1168 msgid "" @@ -2051,6 +2328,8 @@ msgid "" "converts and formats values, or omit type_ and handle conversion in your " "application code." msgstr "" +"若要保持 *choices* 對使用者友善,請考慮使用自訂型別包裝器來轉換和格式化值,或" +"是省略 type_ 並在你的應用程式碼中處理轉換。" #: ../../library/argparse.rst:1172 msgid "" @@ -2059,6 +2338,9 @@ msgid "" "*dest* parameter. If this display isn't desirable (perhaps because there " "are many choices), just specify an explicit metavar_." msgstr "" +"格式化的選擇會覆寫通常從 *dest* 衍生的預設 *metavar*。這通常是你想要的,因為" +"使用者永遠看不到 *dest* 參數。如果此顯示不理想(也許因為有很多選擇),只需指" +"定一個明確的 metavar_。" #: ../../library/argparse.rst:1181 msgid "required" @@ -2071,6 +2353,10 @@ msgid "" "command line. To make an option *required*, ``True`` can be specified for " "the ``required=`` keyword argument to :meth:`~ArgumentParser.add_argument`::" msgstr "" +"一般而言,:mod:`!argparse` 模組假定像 ``-f`` 和 ``--bar`` 這樣的旗標表示\\ *" +"可選*\\ 引數,它們總是可以在命令列上被省略。若要使一個選項成為\\ *必要的*,可" +"以為 :meth:`~ArgumentParser.add_argument` 的 ``required=`` 關鍵字引數指定 " +"``True``: ::" #: ../../library/argparse.rst:1188 msgid "" @@ -2096,12 +2382,17 @@ msgid "" "`~ArgumentParser.parse_args` will report an error if that option is not " "present at the command line." msgstr "" +"如範例所示,如果一個選項被標記為 " +"``required``,:meth:`~ArgumentParser.parse_args` 會在該選項不在命令列上時報告" +"錯誤。" #: ../../library/argparse.rst:1202 msgid "" "Required options are generally considered bad form because users expect " "*options* to be *optional*, and thus they should be avoided when possible." msgstr "" +"必要選項一般認為是不良做法,因為使用者期望\\ *選項*\\ 是\\ *可選的*,因此應盡" +"可能避免使用。" #: ../../library/argparse.rst:1209 msgid "help" @@ -2114,6 +2405,8 @@ msgid "" "at the command line), these ``help`` descriptions will be displayed with " "each argument." msgstr "" +"``help`` 值是一個包含引數簡短描述的字串。當使用者請求說明(通常是在命令列上使" +"用 ``-h`` 或 ``--help``)時,這些 ``help`` 描述會與每個引數一起顯示。" #: ../../library/argparse.rst:1216 msgid "" @@ -2123,6 +2416,10 @@ msgid "" "arguments to :meth:`~ArgumentParser.add_argument`, e.g. ``%(default)s``, " "``%(type)s``, etc.::" msgstr "" +"``help`` 字串可以包含各種格式說明符號以避免重複程式名稱或引數 default_ 之類的" +"內容。可用的說明符號包括程式名稱 ``%(prog)s`` 以" +"及 :meth:`~ArgumentParser.add_argument` 的大多數關鍵字引數,例如 ``%" +"(default)s``、``%(type)s`` 等: ::" #: ../../library/argparse.rst:1221 msgid "" @@ -2155,12 +2452,16 @@ msgid "" "As the help string supports %-formatting, if you want a literal ``%`` to " "appear in the help string, you must escape it as ``%%``." msgstr "" +"由於 help 字串支援 %-格式化,如果你想讓文字 ``%`` 出現在 help 字串中,你必須" +"將它跳脫為 ``%%``。" #: ../../library/argparse.rst:1236 msgid "" ":mod:`!argparse` supports silencing the help entry for certain options, by " "setting the ``help`` value to ``argparse.SUPPRESS``::" msgstr "" +":mod:`!argparse` 支援透過將 ``help`` 值設為 ``argparse.SUPPRESS`` 來隱藏某些" +"選項的說明條目: ::" #: ../../library/argparse.rst:1239 msgid "" @@ -2195,6 +2496,11 @@ msgid "" "single optional argument ``--foo`` that should be followed by a single " "command-line argument will be referred to as ``FOO``. An example::" msgstr "" +"當 :class:`ArgumentParser` 產生說明訊息時,它需要某種方式來指稱每個預期的引" +"數。預設情況下,:class:`!ArgumentParser` 物件使用 dest_ 值作為每個物件的「名" +"稱」。對於位置引數動作,預設直接使用 dest_ 值;對於可選引數動作,則將 dest_ " +"值轉為大寫。因此,一個 ``dest='bar'`` 的位置引數稱為 ``bar``。一個應後接單一" +"命令列引數的 ``--foo`` 可選引數稱為 ``FOO``。範例: ::" #: ../../library/argparse.rst:1262 msgid "" @@ -2230,7 +2536,7 @@ msgstr "" #: ../../library/argparse.rst:1277 msgid "An alternative name can be specified with ``metavar``::" -msgstr "" +msgstr "可以用 ``metavar`` 指定替代名稱: ::" #: ../../library/argparse.rst:1279 msgid "" @@ -2270,6 +2576,8 @@ msgid "" "attribute on the :meth:`~ArgumentParser.parse_args` object is still " "determined by the dest_ value." msgstr "" +"請注意 ``metavar`` 只會更改\\ *顯示*\\ 的名稱" +"——:meth:`~ArgumentParser.parse_args` 物件上的屬性名稱仍由 dest_ 值決定。" #: ../../library/argparse.rst:1298 msgid "" @@ -2277,6 +2585,8 @@ msgid "" "times. Providing a tuple to ``metavar`` specifies a different display for " "each of the arguments::" msgstr "" +"不同的 ``nargs`` 值可能導致 metavar 被多次使用。提供一個元組給 ``metavar`` 可" +"以為每個引數指定不同的顯示: ::" #: ../../library/argparse.rst:1302 msgid "" @@ -2315,6 +2625,11 @@ msgid "" "is normally supplied as the first argument to :meth:`~ArgumentParser." "add_argument`::" msgstr "" +"大多數 :class:`ArgumentParser` action 會將某個值作" +"為 :meth:`~ArgumentParser.parse_args` 回傳物件的屬性加入。此屬性的名稱" +"由 :meth:`~ArgumentParser.add_argument` 的 ``dest`` 關鍵字引數決定。對於位置" +"引數 action,``dest`` 通常作為 :meth:`~ArgumentParser.add_argument` 的第一個" +"引數提供: ::" #: ../../library/argparse.rst:1326 msgid "" @@ -2339,6 +2654,11 @@ msgid "" "characters to make sure the string is a valid attribute name. The examples " "below illustrate this behavior::" msgstr "" +"對於可選引數 action,``dest`` 的值通常從選項字串推" +"斷。:class:`ArgumentParser` 透過取第一個長選項字串並去除開頭的 ``--`` 字串來" +"產生 ``dest`` 的值。如果沒有提供長選項字串,``dest`` 會從第一個短選項字串去除" +"開頭的 ``-`` 字元來衍生。任何內部的 ``-`` 字元都會轉為 ``_`` 字元,以確保該字" +"串是有效的屬性名稱。以下範例說明了此行為: ::" #: ../../library/argparse.rst:1340 msgid "" @@ -2360,7 +2680,7 @@ msgstr "" #: ../../library/argparse.rst:1348 msgid "``dest`` allows a custom attribute name to be provided::" -msgstr "" +msgstr "``dest`` 允許提供自訂的屬性名稱: ::" #: ../../library/argparse.rst:1350 msgid "" @@ -2388,6 +2708,11 @@ msgid "" "the future. For arguments, if ``deprecated`` is ``True``, then a warning " "will be printed to :data:`sys.stderr` when the argument is used::" msgstr "" +"在專案的生命週期中,某些引數可能需要從命令列移除。在移除它們之前,你應告知使" +"用者這些引數已棄用且即將移除。:meth:`~ArgumentParser.add_argument` 的 " +"``deprecated`` 關鍵字引數(預設為 ``False``)指定引數是否已棄用並將在未來移" +"除。對於引數,如果 ``deprecated`` 為 ``True``,那麼使用該引數時會" +"向 :data:`sys.stderr` 印出警告: ::" #: ../../library/argparse.rst:1371 msgid "" @@ -2420,6 +2745,9 @@ msgid "" "follows this API may be passed as the ``action`` parameter to :meth:" "`~ArgumentParser.add_argument`." msgstr "" +":class:`!Action` 類別實作了 Action API,為一個可呼叫物件,會回傳能夠處理來自" +"命令列引數的可呼叫物件。任何遵循此 API 的物件都可以作為 ``action`` 參數傳" +"給 :meth:`~ArgumentParser.add_argument`。" #: ../../library/argparse.rst:1395 msgid "" @@ -2429,6 +2757,10 @@ msgid "" "positional arguments plus any keyword arguments passed to :meth:" "`ArgumentParser.add_argument` except for the ``action`` itself." msgstr "" +":class:`!Action` 物件被 :class:`ArgumentParser` 用來表示從命令列的一個或多個" +"字串中剖析單個引數所需的資訊。:class:`!Action` 類別必須接受兩個位置引數以及傳" +"給 :meth:`ArgumentParser.add_argument` 的任何關鍵字引數(除了 ``action`` 本" +"身)。" #: ../../library/argparse.rst:1401 msgid "" @@ -2438,17 +2770,23 @@ msgid "" "help`, etc. defined. The easiest way to ensure these attributes are defined " "is to call :meth:`!Action.__init__`." msgstr "" +":class:`!Action` 的實例(或任何傳給 ``action`` 參數的可呼叫物件的回傳值)應定" +"義 :attr:`!dest`、:attr:`!option_strings`、:attr:`!default`、:attr:`!" +"type`、:attr:`!required`、:attr:`!help` 等屬性。確保這些屬性被定義的最簡單方" +"式是呼叫 :meth:`!Action.__init__`。" #: ../../library/argparse.rst:1409 msgid "" ":class:`!Action` instances should be callable, so subclasses must override " "the :meth:`!__call__` method, which should accept four parameters:" msgstr "" +":class:`!Action` 實例應該是可呼叫的,因此子類別必須覆寫 :meth:`!__call__` 方" +"法,它應接受四個參數:" #: ../../library/argparse.rst:1412 msgid "" "*parser* - The :class:`ArgumentParser` object which contains this action." -msgstr "" +msgstr "*parser* - 包含此 action 的 :class:`ArgumentParser` 物件。" #: ../../library/argparse.rst:1414 msgid "" @@ -2456,6 +2794,9 @@ msgid "" "`~ArgumentParser.parse_args`. Most actions add an attribute to this object " "using :func:`setattr`." msgstr "" +"*namespace* - 將由 :meth:`~ArgumentParser.parse_args` 回傳" +"的 :class:`Namespace` 物件。大多數 action 使用 :func:`setattr` 將屬性加入此物" +"件。" #: ../../library/argparse.rst:1418 msgid "" @@ -2463,6 +2804,8 @@ msgid "" "applied. Type conversions are specified with the type_ keyword argument to :" "meth:`~ArgumentParser.add_argument`." msgstr "" +"*values* - 相關聯的命令列引數,已套用任何型別轉換。型別轉換透" +"過 :meth:`~ArgumentParser.add_argument` 的 type_ 關鍵字引數指定。" #: ../../library/argparse.rst:1422 msgid "" @@ -2470,6 +2813,8 @@ msgid "" "``option_string`` argument is optional, and will be absent if the action is " "associated with a positional argument." msgstr "" +"*option_string* - 用來呼叫此 action 的選項字串。``option_string`` 引數是可選" +"的,如果 action 與位置引數關聯則不會出現。" #: ../../library/argparse.rst:1426 msgid "" @@ -2477,6 +2822,8 @@ msgid "" "typically set attributes on the ``namespace`` based on ``dest`` and " "``values``." msgstr "" +":meth:`!__call__` 方法可以執行任意動作,但通常會根據 ``dest`` 和 ``values`` " +"在 ``namespace`` 上設定屬性。" #: ../../library/argparse.rst:1431 msgid "" @@ -2485,6 +2832,9 @@ msgid "" "usage of the program. If such method is not provided, a sensible default " "will be used." msgstr "" +":class:`!Action` 子類別可以定義一個 :meth:`!format_usage` 方法,它不接受引數" +"並回傳一個字串,該字串會在印出程式用法時使用。如果未提供此方法,會使用合理的" +"預設值。" #: ../../library/argparse.rst:1437 msgid "" @@ -2493,6 +2843,9 @@ msgid "" "creates both ``--foo`` and ``--no-foo`` options, storing ``True`` and " "``False`` respectively::" msgstr "" +":class:`Action` 的子類別,用於處理具有正向和負向選項的布林旗標。加入像 ``--" +"foo`` 這樣的單個引數會自動建立 ``--foo`` 和 ``--no-foo`` 選項,其分別儲存 " +"``True`` 和 ``False``: ::" #: ../../library/argparse.rst:1442 msgid "" @@ -2517,6 +2870,7 @@ msgid "" "Convert argument strings to objects and assign them as attributes of the " "namespace. Return the populated namespace." msgstr "" +"將引數字串轉換為物件並將它們指定為命名空間的屬性。回傳填充後的命名空間。" #: ../../library/argparse.rst:1459 msgid "" @@ -2524,22 +2878,25 @@ msgid "" "created and how they are assigned. See the documentation for :meth:`!" "add_argument` for details." msgstr "" +"先前對 :meth:`add_argument` 的呼叫決定了確切建立什麼物件以及如何指定它們。詳" +"情請參閱 :meth:`!add_argument` 的文件。" #: ../../library/argparse.rst:1463 msgid "" "args_ - List of strings to parse. The default is taken from :data:`sys." "argv`." -msgstr "" +msgstr "args_ - 要剖析的字串串列。預設值取自 :data:`sys.argv`。" #: ../../library/argparse.rst:1466 msgid "" "namespace_ - An object to take the attributes. The default is a new empty :" "class:`Namespace` object." msgstr "" +"namespace_ - 接收屬性的物件。預設值是一個新的空 :class:`Namespace` 物件。" #: ../../library/argparse.rst:1471 msgid "Option value syntax" -msgstr "" +msgstr "選項值語法" #: ../../library/argparse.rst:1473 msgid "" @@ -2547,6 +2904,8 @@ msgid "" "specifying the value of an option (if it takes one). In the simplest case, " "the option and its value are passed as two separate arguments::" msgstr "" +":meth:`~ArgumentParser.parse_args` 方法支援多種方式來指定選項的值(如果它接受" +"值的話)。在最簡單的情況下,選項和它的值作為兩個獨立的引數傳入: ::" #: ../../library/argparse.rst:1477 msgid "" @@ -2572,6 +2931,8 @@ msgid "" "option and value can also be passed as a single command-line argument, using " "``=`` to separate them::" msgstr "" +"對於長選項(名稱長度超過一個字元的選項),選項和值也可以作為單個命令列引數傳" +"入,使用 ``=`` 來分隔它們: ::" #: ../../library/argparse.rst:1489 msgid "" @@ -2585,7 +2946,7 @@ msgstr "" msgid "" "For short options (options only one character long), the option and its " "value can be concatenated::" -msgstr "" +msgstr "對於短選項(只有一個字元長的選項),選項和它的值可以串接在一起: ::" #: ../../library/argparse.rst:1495 msgid "" @@ -2600,6 +2961,8 @@ msgid "" "Several short options can be joined together, using only a single ``-`` " "prefix, as long as only the last option (or none of them) requires a value::" msgstr "" +"如果只有最後一個選項需要值(或都不需要),多個短選項就可以被合併在一起、只使" +"用一個 ``-`` 前綴: ::" #: ../../library/argparse.rst:1501 msgid "" @@ -2628,6 +2991,9 @@ msgid "" "options, wrong number of positional arguments, etc. When it encounters such " "an error, it exits and prints the error along with a usage message::" msgstr "" +"在剖析命令列時,:meth:`~ArgumentParser.parse_args` 會檢查各種錯誤,包括有歧義" +"的選項、無效的型別、無效的選項、錯誤的位置引數數量等。當遇到此類錯誤時,它會" +"退出並印出錯誤及用法訊息: ::" #: ../../library/argparse.rst:1517 msgid "" @@ -2684,6 +3050,11 @@ msgid "" "like negative numbers and there are no options in the parser that look like " "negative numbers::" msgstr "" +":meth:`~ArgumentParser.parse_args` 方法嘗試在使用者明顯犯錯時給出錯誤,但有些" +"情況本質上是有歧義的。例如命令列引數 ``-1`` 既可能是嘗試指定一個選項,也可能" +"是嘗試提供一個位置引數。:meth:`~ArgumentParser.parse_args` 方法在此處很謹慎:" +"位置引數只有在看起來像負數且剖析器中沒有看起來像負數的選項時,才可以用 ``-`` " +"開頭: ::" #: ../../library/argparse.rst:1548 msgid "" @@ -2717,6 +3088,35 @@ msgid "" "usage: PROG [-h] [-1 ONE] [foo]\n" "PROG: error: argument -1: expected one argument" msgstr "" +">>> parser = argparse.ArgumentParser(prog='PROG')\n" +">>> parser.add_argument('-x')\n" +">>> parser.add_argument('foo', nargs='?')\n" +"\n" +">>> # 沒有負數選項,所以 -1 是位置引數\n" +">>> parser.parse_args(['-x', '-1'])\n" +"Namespace(foo=None, x='-1')\n" +"\n" +">>> # 沒有負數選項,所以 -1 和 -5 是位置引數\n" +">>> parser.parse_args(['-x', '-1', '-5'])\n" +"Namespace(foo='-5', x='-1')\n" +"\n" +">>> parser = argparse.ArgumentParser(prog='PROG')\n" +">>> parser.add_argument('-1', dest='one')\n" +">>> parser.add_argument('foo', nargs='?')\n" +"\n" +">>> # 有負數選項存在,所以 -1 是選項\n" +">>> parser.parse_args(['-1', 'X'])\n" +"Namespace(foo=None, one='X')\n" +"\n" +">>> # 有負數選項存在,所以 -2 是選項\n" +">>> parser.parse_args(['-2'])\n" +"usage: PROG [-h] [-1 ONE] [foo]\n" +"PROG: error: no such option: -2\n" +"\n" +">>> # 有負數選項存在,所以兩個 -1 都是選項\n" +">>> parser.parse_args(['-1', '-1'])\n" +"usage: PROG [-h] [-1 ONE] [foo]\n" +"PROG: error: argument -1: expected one argument" #: ../../library/argparse.rst:1578 msgid "" @@ -2725,6 +3125,8 @@ msgid "" "tells :meth:`~ArgumentParser.parse_args` that everything after that is a " "positional argument::" msgstr "" +"如果你有必須以 ``-`` 開頭且不像負數的位置引數,你可以插入偽引數 ``'--'``,它" +"會告訴 :meth:`~ArgumentParser.parse_args` 在此之後的所有內容都是位置引數: ::" #: ../../library/argparse.rst:1583 msgid "" @@ -2739,6 +3141,8 @@ msgid "" "See also :ref:`the argparse howto on ambiguous arguments ` for more details." msgstr "" +"更多詳情請參閱 :ref:`argparse howto 中關於有歧義引數的說明 `。" #: ../../library/argparse.rst:1592 msgid "Argument abbreviations (prefix matching)" @@ -2750,6 +3154,8 @@ msgid "" "` allows long options to be abbreviated to a prefix, if the " "abbreviation is unambiguous (the prefix matches a unique option)::" msgstr "" +":meth:`~ArgumentParser.parse_args` 方法\\ :ref:`預設 `\\ 允許長" +"選項被縮寫為前綴,只要縮寫是無歧義的(前綴匹配到唯一的選項): ::" #: ../../library/argparse.rst:1598 msgid "" @@ -2780,6 +3186,8 @@ msgid "" "An error is produced for arguments that could produce more than one options. " "This feature can be disabled by setting :ref:`allow_abbrev` to ``False``." msgstr "" +"對於可能產生多個選項的引數會產生錯誤。可以透過將 :ref:`allow_abbrev` 設為 " +"``False`` 來停用此功能。" #: ../../library/argparse.rst:1615 msgid "Beyond ``sys.argv``" @@ -2792,6 +3200,9 @@ msgid "" "passing a list of strings to :meth:`~ArgumentParser.parse_args`. This is " "useful for testing at the interactive prompt::" msgstr "" +"有時候讓 :class:`ArgumentParser` 剖析 :data:`sys.argv` 以外的引數可能很有用。" +"這可以透過將字串串列傳給 :meth:`~ArgumentParser.parse_args` 來完成。這對於在" +"互動式提示中測試很有用: ::" #: ../../library/argparse.rst:1622 msgid "" @@ -2828,6 +3239,8 @@ msgid "" "Simple class used by default by :meth:`~ArgumentParser.parse_args` to create " "an object holding attributes and return it." msgstr "" +":meth:`~ArgumentParser.parse_args` 預設使用的簡單類別,用來建立保存屬性的物件" +"並回傳它。" #: ../../library/argparse.rst:1644 msgid "" @@ -2835,6 +3248,8 @@ msgid "" "readable string representation. If you prefer to have dict-like view of the " "attributes, you can use the standard Python idiom, :func:`vars`::" msgstr "" +"這個類別刻意保持簡單,只是一個具有可讀字串表示的 :class:`object` 子類別。如果" +"你偏好以類字典的方式檢視屬性,可以使用標準 Python 慣用寫法 :func:`vars`: ::" #: ../../library/argparse.rst:1648 msgid "" @@ -2856,6 +3271,9 @@ msgid "" "to an already existing object, rather than a new :class:`Namespace` object. " "This can be achieved by specifying the ``namespace=`` keyword argument::" msgstr "" +"讓 :class:`ArgumentParser` 將屬性指定給已存在的物件而非新" +"的 :class:`Namespace` 物件也可能很有用。這可以透過指定 ``namespace=`` 關鍵字" +"引數來達成: ::" #: ../../library/argparse.rst:1658 msgid "" @@ -2881,7 +3299,7 @@ msgstr "" #: ../../library/argparse.rst:1670 msgid "Other utilities" -msgstr "" +msgstr "其他工具" #: ../../library/argparse.rst:1673 msgid "Subcommands" @@ -2901,6 +3319,14 @@ msgid "" "command name and any :class:`!ArgumentParser` constructor arguments, and " "returns an :class:`!ArgumentParser` object that can be modified as usual." msgstr "" +"許多程式將其功能分割為數個子命令,例如 ``svn`` 程式可以呼叫 ``svn " +"checkout``、``svn update`` 和 ``svn commit`` 等子命令。當程式執行多個需要不同" +"種類命令列引數的不同功能時,以這種方式分割功能是特別好的做" +"法。:class:`ArgumentParser` 透過 :meth:`!add_subparsers` 方法支援建立這類子命" +"令。:meth:`!add_subparsers` 方法通常不帶引數呼叫,並回傳一個特殊的 action 物" +"件。此物件有一個方法 :meth:`~_SubParsersAction.add_parser`,它接受命令名稱和" +"任何 :class:`!ArgumentParser` 建構函式引數,並回傳一個可以像往常一樣修改" +"的 :class:`!ArgumentParser` 物件。" #: ../../library/argparse.rst:1692 msgid "Description of parameters:" @@ -2912,12 +3338,14 @@ msgid "" "\"subcommands\" if description is provided, otherwise uses title for " "positional arguments" msgstr "" +"*title* - 說明輸出中子剖析器群組的標題;預設情況下,若有提供 description 則" +"為 \"subcommands\",否則使用位置引數的標題" #: ../../library/argparse.rst:1698 msgid "" "*description* - description for the sub-parser group in help output, by " "default ``None``" -msgstr "" +msgstr "*description* - 說明輸出中子剖析器群組的描述,預設為 ``None``" #: ../../library/argparse.rst:1701 msgid "" @@ -2925,40 +3353,46 @@ msgid "" "default the name of the program and any positional arguments before the " "subparser argument" msgstr "" +"*prog* - 將與子命令說明一起顯示的用法資訊,預設為程式名稱和子剖析器引數之前的" +"任何位置引數" #: ../../library/argparse.rst:1705 msgid "" "*parser_class* - class which will be used to create sub-parser instances, by " "default the class of the current parser (e.g. :class:`ArgumentParser`)" msgstr "" +"*parser_class* - 將用來建立子剖析器實例的類別,預設為目前剖析器的類別(例" +"如 :class:`ArgumentParser`)" #: ../../library/argparse.rst:1708 msgid "" "action_ - the basic type of action to be taken when this argument is " "encountered at the command line" -msgstr "" +msgstr "action_ - 在命令列遇到此引數時要執行的基本 action 類型" #: ../../library/argparse.rst:1711 msgid "" "dest_ - name of the attribute under which subcommand name will be stored; by " "default ``None`` and no value is stored" -msgstr "" +msgstr "dest_ - 用於儲存子命令名稱的屬性名稱;預設為 ``None`` 且不儲存任何值" #: ../../library/argparse.rst:1714 msgid "" "required_ - Whether or not a subcommand must be provided, by default " "``False`` (added in 3.7)" -msgstr "" +msgstr "required_ - 是否必須提供子命令,預設為 ``False``\\ (在 3.7 中新增)" #: ../../library/argparse.rst:1717 msgid "help_ - help for sub-parser group in help output, by default ``None``" -msgstr "" +msgstr "help_ - 說明輸出中子剖析器群組的說明,預設為 ``None``" #: ../../library/argparse.rst:1719 msgid "" "metavar_ - string presenting available subcommands in help; by default it is " "``None`` and presents subcommands in form {cmd1, cmd2, ..}" msgstr "" +"metavar_ - 在說明中呈現可用子命令的字串;預設為 ``None`` 並以 {cmd1, " +"cmd2, ..} 的形式呈現子命令" #: ../../library/argparse.rst:1722 msgid "Some example usage::" @@ -2986,6 +3420,25 @@ msgid "" ">>> parser.parse_args(['--foo', 'b', '--baz', 'Z'])\n" "Namespace(baz='Z', foo=True)" msgstr "" +">>> # 建立頂層剖析器\n" +">>> parser = argparse.ArgumentParser(prog='PROG')\n" +">>> parser.add_argument('--foo', action='store_true', help='foo help')\n" +">>> subparsers = parser.add_subparsers(help='subcommand help')\n" +">>>\n" +">>> # 建立 \"a\" 命令的剖析器\n" +">>> parser_a = subparsers.add_parser('a', help='a help')\n" +">>> parser_a.add_argument('bar', type=int, help='bar help')\n" +">>>\n" +">>> # 建立 \"b\" 命令的剖析器\n" +">>> parser_b = subparsers.add_parser('b', help='b help')\n" +">>> parser_b.add_argument('--baz', choices=('X', 'Y', 'Z'), help='baz " +"help')\n" +">>>\n" +">>> # 剖析一些引數串列\n" +">>> parser.parse_args(['a', '12'])\n" +"Namespace(bar=12, foo=False)\n" +">>> parser.parse_args(['--foo', 'b', '--baz', 'Z'])\n" +"Namespace(baz='Z', foo=True)" #: ../../library/argparse.rst:1743 msgid "" @@ -2996,6 +3449,10 @@ msgid "" "present, and when the ``b`` command is specified, only the ``foo`` and " "``baz`` attributes are present." msgstr "" +"請注意 :meth:`parse_args` 回傳的物件只會包含主剖析器和命令列所選子剖析器的屬" +"性(而非任何其他子剖析器)。因此在上面的範例中,當指定 ``a`` 命令時,只有 " +"``foo`` 和 ``bar`` 屬性存在;當指定 ``b`` 命令時,只有 ``foo`` 和 ``baz`` 屬" +"性存在。" #: ../../library/argparse.rst:1750 msgid "" @@ -3005,6 +3462,9 @@ msgid "" "subparser command, however, can be given by supplying the ``help=`` argument " "to :meth:`~_SubParsersAction.add_parser` as above.)" msgstr "" +"同樣地,當從子剖析器請求說明訊息時,只會印出該特定剖析器的說明。說明訊息不會" +"包含父剖析器或兄弟剖析器的訊息。(然而每個子剖析器命令的說明訊息可以透過如上" +"所述向 :meth:`~_SubParsersAction.add_parser` 提供 ``help=`` 引數來給定。)" #: ../../library/argparse.rst:1758 msgid "" @@ -3070,6 +3530,8 @@ msgid "" "``description`` keyword arguments. When either is present, the subparser's " "commands will appear in their own group in the help output. For example::" msgstr "" +":meth:`add_subparsers` 方法也支援 ``title`` 和 ``description`` 關鍵字引數。當" +"任一者存在時,子剖析器的命令會在說明輸出中以自己的群組出現。例如: ::" #: ../../library/argparse.rst:1790 msgid "" @@ -3114,6 +3576,9 @@ msgid "" "subparser. This example, like ``svn``, aliases ``co`` as a shorthand for " "``checkout``::" msgstr "" +"此外,:meth:`~_SubParsersAction.add_parser` 支援一個額外的 *aliases* 引數,它" +"允許多個字串參照同一個子剖析器。這個範例像 ``svn`` 一樣,將 ``co`` 作為 " +"``checkout`` 的簡寫別名: ::" #: ../../library/argparse.rst:1812 msgid "" @@ -3136,6 +3601,8 @@ msgid "" ":meth:`~_SubParsersAction.add_parser` supports also an additional " "*deprecated* argument, which allows to deprecate the subparser." msgstr "" +":meth:`~_SubParsersAction.add_parser` 也支援一個額外的 *deprecated* 引數,它" +"允許棄用子剖析器。" #: ../../library/argparse.rst:1833 msgid "" @@ -3144,6 +3611,9 @@ msgid "" "that each subparser knows which Python function it should execute. For " "example::" msgstr "" +"處理子命令的一個特別有效的方式是將 :meth:`add_subparsers` 方法的使用" +"與 :meth:`set_defaults` 的呼叫結合,使每個子剖析器知道它應該執行哪個 Python " +"函式。例如: ::" #: ../../library/argparse.rst:1838 msgid "" @@ -3179,6 +3649,37 @@ msgid "" ">>> args.func(args)\n" "((XYZYX))" msgstr "" +">>> # 子命令函式\n" +">>> def foo(args):\n" +"... print(args.x * args.y)\n" +"...\n" +">>> def bar(args):\n" +"... print('((%s))' % args.z)\n" +"...\n" +">>> # 建立頂層剖析器\n" +">>> parser = argparse.ArgumentParser()\n" +">>> subparsers = parser.add_subparsers(required=True)\n" +">>>\n" +">>> # 建立 \"foo\" 命令的剖析器\n" +">>> parser_foo = subparsers.add_parser('foo')\n" +">>> parser_foo.add_argument('-x', type=int, default=1)\n" +">>> parser_foo.add_argument('y', type=float)\n" +">>> parser_foo.set_defaults(func=foo)\n" +">>>\n" +">>> # 建立 \"bar\" 命令的剖析器\n" +">>> parser_bar = subparsers.add_parser('bar')\n" +">>> parser_bar.add_argument('z')\n" +">>> parser_bar.set_defaults(func=bar)\n" +">>>\n" +">>> # 剖析引數並呼叫所選的函式\n" +">>> args = parser.parse_args('foo 1 -x 2'.split())\n" +">>> args.func(args)\n" +"2.0\n" +">>>\n" +">>> # 剖析引數並呼叫所選的函式\n" +">>> args = parser.parse_args('bar XYZYX'.split())\n" +">>> args.func(args)\n" +"((XYZYX))" #: ../../library/argparse.rst:1870 msgid "" @@ -3189,6 +3690,10 @@ msgid "" "to check the name of the subparser that was invoked, the ``dest`` keyword " "argument to the :meth:`add_subparsers` call will work::" msgstr "" +"這樣你可以讓 :meth:`parse_args` 在引數剖析完成後負責呼叫適當的函式。像這樣將" +"函式與 action 關聯通常是處理每個子剖析器不同動作的最簡單方式。然而如果有必要" +"檢查被呼叫的子剖析器名稱,:meth:`add_subparsers` 呼叫的 ``dest`` 關鍵字引數可" +"以達成此目的: ::" #: ../../library/argparse.rst:1877 msgid "" @@ -3212,13 +3717,13 @@ msgstr "" #: ../../library/argparse.rst:1886 msgid "New *required* keyword-only parameter." -msgstr "" +msgstr "新增 *required* 僅限關鍵字參數。" #: ../../library/argparse.rst:1889 msgid "" "Subparser's *prog* is no longer affected by a custom usage message in the " "main parser." -msgstr "" +msgstr "子剖析器的 *prog* 不再受主剖析器中自訂用法訊息的影響。" #: ../../library/argparse.rst:1895 msgid "FileType objects" @@ -3232,6 +3737,10 @@ msgid "" "with the requested modes, buffer sizes, encodings and error handling (see " "the :func:`open` function for more details)::" msgstr "" +":class:`FileType` 工廠建立可以傳給 :meth:`ArgumentParser.add_argument` 的 " +"type 引數的物件。以 :class:`FileType` 物件作為其型別的引數會以請求的模式、緩" +"衝區大小、編碼和錯誤處理開啟命令列引數作為檔案(更多詳情請參閱 :func:`open` " +"函式): ::" #: ../../library/argparse.rst:1905 msgid "" @@ -3257,6 +3766,9 @@ msgid "" "convert this into :data:`sys.stdin` for readable :class:`FileType` objects " "and :data:`sys.stdout` for writable :class:`FileType` objects::" msgstr "" +"FileType 物件能理解偽引數 ``'-'``,並自動將其轉換為可讀 :class:`FileType` 物" +"件的 :data:`sys.stdin` 和可寫 :class:`FileType` 物件" +"的 :data:`sys.stdout`: ::" #: ../../library/argparse.rst:1915 msgid "" @@ -3278,6 +3790,9 @@ msgid "" "after the parser has run and then use the :keyword:`with`-statement to " "manage the files." msgstr "" +"如果一個引數使用了 *FileType* 然後後續的引數失敗了,雖然會報告錯誤但檔案不會" +"自動關閉。這也可能覆蓋輸出檔案。在這種情況下,最好等到剖析器執行完畢後,再使" +"用 :keyword:`with` 陳述式來管理檔案。" #: ../../library/argparse.rst:1928 msgid "Added the *encodings* and *errors* parameters." @@ -3295,6 +3810,9 @@ msgid "" "appropriate groups can be created using the :meth:`!add_argument_group` " "method::" msgstr "" +"預設情況下,:class:`ArgumentParser` 在顯示說明訊息時會將命令列引數分成「位置" +"引數」和「選項」兩組。當引數有比此預設分組更好的概念性分組時,可以使" +"用 :meth:`!add_argument_group` 方法建立適當的群組: ::" #: ../../library/argparse.rst:1946 msgid "" @@ -3330,6 +3848,11 @@ msgid "" "accepts *title* and *description* arguments which can be used to customize " "this display::" msgstr "" +":meth:`add_argument_group` 方法回傳一個引數群組物件,它具有與一" +"般 :class:`ArgumentParser` 相同的 :meth:`~ArgumentParser.add_argument` 方法。" +"當引數被加入群組時,剖析器會像處理正常引數一樣處理它,但在說明訊息中以獨立的" +"群組顯示該引數。:meth:`!add_argument_group` 方法接受 *title* 和 " +"*description* 引數,可以用來自訂此顯示: ::" #: ../../library/argparse.rst:1965 msgid "" @@ -3377,12 +3900,17 @@ msgid "" "`ArgumentParser` constructor, but apply specifically to the argument group " "rather than the entire parser." msgstr "" +"選擇性的僅限關鍵字參數 argument_default_ 和 conflict_handler_ 允許對引數群組" +"的行為進行更精細的控制。這些參數的意義與 :class:`ArgumentParser` 建構函式中的" +"相同,但專門適用於引數群組而非整個剖析器。" #: ../../library/argparse.rst:1988 msgid "" "Note that any arguments not in your user-defined groups will end up back in " "the usual \"positional arguments\" and \"optional arguments\" sections." msgstr "" +"請注意,任何不在使用者定義群組中的引數會歸入一般的「位置引數」和「可選引數」" +"區段。" #: ../../library/argparse.rst:1991 msgid "" @@ -3390,14 +3918,16 @@ msgid "" "exception. This nesting was never supported, often failed to work correctly, " "and was unintentionally exposed through inheritance." msgstr "" +"在引數群組上呼叫 :meth:`add_argument_group` 現在會引發例外。這種巢狀用法從未" +"被支援,通常無法正確運作,是因為繼承而無意間被公開出來的。" #: ../../library/argparse.rst:1996 msgid "Passing prefix_chars_ to :meth:`add_argument_group` is now deprecated." -msgstr "" +msgstr "將 prefix_chars_ 傳給 :meth:`add_argument_group` 的用法現在已棄用。" #: ../../library/argparse.rst:2002 msgid "Mutual exclusion" -msgstr "" +msgstr "互斥" #: ../../library/argparse.rst:2006 msgid "" @@ -3405,6 +3935,8 @@ msgid "" "one of the arguments in the mutually exclusive group was present on the " "command line::" msgstr "" +"建立一個互斥群組。:mod:`!argparse` 會確保互斥群組中只有一個引數出現在命令列" +"上: ::" #: ../../library/argparse.rst:2010 msgid "" @@ -3438,6 +3970,8 @@ msgid "" "argument, to indicate that at least one of the mutually exclusive arguments " "is required::" msgstr "" +":meth:`add_mutually_exclusive_group` 方法也接受一個 *required* 引數,用來指示" +"至少需要一個互斥引數: ::" #: ../../library/argparse.rst:2026 msgid "" @@ -3464,6 +3998,9 @@ msgid "" "add_argument_group`. However, a mutually exclusive group can be added to an " "argument group that has a title and description. For example::" msgstr "" +"請注意,目前互斥引數群組不支援 :meth:`~ArgumentParser.add_argument_group` 的 " +"*title* 和 *description* 引數。然而互斥群組可以加入到具有標題和描述的引數群組" +"中。例如: ::" #: ../../library/argparse.rst:2040 msgid "" @@ -3508,10 +4045,13 @@ msgid "" "never supported, often failed to work correctly, and was unintentionally " "exposed through inheritance." msgstr "" +"在互斥群組上呼叫 :meth:`add_argument_group` " +"或 :meth:`add_mutually_exclusive_group` 現在會引發例外。這種巢狀用法從未支" +"援,通常無法正確運作,且是因為繼承而無意間被公開出來的。" #: ../../library/argparse.rst:2065 msgid "Parser defaults" -msgstr "" +msgstr "剖析器預設值" #: ../../library/argparse.rst:2069 msgid "" @@ -3521,6 +4061,9 @@ msgid "" "additional attributes that are determined without any inspection of the " "command line to be added::" msgstr "" +"在大多數情況下,:meth:`parse_args` 回傳物件的屬性會完全由檢查命令列引數和引" +"數 action 來決定。:meth:`set_defaults` 允許加入一些無需檢查命令列即可決定的額" +"外屬性: ::" #: ../../library/argparse.rst:2075 msgid "" @@ -3543,6 +4086,9 @@ msgid "" "are called for the same argument, the last default set for an argument is " "used::" msgstr "" +"請注意,預設值可以在剖析器層級使用 :meth:`set_defaults` 設定,也可以在引數層" +"級使用 :meth:`add_argument` 設定。如果兩者都為同一個引數呼叫,會使用最後設定" +"的預設值: ::" #: ../../library/argparse.rst:2085 msgid "" @@ -3564,12 +4110,16 @@ msgid "" "parsers. See the :meth:`~ArgumentParser.add_subparsers` method for an " "example of this type." msgstr "" +"在使用多個剖析器時,剖析器層級的預設值可能特別有用。請參" +"閱 :meth:`~ArgumentParser.add_subparsers` 方法以了解這類範例。" #: ../../library/argparse.rst:2097 msgid "" "Get the default value for a namespace attribute, as set by either :meth:" "`~ArgumentParser.add_argument` or by :meth:`~ArgumentParser.set_defaults`::" msgstr "" +"取得命名空間屬性的預設值,由 :meth:`~ArgumentParser.add_argument` " +"或 :meth:`~ArgumentParser.set_defaults` 設定: ::" #: ../../library/argparse.rst:2101 msgid "" @@ -3593,6 +4143,8 @@ msgid "" "care of formatting and printing any usage or error messages. However, " "several formatting methods are available:" msgstr "" +"在大多數典型的應用程式中,:meth:`~ArgumentParser.parse_args` 會處理格式化和印" +"出任何用法或錯誤訊息。然而,有幾個格式化方法可以使用:" #: ../../library/argparse.rst:2116 msgid "" @@ -3600,6 +4152,8 @@ msgid "" "invoked on the command line. If *file* is ``None``, :data:`sys.stdout` is " "assumed." msgstr "" +"印出 :class:`ArgumentParser` 在命令列上呼叫方式的簡短描述。如果 *file* 為 " +"``None``,則假設為 :data:`sys.stdout`。" #: ../../library/argparse.rst:2122 msgid "" @@ -3607,28 +4161,33 @@ msgid "" "arguments registered with the :class:`ArgumentParser`. If *file* is " "``None``, :data:`sys.stdout` is assumed." msgstr "" +"印出說明訊息,包括程式用法和在 :class:`ArgumentParser` 中註冊的引數相關資訊。" +"如果 *file* 為 ``None``,假設為 :data:`sys.stdout`。" #: ../../library/argparse.rst:2126 msgid "" "There are also variants of these methods that simply return a string instead " "of printing it:" -msgstr "" +msgstr "這些方法也有變體,它們只回傳字串而非印出它:" #: ../../library/argparse.rst:2131 msgid "" "Return a string containing a brief description of how the :class:" "`ArgumentParser` should be invoked on the command line." msgstr "" +"回傳一個字串,包含 :class:`ArgumentParser` 在命令列上的呼叫方式的簡短描述。" #: ../../library/argparse.rst:2136 msgid "" "Return a string containing a help message, including the program usage and " "information about the arguments registered with the :class:`ArgumentParser`." msgstr "" +"回傳一個字串,包含說明訊息,包括程式用法和在 :class:`ArgumentParser` 中註冊的" +"引數相關資訊。" #: ../../library/argparse.rst:2141 msgid "Partial parsing" -msgstr "" +msgstr "部分剖析" #: ../../library/argparse.rst:2145 msgid "" @@ -3637,6 +4196,9 @@ msgid "" "In these cases, the :meth:`~ArgumentParser.parse_known_args` method can be " "useful." msgstr "" +"有時候一個腳本只需要處理特定的一組命令列引數,將任何未辨識的引數留給另一個腳" +"本或程式。在這些情況下,:meth:`~ArgumentParser.parse_known_args` 方法可能很有" +"用。" #: ../../library/argparse.rst:2150 msgid "" @@ -3645,6 +4207,9 @@ msgid "" "parses the known arguments and returns a two item tuple that contains the " "populated namespace and the list of any unrecognized arguments." msgstr "" +"此方法的運作方式類似 :meth:`~ArgumentParser.parse_args`,但它不會對額外的、未" +"辨識的引數引發錯誤,而是會只剖析已知的引數並回傳一個包含填充後的命名空間和任" +"何未辨識引數串列的二元素元組。" #: ../../library/argparse.rst:2157 msgid "" @@ -3667,10 +4232,13 @@ msgid "" "it's just a prefix of one of its known options, instead of leaving it in the " "remaining arguments list." msgstr "" +":ref:`前綴匹配 `\\ 規則適用" +"於 :meth:`~ArgumentParser.parse_known_args`。即使選項只是其已知選項之一的前" +"綴,剖析器也可能消耗它,而非將其留在剩餘引數串列中。" #: ../../library/argparse.rst:2171 msgid "Customizing file parsing" -msgstr "" +msgstr "自訂檔案剖析" #: ../../library/argparse.rst:2175 msgid "" @@ -3679,6 +4247,9 @@ msgid "" "per line. :meth:`convert_arg_line_to_args` can be overridden for fancier " "reading." msgstr "" +"從檔案讀取的引數(請參閱 :class:`ArgumentParser` 建構函式的 " +"*fromfile_prefix_chars* 關鍵字引數)每行讀取一個引數。可以覆" +"寫 :meth:`convert_arg_line_to_args` 以進行更精細的讀取。" #: ../../library/argparse.rst:2180 msgid "" @@ -3686,12 +4257,16 @@ msgid "" "the argument file. It returns a list of arguments parsed from this string. " "The method is called once per line read from the argument file, in order." msgstr "" +"此方法接受單個引數 *arg_line*,它是從引數檔案讀取的字串。它回傳從此字串剖析出" +"的引數串列。此方法對從引數檔案讀取的每一行依序呼叫一次。" #: ../../library/argparse.rst:2184 msgid "" "A useful override of this method is one that treats each space-separated " "word as an argument. The following example demonstrates how to do this::" msgstr "" +"此方法的一個有用覆寫是將每個以空格分隔的詞作為引數。以下範例示範了如何做到這" +"一點: ::" #: ../../library/argparse.rst:2187 msgid "" @@ -3705,7 +4280,7 @@ msgstr "" #: ../../library/argparse.rst:2193 msgid "Exiting methods" -msgstr "" +msgstr "退出方法" #: ../../library/argparse.rst:2197 msgid "" @@ -3713,6 +4288,9 @@ msgid "" "if given, it prints a *message* to :data:`sys.stderr` before that. The user " "can override this method to handle these steps differently::" msgstr "" +"此方法終止程式,以指定的 *status* 退出,並在退出前(如果有提供的話)" +"向 :data:`sys.stderr` 印出一個 *message*。使用者可以覆寫此方法來以不同方式處" +"理這些步驟: ::" #: ../../library/argparse.rst:2201 msgid "" @@ -3733,10 +4311,12 @@ msgid "" "This method prints a usage message, including the *message*, to :data:`sys." "stderr` and terminates the program with a status code of 2." msgstr "" +"此方法將用法訊息(包含 *message*)印出到 :data:`sys.stderr`,並以狀態碼 2 終" +"止程式。" #: ../../library/argparse.rst:2214 msgid "Intermixed parsing" -msgstr "" +msgstr "交錯剖析" #: ../../library/argparse.rst:2219 msgid "" @@ -3745,6 +4325,9 @@ msgid "" "and :meth:`~ArgumentParser.parse_known_intermixed_args` methods support this " "parsing style." msgstr "" +"許多 Unix 命令允許使用者將可選引數與位置引數交錯使" +"用。:meth:`~ArgumentParser.parse_intermixed_args` " +"和 :meth:`~ArgumentParser.parse_known_intermixed_args` 方法支援此剖析風格。" #: ../../library/argparse.rst:2224 msgid "" @@ -3753,6 +4336,8 @@ msgid "" "subparsers, and mutually exclusive groups that include both optionals and " "positionals are not supported." msgstr "" +"這些剖析器不支援所有 :mod:`!argparse` 功能,如果使用了不支援的功能會引發例" +"外。特別是不支援子剖析器,以及同時包含可選引數和位置引數的互斥群組。" #: ../../library/argparse.rst:2229 msgid "" @@ -3761,6 +4346,9 @@ msgid "" "former returns ``['2', '3']`` as unparsed arguments, while the latter " "collects all the positionals into ``rest``. ::" msgstr "" +"以下範例展示了 :meth:`~ArgumentParser.parse_known_args` " +"和 :meth:`~ArgumentParser.parse_intermixed_args` 之間的差異:前者回傳 " +"``['2', '3']`` 作為未剖析的引數,而後者將所有位置引數收集到 ``rest`` 中。 ::" #: ../../library/argparse.rst:2235 msgid "" @@ -3789,10 +4377,14 @@ msgid "" "strings. :meth:`~ArgumentParser.parse_intermixed_args` raises an error if " "there are any remaining unparsed argument strings." msgstr "" +":meth:`~ArgumentParser.parse_known_intermixed_args` 回傳一個二元素元組,包含" +"填充後的命名空間和剩餘引數字串的串" +"列。:meth:`~ArgumentParser.parse_intermixed_args` 在有任何剩餘未剖析的引數字" +"串時會引發錯誤。" #: ../../library/argparse.rst:2253 msgid "Registering custom types or actions" -msgstr "" +msgstr "註冊自訂型別或 action" #: ../../library/argparse.rst:2257 msgid "" @@ -3801,6 +4393,9 @@ msgid "" "register custom actions or types with a parser and allow you to reference " "the type by their registered name instead of their callable name." msgstr "" +"有時候想要在錯誤訊息中使用自訂字串以提供更友善的輸出。在這些情況下,可以使" +"用 :meth:`!register` 向剖析器註冊自訂 action 或型別,讓你可以透過註冊名稱而非" +"可呼叫物件名稱來參照型別。" #: ../../library/argparse.rst:2262 msgid "" @@ -3809,11 +4404,14 @@ msgid "" "``action``, ``type``), *value*, which is the key under which the object will " "be registered, and object, the callable to be registered." msgstr "" +":meth:`!register` 方法接受三個引數——*registry_name*,指定用於儲存物件的內部登" +"錄檔(例如 ``action``、``type``);*value*,用於註冊物件的鍵值;以及 object," +"要註冊的可呼叫物件。" #: ../../library/argparse.rst:2267 msgid "" "The following example shows how to register a custom type with a parser::" -msgstr "" +msgstr "以下範例展示如何向剖析器註冊自訂型別: ::" #: ../../library/argparse.rst:2269 msgid "" @@ -3849,22 +4447,22 @@ msgstr "例外" #: ../../library/argparse.rst:2285 msgid "An error from creating or using an argument (optional or positional)." -msgstr "" +msgstr "建立或使用引數(可選或位置引數)時的錯誤。" #: ../../library/argparse.rst:2287 msgid "" "The string value of this exception is the message, augmented with " "information about the argument that caused it." -msgstr "" +msgstr "此例外的字串值是訊息,附帶了導致錯誤的引數的相關資訊。" #: ../../library/argparse.rst:2292 msgid "" "Raised when something goes wrong converting a command line string to a type." -msgstr "" +msgstr "當將命令列字串轉換為型別時出錯時引發。" #: ../../library/argparse.rst:2296 msgid "Guides and Tutorials" -msgstr "" +msgstr "指南與教學" #: ../../library/argparse.rst:908 msgid "? (question mark)" @@ -3882,34 +4480,3 @@ msgstr "* (星號)" #: ../../library/argparse.rst:954 msgid "+ (plus)" msgstr "+ (加號)" - -#~ msgid "" -#~ ">>> parser = argparse.ArgumentParser()\n" -#~ ">>> parser.add_argument('infile', nargs='?', type=argparse." -#~ "FileType('r'),\n" -#~ "... default=sys.stdin)\n" -#~ ">>> parser.add_argument('outfile', nargs='?', type=argparse." -#~ "FileType('w'),\n" -#~ "... default=sys.stdout)\n" -#~ ">>> parser.parse_args(['input.txt', 'output.txt'])\n" -#~ "Namespace(infile=<_io.TextIOWrapper name='input.txt' encoding='UTF-8'>,\n" -#~ " outfile=<_io.TextIOWrapper name='output.txt' " -#~ "encoding='UTF-8'>)\n" -#~ ">>> parser.parse_args([])\n" -#~ "Namespace(infile=<_io.TextIOWrapper name='' encoding='UTF-8'>,\n" -#~ " outfile=<_io.TextIOWrapper name='' encoding='UTF-8'>)" -#~ msgstr "" -#~ ">>> parser = argparse.ArgumentParser()\n" -#~ ">>> parser.add_argument('infile', nargs='?', type=argparse." -#~ "FileType('r'),\n" -#~ "... default=sys.stdin)\n" -#~ ">>> parser.add_argument('outfile', nargs='?', type=argparse." -#~ "FileType('w'),\n" -#~ "... default=sys.stdout)\n" -#~ ">>> parser.parse_args(['input.txt', 'output.txt'])\n" -#~ "Namespace(infile=<_io.TextIOWrapper name='input.txt' encoding='UTF-8'>,\n" -#~ " outfile=<_io.TextIOWrapper name='output.txt' " -#~ "encoding='UTF-8'>)\n" -#~ ">>> parser.parse_args([])\n" -#~ "Namespace(infile=<_io.TextIOWrapper name='' encoding='UTF-8'>,\n" -#~ " outfile=<_io.TextIOWrapper name='' encoding='UTF-8'>)"