77import sys
88
99import inquirer .errors as errors
10+ from inquirer .render .console ._other import GLOBAL_OTHER_CHOICE
1011
1112
1213class TaggedValue :
@@ -32,7 +33,17 @@ def __ne__(self, other):
3233class Question :
3334 kind = "base question"
3435
35- def __init__ (self , name , message = "" , choices = None , default = None , ignore = False , validate = True , show_default = False ):
36+ def __init__ (
37+ self ,
38+ name ,
39+ message = "" ,
40+ choices = None ,
41+ default = None ,
42+ ignore = False ,
43+ validate = True ,
44+ show_default = False ,
45+ other = False ,
46+ ):
3647 self .name = name
3748 self ._message = message
3849 self ._choices = choices or []
@@ -41,6 +52,22 @@ def __init__(self, name, message="", choices=None, default=None, ignore=False, v
4152 self ._validate = validate
4253 self .answers = {}
4354 self .show_default = show_default
55+ self ._other = other
56+
57+ if self ._other :
58+ self ._choices .append (GLOBAL_OTHER_CHOICE )
59+
60+ def add_choice (self , choice ):
61+ try :
62+ index = self ._choices .index (choice )
63+ return index
64+ except ValueError :
65+ if self ._other :
66+ self ._choices .insert (- 1 , choice )
67+ return len (self ._choices ) - 2
68+
69+ self ._choices .append (choice )
70+ return len (self ._choices ) - 1
4471
4572 @property
4673 def ignore (self ):
@@ -112,18 +139,22 @@ def __init__(self, name, default=False, **kwargs):
112139class List (Question ):
113140 kind = "list"
114141
115- def __init__ (self , name , message = "" , choices = None , default = None , ignore = False , validate = True , carousel = False ):
142+ def __init__ (
143+ self , name , message = "" , choices = None , default = None , ignore = False , validate = True , carousel = False , other = False
144+ ):
116145
117- super ().__init__ (name , message , choices , default , ignore , validate )
146+ super ().__init__ (name , message , choices , default , ignore , validate , other = other )
118147 self .carousel = carousel
119148
120149
121150class Checkbox (Question ):
122151 kind = "checkbox"
123152
124- def __init__ (self , name , message = "" , choices = None , default = None , ignore = False , validate = True , carousel = False ):
153+ def __init__ (
154+ self , name , message = "" , choices = None , default = None , ignore = False , validate = True , carousel = False , other = False
155+ ):
125156
126- super ().__init__ (name , message , choices , default , ignore , validate )
157+ super ().__init__ (name , message , choices , default , ignore , validate , other = other )
127158 self .carousel = carousel
128159
129160
0 commit comments