Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
677e10a4
Kaydet (Commit)
677e10a4
authored
Ara 07, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add example for the entry for argparse
üst
9b955de7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
2 deletions
+60
-2
argparse.rst
Doc/library/argparse.rst
+1
-0
3.2.rst
Doc/whatsnew/3.2.rst
+59
-2
No files found.
Doc/library/argparse.rst
Dosyayı görüntüle @
677e10a4
...
...
@@ -1729,6 +1729,7 @@ Exiting methods
This method prints a usage message including the *message* to the
standard output and terminates the program with a status code of 2.
.. _upgrading-optparse-code:
Upgrading optparse code
-----------------------
...
...
Doc/whatsnew/3.2.rst
Dosyayı görüntüle @
677e10a4
...
...
@@ -77,7 +77,7 @@ PEP 389: Argparse Command Line Parsing Module
A new module for command line parsing, :mod:`argparse`, was introduced to
overcome the limitations of :mod:`optparse` which did not provide support for
positional arguments (not just option), subcommands, required options and other
positional arguments (not just option
s
), subcommands, required options and other
common patterns of specifying and validating options.
This module has already has wide-spread success in the community as a
...
...
@@ -86,13 +86,66 @@ third-party module. Being more fully featured than its predecessor,
older module is still being kept available because of the substantial amount of
legacy code that depends on it.
.. XXX add examples that highlight the new features
Here's an annotated example parser showing features like limiting results to a
set of choices, specifying a *metavar* in the help screen, validating that one
or more postional arguments is present, and making a required option::
import argparse
parser = argparse.ArgumentParser(
description = 'Manage servers', # main description for help
epilog = 'Tested on Solaris and Linux') # displayed after help
parser.add_argument('action', # argument name
choices = ['deploy', 'start', 'stop'], # one of four allowed values
help = 'action on each target') # help msg
parser.add_argument('targets',
metavar = 'HOSTNAME', # var name used in help msg
nargs = '+', # require 1 or more targets
help = 'url for target machines') # help msg explanation
parser.add_argument('-u', '--user', # -u or --user option
required = True, # make this a required argument
help = 'login as user')
Example of calling the parser on a command string::
>>> cmd = 'deploy sneezy.example.com sleepy.example.com -u skycaptain'
>>> result = parser.parse_args(cmd.split())
>>> # parsed variable are stored in the attributes
>>> result.action
'deploy'
>>> result.targets
['sneezy.example.com', 'sleepy.example.com']
>>> result.user
'skycaptain'
Example of the parser's automatically generated help::
>>> parser.parse_args('-h'.split())
usage: tmp_argparse_example.py [-h] -u USER
{deploy,start,stop} HOSTNAME [HOSTNAME ...]
Manage servers
positional arguments:
{deploy,start,stop} action on each target
HOSTNAME url for target machines
optional arguments:
-h, --help show this help message and exit
-u USER, --user USER login as user
Tested on Solaris and Linux
.. seealso::
:pep:`389` - New Command Line Parsing Module
PEP written by Steven Bethard.
:ref:`upgrading-optparse-code` for details on the differences from
:mod:`optparse`.
PEP 391: Dictionary Based Configuration for Logging
====================================================
...
...
@@ -992,6 +1045,10 @@ The docs now contain more examples and recipes. In particular, :mod:`re` module
has an extensive section, :ref:`re-examples`. Likewise, the :mod:`itertools`
module continues to be updated with new :ref:`itertools-recipes`.
The :mod:`datetime` module now has an auxiliary implementation in pure Python.
No functionality was changed. This just provides an easier-to-read
alternate implementation. (Contributed by Alexander Belopolsky.)
IDLE
====
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment