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
60b2e38b
Kaydet (Commit)
60b2e38b
authored
Ara 15, 2008
tarafından
Georg Brandl
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
#4568: remove limitation in varargs callback example.
üst
2d2fe572
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
25 deletions
+22
-25
optparse.rst
Doc/library/optparse.rst
+22
-25
No files found.
Doc/library/optparse.rst
Dosyayı görüntüle @
60b2e38b
...
@@ -1630,36 +1630,33 @@ directly).
...
@@ -1630,36 +1630,33 @@ directly).
Nevertheless, here's a stab at a callback for an option with variable
Nevertheless, here's a stab at a callback for an option with variable
arguments::
arguments::
def vararg_callback(option, opt_str, value, parser):
def vararg_callback(option, opt_str, value, parser):
assert value is None
assert value is None
done = 0
value = []
value = []
rargs = parser.rargs
def floatable(str):
while rargs:
try:
arg = rargs[0]
float(str)
return True
# Stop if we hit an arg like "--foo", "-a", "-fx", "--file=f",
except ValueError:
# etc. Note that this also stops on "-3" or "-3.0", so if
return False
# your option takes numeric values, you will need to handle
# this.
for arg in parser.rargs:
if ((arg[:2] == "--" and len(arg) > 2) or
# stop on --foo like options
(arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")):
if arg[:2] == "--" and len(arg) > 2:
break
break
else:
# stop on -a, but not on -3 or -3.0
value.append(arg)
if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
del rargs[0]
break
value.append(arg)
setattr(parser.values, option.dest, value)
del parser.rargs[:len(value)]
setattr(parser.values, option.dest, value))
[...]
[...]
parser.add_option("-c", "--callback", dest="vararg_attr",
parser.add_option("-c", "--callback", dest="vararg_attr",
action="callback", callback=vararg_callback)
action="callback", callback=vararg_callback)
The main weakness with this particular implementation is that negative numbers
in the arguments following ``"-c"`` will be interpreted as further options
(probably causing an error), rather than as arguments to ``"-c"``. Fixing this
is left as an exercise for the reader.
.. _optparse-extending-optparse:
.. _optparse-extending-optparse:
...
...
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