PEP 389: argparse - æ–°ã—ã„コマンドラインparseモジュール ========================================================== 原文: http://www.python.org/dev/peps/pep-0389/ 訳注: argparseã®å…¬å¼ãƒ‰ã‚ュメント㯠http://argparse.googlecode.com/svn/trunk/doc/index.html +----------------+--------------------------------------------+ | PEP | 389 | +----------------+--------------------------------------------+ | Title | argparse - New Command Line Parsing Module | +----------------+--------------------------------------------+ | Version | 78618 | +----------------+--------------------------------------------+ | Last-Modified | 2010-03-03 03:55:24 +0100(Wed, 03 Mar 2010)| +----------------+--------------------------------------------+ | Author | Steven Bethard <steven.bethard@gmail.com> | +----------------+--------------------------------------------+ | Status | Accepted | +----------------+--------------------------------------------+ | Type | Standards Track | +----------------+--------------------------------------------+ | Content-Type | text/x-rst | +----------------+--------------------------------------------+ | Created | 25-Sep-2009 | +----------------+--------------------------------------------+ | Python-Version | 2.7 and 3.2 | +----------------+--------------------------------------------+ | Post-History | 27-Sep-2009, 24-Oct-2009 | +----------------+--------------------------------------------+ .. Acceptance ========== æ‰¿èª ---------- .. This PEP was approved by Guido on python-dev on February 21, 2010 [17]_. ã“ã®PEPã¯Guidoã«ã‚ˆã£ã¦ã€2010å¹´4月21æ—¥ã®python-devã«ã¦æ‰¿èªã•れã¾ã—ãŸã€‚ [17]_ .. Abstract ------== æ¦‚è¦ -------- .. This PEP proposes inclusion of the argparse [1]_ module in the Python standard library in Python 2.7 and 3.2. ã“ã®PEPã§ã¯ã€Python 2.7ã¨3.2ã§æ¨™æº–ライブラリã«åŠ ã‚ã£ãŸargparse [1]_ モ ジュールã«é–¢ã—ã¦èª¬æ˜Žã—ã¾ã™ã€‚ .. Motivation ------==== モãƒãƒ™ãƒ¼ã‚·ãƒ§ãƒ³ -------------- .. The argparse module is a command line parsing library which provides more functionality than the existing command line parsing modules in the standard library, getopt [2]_ and optparse [3]_. It includes support for positional arguments (not just options), subcommands, required options, options syntaxes like "/f" and "+rgb", zero-or-more and one-or-more style arguments, and many other features the other two lack. argparseãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã¯æ—¢å˜ã®ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³å¼•数を解釈ã™ã‚‹ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã§ã‚ã‚‹ getopt [2]_ ã¨optparse [3]_ よりも高機能ãªparseライブラリã§ã™ã€‚argparseã¯ã€ å˜ãªã‚‹ã‚ªãƒ—ションã ã‘ã§ãªã固定引数(positional argument)ã€ã‚µãƒ–コマンドã€å¿… é ˆã‚ªãƒ—ã‚·ãƒ§ãƒ³ã€"/f"ã‚„"+rgb"ã¨ã„ã£ãŸã‚ªãƒ—ション文法ã€0以上や1以上ã¨ã„ã†å½¢å¼ ã®å¼•æ•°ã€ãã®ä»–getoptã¨optparseãŒæŒã£ã¦ã„ãªã„å¤šæ•°ã®æ©Ÿèƒ½ã‚’æä¾›ã—ã¾ã™ã€‚ .. The argparse module is also already a popular third-party replacement for these modules. It is used in projects like IPython (the Scipy Python shell) [4]_, is included in Debian testing and unstable [5]_, and since 2007 has had various requests for its inclusion in the standard library [6]_ [7]_ [8]_. This popularity suggests it may be a valuable addition to the Python libraries. argparseモジュールã¯ã“れらã®ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã‚’ç½®ãæ›ãˆã‚‹ã‚µãƒ¼ãƒ‰ãƒ‘ーティ製ã®ãƒ¢ ジュールã¨ã—ã¦ã™ã§ã«äººæ°—ãŒã‚りã¾ã™ã€‚argparseã¯IPython(Scipy Python Shell) [4]_ ã§ä½¿ã‚れã¦ãŠã‚Šã€Debianã®testingã¨usntable [5]_ ã«å«ã¾ã‚Œã¦ãŠ ã‚Šã€2007å¹´ã‹ã‚‰æ¨™æº–ライブラリã«çµ„ã¿è¾¼ã‚€ã‚ˆã†ãŸãã•ã‚“ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆã‚’å—ã‘å–ã£ ã¦æ¥ã¾ã—㟠[6]_ [7]_ [8]_ 。ã“れã¯Pythonã®ãƒ©ã‚¤ãƒ–ラリã«è¿½åŠ ã™ã‚‹ä¾¡å€¤ãŒã‚ã‚‹ ã“ã¨ã‚’示唆ã—ã¦ã„ã¾ã™ã€‚ .. Why aren't getopt and optparse enough? ====================================== ãªãœgetoptã¨optparseã¯å分ã§ã¯ãªã„ã®ï¼Ÿ --------------------------------------- .. One argument against adding argparse is that thare are "already two different option parsing modules in the standard library" [9]_. The following is a list of features provided by argparse but not present in getopt or optparse: argparseã«å¯¾ã™ã‚‹è°è«–ã®ä¸€ã¤ã¨ã—ã¦ã€Œã™ã§ã«ã‚‚ã†äºŒã¤ã®ç•°ãªã‚‹OptionパーザãŒã‚ ã‚‹ã˜ã‚ƒãªã„ã‹ [9]_ ã€ã¨ã„ã†ã‚‚ã®ãŒã‚りã¾ã™ã€‚以下ã«argparseã«ã¯ã‚ã‚‹ãŒä»–ã® getoptã¨optparseã«ã¯ãªã„機能ã®ãƒªã‚¹ãƒˆã‚’記ã—ã¾ã™ã€‚ .. * While it is true there are two *option* parsing libraries, there are no full command line parsing libraries -- both getopt and optparse support only options and have no support for positional arguments. The argparse module handles both, and as a result, is able to generate better help messages, avoiding redundancies like the ``usage=`` string usually required by optparse. * æ—¢ã«äºŒã¤ã®*option* パーザライブラリãŒã‚ã‚‹ã“ã¨ã¯ç¢ºã‹ã§ã™ãŒã€å®Œå…¨ãªã‚³ãƒžãƒ³ ドラインパーズライブラリã¯ã‚りã¾ã›ã‚“。getoptã¨optparseã®ä¸¡æ–¹ã¨ã‚‚〠optionã—ã‹ã‚µãƒãƒ¼ãƒˆã—ã¦ãŠã‚‰ãšã€å›ºå®šå¼•æ•°(positional argument)をサãƒãƒ¼ãƒˆã— ã¦ã„ã¾ã›ã‚“。argparseã¯ä¸¡æ–¹ã‚’扱ã†ã“ã¨ãŒã§ãã€çµæžœã¨ã—ã¦ã€ã‚ˆã‚Šè‰¯ã„ヘルプ メッセージを生æˆã§ãã€optparseã§å¿…è¦ã«ãªã£ã¦ãã‚‹ ``usage=`` æ–‡å—列ã¨ã„㣠ãŸå†—é•·ãªè¨˜è¿°ã‚’é¿ã‘られã¾ã™ã€‚ .. * The argparse module values practicality over purity. Thus, argparse allows required options and customization of which characters are used to identify options, while optparse explicitly states "the phrase 'required option' is self-contradictory" and that the option syntaxes ``-pf``, ``-file``, ``+f``, ``+rgb``, ``/f`` and ``/file`` "are not supported by optparse, and they never will be". * argparseモジュールã¯ç´”æ£ã‚ˆã‚Šã‚‚実用性を評価ã—ã¾ã™ã€‚従ã£ã¦ã€optparse㌠「 'required option'ã¯è‡ªå·±çŸ›ç›¾ã§ã‚り〠``-pf``, ``-file``, ``+f``, ``+rgb``, ``/f``, ``/file`` ã¯optparseã§ã‚µãƒãƒ¼ãƒˆã•れãšã€ä»Šå¾Œã‚‚サãƒãƒ¼ãƒˆã• れãªã„ã€ã¨æ˜Žç¢ºã«è¨˜è¼‰ã•れã¦ã„ã‚‹ã®ã¨ç•°ãªã‚Šã€argparseã¯å¿…è¦ã¨ãªã‚‹ã‚ªãƒ—ショ ンã¨ã‚ªãƒ—ションをè˜åˆ¥ã™ã‚‹ã®ã«ä½¿ã‚れる文å—ã®ã‚«ã‚¹ã‚¿ãƒžã‚¤ã‚ºãŒã§ãã¾ã™ã€‚ .. * The argparse module allows options to accept a variable number of arguments using ``nargs='?'``, ``nargs='*'`` or ``nargs='+'``. The optparse module provides an untested recipe for some part of this functionality [10]_ but admits that "things get hairy when you want an option to take a variable number of arguments." * argparse㯠``nargs='?'``, ``nargs='*'`` ã‚ã‚‹ã„㯠``nargs='+'`` を使ã£ã¦ å¯å¤‰é•·å¼•数を記述ã§ãã¾ã™ã€‚optparseã¯ã“ã®æ©Ÿèƒ½ã®ä¸€éƒ¨ã‚’テストã•れã¦ã„ãªã„ レシピを使ã£ã¦æä¾›ã—ã¦ã„ã¾ã™ãŒ [10]_ ã€ã€Œå¯å¤‰é•·å¼•数を使ãŠã†ã¨ã™ã‚‹ã¨ã‚„ã° ã„ã“ã¨ã«ãªã‚Šã‹ããªã„よã€ã¨èªã‚ã¦ã„ã¾ã™ã€‚ .. * The argparse module supports subcommands, where a main command line parser dispatches to other command line parsers depending on the command line arguments. This is a common pattern in command line interfaces, e.g. ``svn co`` and ``svn up``. * argparseã¯ã‚µãƒ–コマンドをサãƒãƒ¼ãƒˆã—ã¦ã„ã¾ã™ã€‚ã“れã¯ã€ä¸»ã¨ãªã‚‹ã‚³ãƒžãƒ³ãƒ‰ãƒ© インパーザãŒä¾å˜ã™ã‚‹å¼•æ•°ã‚’ä»–ã®ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³ãƒ‘ーザã«é€ã‚Šã¾ã™ã€‚ã“れã¯ã€ コマンドラインインタフェースã§ã¯ã‚ˆã使ã‚れã¦ã„ã¾ã™ã€‚ 例: ``svn co`` ã‚„ ``svn up`` .. Why isn't the functionality just being added to optparse? ========================================================= optparseã«æ©Ÿèƒ½ã‚’è¿½åŠ ã™ã‚‹ã®ã§ã¯ãªãœã ã‚ãªã®ï¼Ÿ ----------------------------------------------------- .. Clearly all the above features offer improvements over what is available through optparse. A reasonable question then is why these features are not simply provided as patches to optparse, instead of introducing an entirely new module. In fact, the original development of argparse intended to do just that, but because of various fairly constraining design decisions of optparse, this wasn't really possible. Some of the problems included: 明らã‹ã«ã€ä»¥ä¸Šè¿°ã¹ã¦ããŸæ©Ÿèƒ½è¿½åŠ ã¯optparseã«è¿½åŠ ã—ã¦å®Ÿç¾ã§ãる許容é‡ã‚’ è¶Šãˆã¦ã„ã¾ã™ã€‚利ã«ã‹ãªã£ãŸè³ªå•ã¯ã€ã“ã‚Œã‚‰ã®æ©Ÿèƒ½ã¯ã¾ã£ãŸãæ–°ã—ã„モジュー ルã¨ã—ã¦ã§ã¯ãªãã€ãªãœå˜ç´”ã«optparseã¸ã®patchã¨ã—ã¦æä¾›ã•れãªã„ã®ã§ã™ã‹ã€ ã§ã™ã€‚事実ã€argparseã®é–‹ç™ºåˆæœŸæ®µéšŽã¯patchã¨ã—ã¦è¡Œã‚れã¦ã„ãŸã®ã§ã™ãŒã€ optparseã®è¨è¨ˆã‹ã‚‰ãã‚‹ã•ã¾ã–ã¾ãªåˆ¶ç´„ã«ã‚ˆã‚Šã€ã“れã¯ä¸å¯èƒ½ã ã£ãŸã®ã§ã™ã€‚ 以下ã«ç¤ºã™ã„ãã¤ã‹ã®å•題ãŒã‚りã¾ã—ãŸã€‚ .. * The optparse module exposes the internals of its parsing algorithm. In particular, ``parser.largs`` and ``parser.rargs`` are guaranteed to be available to callbacks [11]_. This makes it extremely difficult to improve the parsing algorithm as was necessary in argparse for proper handling of positional arguments and variable length arguments. For example, ``nargs='+'`` in argparse is matched using regular expressions and thus has no notion of things like ``parser.largs``. * optparseモジュールã¯å†…部ã®ãƒ‘ースアルゴリズムを公開ã—ã¦ã„ã¾ã™ã€‚特ã«ã€ ``parser.largs`` 㨠``parser.rargs`` ã¯ã‚³ãƒ¼ãƒ«ãƒãƒƒã‚¯ãŒã‚ã‚‹ã“ã¨ã‚’剿ã¨ã— ã¦ã„ã¾ã™ [11]_ 。ã“ã®ã“ã¨ã¯ã€å›ºå®šå¼•æ•°ã‚„å¯å¤‰é•·å¼•æ•°ã«å¯¾å¿œã™ã‚‹ãŸã‚ã« argparseã§ãƒ‘ースアルゴリズムを改良ã™ã‚‹ã“ã¨ã‚’阻害ã—ã¦ã„ã¾ã™ã€‚例ãˆã°ã€ argparseã§ ``nargs='+'`` を使ã£ã¦æ£è¦è¡¨ç¾ã«ãƒžãƒƒãƒã•ã›ã‚‹æ™‚ã¯ã€ ``parser.largs`` ãªã©ã‚’ä½¿ã†æ„図ã¯ã‚りã¾ã›ã‚“。(訳注:訳ã«è‡ªä¿¡ãªã—) .. * The optparse extension APIs are extremely complex. For example, just to use a simple custom string-to-object conversion function, you have to subclass ``Option``, hack class attributes, and then specify your custom option type to the parser, like this:: * optparseã®æ‹¡å¼µAPIã¯éžå¸¸ã«è¤‡é›‘ã§ã™ã€‚例ãˆã°ã€ç°¡å˜ãªstringã‹ã‚‰objectã¸ã¨ 変æ›ã™ã‚‹è‡ªä½œã®é–¢æ•°ã‚’使ãŠã†ã¨ã™ã‚‹ã¨ãã§ã‚‚〠``Option`` サブクラスを作 りã€ã‚¯ãƒ©ã‚¹å¤‰æ•°ã‚’ã„ã˜ã‚Šã€è‡ªä½œã®ã‚ªãƒ—ションタイプをparserã«æŒ‡å®šã™ã‚‹å¿…è¦ ãŒã‚ã‚‹ã®ã§ã™ã€‚例を示ã—ã¾ã™ã€‚:: class MyOption(Option): TYPES = Option.TYPES + ("mytype",) TYPE_CHECKER = copy(Option.TYPE_CHECKER) TYPE_CHECKER["mytype"] = check_mytype parser = optparse.OptionParser(option_class=MyOption) parser.add_option("-m", type="mytype") .. For comparison, argparse simply allows conversion functions to be used as ``type=`` arguments directly, e.g.:: 比較ã™ã‚‹ã¨ã€argparseã¯å˜ã«å¤‰æ›é–¢æ•°ã‚’ ``type=`` 引数ã«ç›´æŽ¥ä¸Žãˆã‚‹ã ã‘ã§æ¸ˆã¿ ã¾ã™ã€‚ :: parser = argparse.ArgumentParser() parser.add_option("-m", type=check_mytype) .. But given the baroque customization APIs of optparse, it is unclear how such a feature should interact with those APIs, and it is quite possible that introducing the simple argparse API would break existing custom Option code. 与ãˆã‚‰ã‚ŒãŸoptparseã®å¤å…¸çš„ãªã‚«ã‚¹ã‚¿ãƒ APIã§ã¯ã€ã“ã®ã‚ˆã†ãªæ©Ÿèƒ½ãŒã“れら㮠APIã‚’ã©ã®ã‚ˆã†ã«ä½¿ãˆã°ã„ã„ã®ã‹ä¸æ˜Žç¢ºã§ã™ã€‚ãã®ãŸã‚ã€å˜ç´”ãªargparseã®API ã‚’å°Žå…¥ã™ã‚‹ã¨ã€æ—¢å˜ã®è‡ªä½œã®Optionコードを壊ã™å¯èƒ½æ€§ãŒé«˜ã„ã§ã™ã€‚ .. * Both optparse and argparse parse command line arguments and assign them as attributes to an object returned by ``parse_args``. However, the optparse module guarantees that the ``take_action`` method of custom actions will always be passed a ``values`` object which provides an ``ensure_value`` method [12]_, while the argparse module allows attributes to be assigned to any object, e.g.:: * optparseã¨argparseã¯ä¸¡æ–¹å…±ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³ã®å¼•数をパーズã—〠``parse_args`` ã«ã‚ˆã£ã¦è¿”ã•れãŸã‚ªãƒ–ジェクトã®å±žæ€§ã‚’è¨å®šã—ã¾ã™ã€‚ã—ã‹ã—〠optparseモジュールã¯è‡ªä½œã®ã‚¢ã‚¯ã‚·ãƒ§ãƒ³ã§ã‚ã‚‹ ``take_action`` メソッド㫠``ensure_value`` メソッドをæŒã¤ ``values`` オブジェクトãŒå¸¸ã«æ¸¡ã•れる㓠ã¨ã‚’剿ã¨ã—ã¦ã„ã¾ã™ [12]_ 。一方ã€argparseã¯ã©ã‚“ãªã‚ªãƒ–ジェクトã«ã§ã‚‚属 性をè¨å®šã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚例 :: foo_object = ... parser.parse_args(namespace=foo_object) foo_object.some_attribute_parsed_from_command_line .. Modifying optparse to allow any object to be passed in would be difficult because simply passing the ``foo_object`` around instead of a ``Values`` instance will break existing custom actions that depend on the ``ensure_value`` method. optparseã‚’ã©ã‚“ãªã‚ªãƒ–ジェクトã§ã‚‚å—ã‘入れるよã†ã«å¤‰æ›´ã™ã‚‹ã“ã¨ã¯é›£ã—ã„ã“ ã¨ã§ã—ãŸã€‚ãªãœãªã‚‰ã€å˜ã« ``Values`` インスタンスã®ä»£ã‚り㫠``foo_object`` を渡ã™ã“ã¨ã¯ ``ensure_value`` メソッドã«ä¾å˜ã—ã¦ã„ã‚‹æ—¢å˜ ã®è‡ªä½œã‚¢ã‚¯ã‚·ãƒ§ãƒ³ã‚’壊ã™ã‹ã‚‰ã§ã™ã€‚ .. Because of issues like these, which made it unreasonably difficult for argparse to stay compatible with the optparse APIs, argparse was developed as an independent module. Given these issues, merging all the argparse features into optparse with no backwards incompatibilities seems unlikely. ã“ã®ã‚ˆã†ãªå•題ã«ã‚ˆã‚Šã€argparseãŒoptparse APIã¨äº’æ›æ€§ã‚’ä¿ã£ãŸã¾ã¾ã«ã™ã‚‹ ã“ã¨ã¯é›£ã—ãã€argparseã¯ç‹¬ç«‹ã—ãŸãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã¨ã—ã¦é–‹ç™ºã•れã¾ã—ãŸã€‚付ã‘åŠ ãˆã‚‹ã¨ã€åŒã˜ç†ç”±ã«ã‚ˆã‚Šargparseã®å…¨ã¦ã®æ©Ÿèƒ½ã‚’å¾Œæ–¹äº’æ›æ€§ã‚’ä¿ã£ãŸã¾ã¾ optparseã«å…¥ã‚Œã‚‹ã“ã¨ã¯è€ƒãˆã‚‰ã‚Œã¾ã›ã‚“。 .. Deprecation of optparse ======================= optparseã®å»ƒæ¢ ----------------------- .. Because all of optparse's features are available in argparse, the optparse module will be deprecated. However, because of the widespread use of optparse, the deprecation strategy contains only documentation changes and warnings that will not be visible by default: argparseã§ã€optparseã®å…¨ã¦ã®æ©Ÿèƒ½ãŒå®Ÿç¾ã•れã¦ã„ã‚‹ãŸã‚ã€optparseモジュール ã¯å°†æ¥çš„ã«å»ƒæ¢ã•れã¾ã™ã€‚ã—ã‹ã—ã€optparseã¯åºƒã使用ã•れã¦ã„ã‚‹ãŸã‚ã€å»ƒæ¢ã® é“ç‹ã¯å˜ã«æ–‡æ›¸ã®å¤‰æ›´ã ã‘ã§ã‚ã‚Šã€æ¨™æº–ã§ã¯è¦å‘Šã¯è¡¨ç¤ºã•れã¾ã›ã‚“。 .. * Python 2.7+ and 3.2+ -- The following note will be added to the .. optparse documentation: .. The optparse module is deprecated and will not be developed further; development will continue with the argparse module. * Python 2.7以é™ã¨3.2ä»¥é™ -- optparseã®æ–‡ç« ã«ä»¥ä¸‹ã®æ³¨æ„書ããŒä»˜ã‘åŠ ãˆã‚‰ れã¾ã™ã€‚ optparseモジュールã¯å»ƒæ¢ã•れã€ä»¥é™é–‹ç™ºã•れã¾ã›ã‚“。以é™ã®é–‹ç™ºã¯ argparseモジュールã§ç¶™ç¶šã•れã¾ã™ã€‚ .. * Python 2.7+ -- If the Python 3 compatibility flag, ``-3``, is provided at the command line, then importing optparse will issue a DeprecationWarning. Otherwise no warnings will be issued. * Python 2.7ä»¥é™ -- ã‚‚ã—Python 3ã®äº’æ›æ€§ãƒ•ラグ(``-3``)ãŒä¸Žãˆã‚‰ã‚ŒãŸå ´åˆã€ optparseã¯DeprecationWarningã¨ã„ã†è¦å‘Šã‚’出ã—ã¾ã™ã€‚ãã†ã§ãªã‘れã°ã€è¦å‘Š ã¯ãªã«ã‚‚出ã¾ã›ã‚“。 .. * Python 3.2+ -- Importing optparse will issue a PendingDeprecationWarning, which is not displayed by default. * Python 3.2以上 -- optparseã‚’importã™ã‚‹ã¨ã€PendingDeprecationWarning㨠ã„ã†ã€æ¨™æº–ã§ã¯è¡¨ç¤ºã•れãªã„è¦å‘ŠãŒå‡ºã¾ã™ã€‚ .. Note that no removal date is proposed for optparse. optparseãŒå‰Šé™¤ã•れる日付ã«é–¢ã—ã¦ã¯ãªã«ã‚‚決ã¾ã£ã¦ã„ãªã„ã“ã¨ã«æ³¨æ„ã—ã¦ãã ã•ã„。 .. Updates to getopt documentation =============================== getoptã®æ–‡æ›¸ã®æ›´æ–° ------------------------------- .. The getopt module will not be deprecated. However, its documentation will be updated to point to argparse in a couple of places. At the top of the module, the following note will be added: The getopt module is a parser for command line options whose API is designed to be familiar to users of the C getopt function. Users who are unfamiliar with the C getopt function or who would like to write less code and get better help and error messages should consider using the argparse module instead. getoptモジュールã¯å»ƒæ¢ã•れã¾ã›ã‚“。ã—ã‹ã—ã€ãã®æ–‡æ›¸ã®ã„ãã¤ã‹ã®å ´æ‰€ã§ã¯ argparseを示ã™ã‚ˆã†ã«æ›´æ–°ã•れã¾ã™ã€‚モジュールã®å…ˆé ã«ã¯ä»¥ä¸‹ã®æ³¨æ„書ã㌠付ã‘åŠ ãˆã‚‰ã‚Œã¾ã™ã€‚ :: getoptモジュールã¯C言語ã®getopt関数ã®APIã«æ…£ã‚Œè¦ªã—ã‚“ã ユーザã®ãŸã‚ã«ä½œ られãŸã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³ã‚ªãƒ—ションã®ãƒ‘ーズモジュールã§ã™ã€‚C言語ã®getopt関数 ã«ä¸æ…£ã‚Œãªã€ã‚ã‚‹ã„ã¯å°‘ãªã„コードé‡ã§ã‚ˆã‚Šè‰¯ã„ヘルプã¨ã‚¨ãƒ©ãƒ¼ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’ å¾—ãŸã„ユーザã¯ã€ä»£ã‚りã«argparseモジュールを使ã£ã¦ãã ã•ã„。 .. Additionally, after the final getopt example, the following note will be added: Note that an equivalent command line interface could be produced with less code by using the argparse module:: ã•らã«ã€æœ€å¾Œã®getoptã®ä¾‹ã®å¾Œã«ã€ä»¥ä¸‹ã®æ³¨æ„書ããŒä»˜ã‘åŠ ãˆã‚‰ã‚Œã¾ã™ã€‚ :: å°‘ãªã„コードé‡ã§åŒç‰ã®ã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³ã‚¤ãƒ³ã‚¿ãƒ•ェースãŒã€argparseモジュールを使ã†ã“ã¨ã§å¾—られã¾ã™ã€‚ import argparse if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-o', '--output') parser.add_argument('-v', dest='verbose', action='store_true') args = parser.parse_args() # ... do something with args.output ... # ... do something with args.verbose .. .. Deferred: string formatting =========================== 延期: æ–‡å—列フォーマット --------------------------- .. The argparse module supports Python from 2.3 up through 3.2 and as a result relies on traditional ``%(foo)s`` style string formatting. It has been suggested that it might be better to use the new style ``{foo}`` string formatting [13]_. There was some discussion about how best to do this for modules in the standard library [14]_ and several people are developing functions for automatically converting %-formatting to {}-formatting [15]_ [16]_. When one of these is added to the standard library, argparse will use them to support both formatting styles. argparseモジュールã¯Python 2.3ã‹ã‚‰3.2ã¾ã§ã‚µãƒãƒ¼ãƒˆã—ã¾ã™ãŒã€ãã®ãŸã‚ã€ä¼çµ± 的㪠``%(foo)s`` å½¢å¼ã®æ–‡å—列フォーマットã«ä¾å˜ã—ã¾ã™ã€‚ã“ã‚Œã¯æ–°ã—ã„å½¢å¼ã® ``{foo}`` フォーマットを使ã†ã‚ˆã†ã«æŽ¨å¥¨ã•れã¦ã„ã¾ã™ [13]_ 。標準ライブラリ ã§ã¯ã“ã®å•題ã«ã¤ã„ã¦è°è«–ã•れ [14]_ ã€ä½•人ã‹ã¯%å½¢å¼ã‹ã‚‰{}å½¢å¼ã«è‡ªå‹•çš„ã«å¤‰ æ›ã™ã‚‹æ©Ÿèƒ½ã‚’開発ã—ã¦ã„ã¾ã™ [15]_ [16]_ 。ã“ã®å¤‰æ›æ©Ÿèƒ½ãŒæ¨™æº–ライブラリã«ä»˜ ã‘åŠ ãˆã‚‰ã‚ŒãŸæ™‚ã«ã¯ã€argparseã‚‚ã“ã®ä¸¡æ–¹ã®ãƒ•ォーマット形å¼ã‚’サãƒãƒ¼ãƒˆã—ã¾ã™ã€‚ .. Rejected: getopt compatibility methods ====================================== getopt互æ›ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯å–り除ã‹ã‚Œã¾ã—㟠----------------------------------------- .. Previously, when this PEP was suggesting the deprecation of getopt as well as optparse, there was some talk of adding a method like:: 以å‰ã€ã“ã®PEPãŒoptparseã¨åŒã˜ãgetoptã®å»ƒæ¢ã‚‚ææ¡ˆã—ãŸã¨ãã€ä»¥ä¸‹ã®ã‚ˆã†ãª メソッドã®è¿½åŠ ã«é–¢ã™ã‚‹è°è«–ãŒã‚りã¾ã—ãŸã€‚ :: ArgumentParser.add_getopt_arguments(options[, long_options]) .. However, this method will not be added for a number of reasons: ã—ã‹ã—ã€ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ã“れらã®ç†ç”±ã«ã‚ˆã‚Šè¿½åŠ ã•れã¾ã›ã‚“。 .. * The getopt module is not being deprecated, so there is less need. * getoptモジュールã¯å»ƒæ¢ã•れãªã„ãŸã‚ã€å¿…è¦æ€§ã¯ä½Žã„ã§ã™ã€‚ .. * This method would not actually ease the transition for any getopt users who were already maintaining usage messages, because the API above gives no way of adding help messages to the arguments. * ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ã™ã§ã«usageã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’管ç†ã—ã¦ã„ã‚‹getoptユーザãŒç§»è¡Œ ã«ä½¿ã†ã«ã¯ç°¡å˜ã§ã¯ã‚りã¾ã›ã‚“。ãªãœãªã‚‰ã“ã®APIã¯å¼•æ•°ã«ãƒ˜ãƒ«ãƒ—メッセージ ã‚’è¿½åŠ ã™ã‚‹æ©Ÿèƒ½ã‚’æä¾›ã—ã¦ã„ãªã„ã‹ã‚‰ã§ã™ã€‚ .. * Some users of getopt consider it very important that only a single function call is necessary. The API above does not satisfy this requirement because both ``ArgumentParser()`` and ``parse_args()`` must also be called. * getoptãŒæƒ³å®šã—ã¦ã„るユーザã®ã†ã¡ä½•人ã‹ã¯ã€é–¢æ•°ã‚’一回ã ã‘呼ã¹ã°ã„ã„ã¨ã„ ã†ã“ã¨ã‚’é‡è¦è¦–ã—ã¦ã„ã¾ã™ã€‚ã“ã®APIã§ã¯ ``ArgumentParser()`` 㨠``parse_args()`` ã®ä¸¡æ–¹ã‚’呼ã¶å¿…è¦ãŒã‚ã‚‹ãŸã‚ã€ã“ã®è¦æ±‚を満ãŸã›ã¾ã›ã‚“。 .. Out of Scope: Various Feature Requests ====================================== スコープ外: æ©Ÿèƒ½è¦æ±‚ -------------------------- .. Several feature requests for argparse were made in the discussion of this PEP: argparseã«å¯¾ã™ã‚‹æ©Ÿèƒ½è¦æ±‚ã®ã„ãã¤ã‹ã¯ã€ã“ã®PEPã§ã™ã§ã«è°è«–ã•れã¦ã„ã¾ã™ã€‚ .. * Support argument defaults from environment variables * Support argument defaults from configuration files * Support "foo --help subcommand" in addition to the currently supported "foo subcommand --help" * 環境変数ã‹ã‚‰ãƒ‡ãƒ•ォルトã®å¼•æ•°ã‚’è¨å®šã™ã‚‹æ©Ÿèƒ½ * è¨å®šãƒ•ァイルã‹ã‚‰ãƒ‡ãƒ•ォルトã®å¼•æ•°ã‚’è¨å®šã™ã‚‹æ©Ÿèƒ½ * ç¾åœ¨ã®"foo subcommand --help"ã«åŠ ãˆã¦"foo --help subcommand"を解釈ã™ã‚‹ 機能 .. These are all reasonable feature requests for the argparse module, but are out of the scope of this PEP, and have been redirected to the argparse issue tracker. ã“れらã¯å…¨ã¦æ£å½“ãªç†ç”±ãŒã‚ã‚‹æ©Ÿèƒ½è¦æ±‚ã§ã™ãŒã€ã“ã®PEPã®ã‚¹ã‚³ãƒ¼ãƒ—外ã§ã‚り〠argparseã®Issue Trackerã§è°è«–ã•れã¾ã™ã€‚ .. Discussion: sys.stderr and sys.exit =================================== è°è«–: sys.stderrã¨sys.exit ----------------------------------- .. There were some concerns that argparse by default always writes to ``sys.stderr`` and always calls ``sys.exit`` when invalid arguments are provided. This is the desired behavior for the vast majority of argparse use cases which revolve around simple command line interfaces. However, in some cases, it may be desirable to keep argparse from exiting, or to have it write its messages to something other than ``sys.stderr``. These use cases can be supported by subclassing ``ArgumentParser`` and overriding the ``exit`` or ``_print_message`` methods. The latter is an undocumented implementation detail, but could be officially exposed if this turns out to be a common need. argparseを使ã†ã¨ã€æ¨™æº–ã§ã¯å¸¸ã« ``sys.stderr`` ã«æ›¸ã出ã—ã€ä¸æ£ãªå¼•æ•°ãŒä¸Ž ãˆã‚‰ã‚ŒãŸæ™‚ã«å¸¸ã« ``sys.exit`` を呼んã§ã„ã¾ã™ã€‚ã“れã¯ç°¡å˜ãªã‚³ãƒžãƒ³ãƒ‰ãƒ©ã‚¤ãƒ³ インタフェースをæä¾›ã™ã‚‹argparseã®ãƒ¦ãƒ¼ã‚¹ã‚±ãƒ¼ã‚¹ã®å¤§éƒ¨åˆ†ã«ã¨ã£ã¦æœŸå¾…ã•れ㟠振る舞ã„ã§ã™ã€‚ã—ã‹ã—ã€ã„ãã¤ã‹ã®å ´åˆã§ã¯ã€argparseãŒexitã—ãªã„よã†ã«ã—㦠欲ã—ã‹ã£ãŸã‚Šã€ã‚ã‚‹ã„㯠``sys.stderr`` ä»¥å¤–ã«æ›¸ã出ã—ã¦æ¬²ã—ã‹ã£ãŸã‚Šã—ã¾ã™ã€‚ ã“ã®ã‚ˆã†ãªå ´åˆã€ ``ArgumentParser`` ã¨ã„ã†ã‚µãƒ–クラスを作りã€``exit`` ã‚„ ``_print_message`` をオーãƒãƒ¼ãƒ©ã‚¤ãƒ‰ã™ã‚‹ã“ã¨ã§æ¬²ã—ã„æ©Ÿèƒ½ã‚’実ç¾ã§ãã¾ã™ã€‚後 è€…ã¯æ–‡æ›¸åŒ–ã•れã¦ã„ãªã„実相ã®è©³ç´°ã§ã™ãŒã€ã‚ˆã使ã‚れるよã†ã«ãªã‚Œã°å…¬é–‹ã—ã¾ ã™ã€‚ å‚考文献 ---------- .. [1] argparse (http://code.google.com/p/argparse/) .. [2] getopt (http://docs.python.org/library/getopt.html) .. [3] optparse (http://docs.python.org/library/optparse.html) .. [4] argparse in IPython (http://mail.scipy.org/pipermail/ipython-dev/2009-April/005102.html) .. [5] argparse in Debian (http://packages.debian.org/search?keywords=argparse) .. [6] 2007-01-03 request for argparse in the standard library (http://mail.python.org/pipermail/python-list/2007-January/472276.html) .. [7] 2009-06-09 request for argparse in the standard library (http://bugs.python.org/issue6247) .. [8] 2009-09-10 request for argparse in the standard library (http://mail.python.org/pipermail/stdlib-sig/2009-September/000342.html) .. [9] Fredrik Lundh response to [6]_ (http://mail.python.org/pipermail/python-list/2007-January/1086892.html) .. [10] optparse variable args (http://docs.python.org/library/optparse.html#callback-example-6-variable-arguments) .. [11] parser.largs and parser.rargs (http://docs.python.org/library/optparse.html#how-callbacks-are-called) .. [12] take_action values argument (http://docs.python.org/library/optparse.html#adding-new-actions) .. [13] use {}-formatting instead of %-formatting (http://bugs.python.org/msg89279) .. [14] transitioning from % to {} formatting (http://mail.python.org/pipermail/pytho .. [15] Vinay Sajip's %-to-{} converter (http://gist.github.com/200936) .. [16] Benjamin Peterson's %-to-{} converter (http://bazaar.launchpad.net/~gutworth/+junk/mod2format/files) .. [17] Guido's approval (http://mail.python.org/pipermail/python-dev/2010-February/097839.html) .. Copyright ========== 著作権 ------- .. This document has been placed in the public domain. ã“ã®ãƒ‰ã‚ュメントã¯ãƒ‘ブリック・ドメインã«å±žã—ã¾ã™ã€‚