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
eb9e885f
Kaydet (Commit)
eb9e885f
authored
Ock 12, 2013
tarafından
Chris Jerdonek
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Issue #16933 (merge from 3.3): Improve choices examples in argparse docs.
üst
1ba81ee1
71e39fb4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
23 deletions
+24
-23
argparse.rst
Doc/library/argparse.rst
+24
-23
No files found.
Doc/library/argparse.rst
Dosyayı görüntüle @
eb9e885f
...
...
@@ -1022,32 +1022,33 @@ choices
^^^^^^^
Some command-line arguments should be selected from a restricted set of values.
These can be handled by passing a container object as the
``choices``
keyword
These can be handled by passing a container object as the
*choices*
keyword
argument to :meth:`~ArgumentParser.add_argument`. When the command 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::
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('foo', choices='abc')
>>> parser.parse_args('c'.split())
Namespace(foo='c')
>>> parser.parse_args('X'.split())
usage: PROG [-h] {a,b,c}
PROG: error: argument foo: invalid choice: 'X' (choose from 'a', 'b', 'c')
Note that inclusion in the ``choices`` container is checked after any type_
conversions have been performed, so the type of the objects in the ``choices``
parsed, argument values will be checked, and an error message will be displayed
if the argument was not one of the acceptable values::
>>> parser = argparse.ArgumentParser(prog='game.py')
>>> parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
>>> parser.parse_args(['rock'])
Namespace(move='rock')
>>> parser.parse_args(['fire'])
usage: game.py [-h] {rock,paper,scissors}
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
'paper', 'scissors')
Note that inclusion in the *choices* container is checked after any type_
conversions have been performed, so the type of the objects in the *choices*
container should match the type_ specified::
>>> parser = argparse.ArgumentParser(prog='
PROG
')
>>> parser.add_argument('
foo', type=complex, choices=[1, 1j]
)
>>> p
arser.parse_args('1j'.split(
))
Namespace(
foo=1j
)
>>> parser.parse_args(
'-- -4'.split()
)
usage:
PROG [-h] {1,1j
}
PROG: error: argument foo: invalid choice: (-4+0j) (choose from 1, 1j
)
Any object that supports the ``in`` operator can be passed as the
``choices``
>>> parser = argparse.ArgumentParser(prog='
doors.py
')
>>> parser.add_argument('
door', type=int, choices=range(1, 4)
)
>>> p
rint(parser.parse_args(['3']
))
Namespace(
door=3
)
>>> parser.parse_args(
['4']
)
usage:
doors.py [-h] {1,2,3
}
doors.py: error: argument door: invalid choice: 4 (choose from 1, 2, 3
)
Any object that supports the ``in`` operator can be passed as the
*choices*
value, so :class:`dict` objects, :class:`set` objects, custom containers,
etc. are all supported.
...
...
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