@@ -1447,7 +1447,8 @@ msgid ""
1447
1447
"BooleanOptionalAction` is available in :mod:`!argparse` and adds support for "
1448
1448
"boolean actions such as ``--foo`` and ``--no-foo``::"
1449
1449
msgstr ""
1450
- ""
1450
+ "你也可以透過傳入一個 :class:`Action` 子類別或其他任意時做了相同界面的物件來指定一個任意動作。:class:`!"
1451
+ "BooleanOptionalAction` 在 :mod:`!argparse` 中可用,且支援如 ``--foo`` 與 ``--no-foo`` 樣式的布林動作:::"
1451
1452
1452
1453
#: ../../library/argparse.rst:745
1453
1454
msgid ""
@@ -1469,10 +1470,12 @@ msgid ""
1469
1470
"overriding the :meth:`!__call__` method and optionally the :meth:`!__init__` "
1470
1471
"and :meth:`!format_usage` methods."
1471
1472
msgstr ""
1473
+ "擴充 :class:`Action`,覆寫其中的 :meth:`!__call__` 方法、以及非必要的 :meth:`!__init__` 、"
1474
+ ":meth:`!format_usage` 等方法是目前自創一個動作時的推薦方式。"
1472
1475
1473
1476
#: ../../library/argparse.rst:757
1474
1477
msgid "An example of a custom action::"
1475
- msgstr ""
1478
+ msgstr "以下是一個自訂動作的範例::: "
1476
1479
1477
1480
#: ../../library/argparse.rst:759
1478
1481
msgid ""
@@ -1494,10 +1497,27 @@ msgid ""
1494
1497
">>> args\n"
1495
1498
"Namespace(bar='1', foo='2')"
1496
1499
msgstr ""
1500
+ ">>> class FooAction(argparse.Action):\n"
1501
+ "... def __init__(self, option_strings, dest, nargs=None, **kwargs):\n"
1502
+ "... if nargs is not None:\n"
1503
+ "... raise ValueError(\" 不允許 nargs 有值\" )\n"
1504
+ "... super().__init__(option_strings, dest, **kwargs)\n"
1505
+ "... def __call__(self, parser, namespace, values, option_string=None):\n"
1506
+ "... print('%r %r %r' % (namespace, values, option_string))\n"
1507
+ "... setattr(namespace, self.dest, values)\n"
1508
+ "...\n"
1509
+ ">>> parser = argparse.ArgumentParser()\n"
1510
+ ">>> parser.add_argument('--foo', action=FooAction)\n"
1511
+ ">>> parser.add_argument('bar', action=FooAction)\n"
1512
+ ">>> args = parser.parse_args('1 --foo 2'.split())\n"
1513
+ "Namespace(bar=None, foo=None) '1' None\n"
1514
+ "Namespace(bar='1', foo=None) '2' '--foo'\n"
1515
+ ">>> args\n"
1516
+ "Namespace(bar='1', foo='2')"
1497
1517
1498
1518
#: ../../library/argparse.rst:777
1499
1519
msgid "For more details, see :class:`Action`."
1500
- msgstr "更多詳情請見 :class:`Action`。"
1520
+ msgstr "詳情請見 :class:`Action`。"
1501
1521
1502
1522
#: ../../library/ar
8000
gparse.rst:783
1503
1523
msgid "nargs"
@@ -1511,12 +1531,15 @@ msgid ""
1511
1531
"action. See also :ref:`specifying-ambiguous-arguments`. The supported values "
1512
1532
"are:"
1513
1533
msgstr ""
1534
+ ":class:`ArgumentParser` 一般來說會為每一個命令列選項分配一個動作。 ``nargs`` 關鍵字引數可使多個選項共用同一個動作。"
1535
+ "請另參閱 :ref:`specifying-ambiguous-arguments`。 本引數支援的值有:"
1514
1536
1515
1537
#: ../../library/argparse.rst:790
1516
1538
msgid ""
1517
1539
"``N`` (an integer). ``N`` arguments from the command line will be gathered "
1518
1540
"together into a list. For example::"
1519
1541
msgstr ""
1542
+ "``N`` (一個整數。) ``N`` 個來自命令的引數會被集成至一個串列(list)中。例如:::"
1520
1543
1521
1544
#: ../../library/argparse.rst:793
1522
1545
msgid ""
@@ -1537,6 +1560,7 @@ msgid ""
1537
1560
"Note that ``nargs=1`` produces a list of one item. This is different from "
1538
1561
"the default, in which the item is produced by itself."
1539
1562
msgstr ""
1563
+ "注意到 ``nargs=1`` 會產生含有一個元素的串列。與預設情況不同,預設情況下只會產生該選項的值本身。"
1540
1564
1541
1565
#: ../../library/argparse.rst:804
1542
1566
msgid ""
@@ -1547,6 +1571,8 @@ msgid ""
1547
1571
"by a command-line argument. In this case the value from const_ will be "
1548
1572
"produced. Some examples to illustrate this::"
1549
1573
msgstr ""
1574
+ "``'?'``。 若情況允許,會從命令中消耗一個引數,並產生一個物件。如果沒有任何可用的引數, default_ 的預設值會被使用。"
1575
+ "請注意,若是可選引數則還有其他可能—如果選項字串存在,但後方卻沒有任何來自命令的引數,則 const_ 中的值會被作為預設值使用。以下的範例展示了這個行為:::"
1550
1576
1551
1577
#: ../../library/argparse.rst:811
1552
1578
msgid ""
@@ -1575,6 +1601,7 @@ msgid ""
1575
1601
"One of the more common uses of ``nargs='?'`` is to allow optional input and "
1576
1602
"output files::"
1577
1603
msgstr ""
1604
+ "其中一個最常見的 ``nargs='?'`` 用法是令指定輸入或輸出檔案的引數成為可選項:::"
1578
1605
1579
1606
#: ../../library/argparse.rst:824
1580
1607
msgid ""
@@ -1609,6 +1636,7 @@ msgid ""
1609
1636
"argument with ``nargs='*'``, but multiple optional arguments with "
1610
1637
"``nargs='*'`` is possible. For example::"
1611
1638
msgstr ""
1639
+ "``'*'``。 所有可用的命令列引數都會被蒐集至一個串列(list)中。一般來說不會讓一個以上的選項具有 ``nargs='*'``,但確實可以做到讓數個選項同時具有 ``nargs='*'``。請見下例:::"
1612
1640
1613
1641
#: ../../library/argparse.rst:843
1614
1642
msgid ""
@@ -1632,6 +1660,8 @@ msgid ""
1632
1660
"a list. Additionally, an error message will be generated if there wasn't at "
1633
1661
"least one command-line argument present. For example::"
1634
1662
msgstr ""
1663
+ "``'+'``. 與 ``'*'`` 很像,所有可用的命令列引數都會被收集到一個串列(llist)中。"
1664
+ "除此之外,如果可用的引數少於一個則會產生一個錯誤訊息。請見下例:::"
1635
1665
1636
1666
#: ../../library/argparse.rst:856
1637
1667
msgid ""
0 commit comments