1414
1515.. autoclass:: EnumName
1616
17+ .. autoclass:: AppendEnumValue
18+
19+ .. autoclass:: AppendEnumName
20+
1721
1822Date and Time types
1923~~~~~~~~~~~~~~~~~~~
4650 "EnumValue" ,
4751 "EnumName" ,
4852 "EnumNameList" ,
53+ "AppendEnumValue" ,
54+ "AppendEnumName" ,
4955 "DateAction" ,
5056 "TimeAction" ,
5157 "DateTimeAction" ,
@@ -59,15 +65,15 @@ class KeyValueAction(Action):
5965
6066 Example of use::
6167
68+ @app.command
69+ def my_command(options: Mapping[str, str]):
70+ print(options)
71+
6272 @app.command
6373 @argument("--option", action=KeyValueAction)
6474 def my_command(args: Namespace):
6575 print(args.option)
6676
67- @app.command
68- def my_command(options: Mapping[str, str]):
69- print(options)
70-
7177 From CLI::
7278
7379 > my_app m_command --option a=foo --option b=bar
@@ -159,7 +165,7 @@ def to_enum(self, value):
159165class EnumValue (_EnumAction ):
160166 """
161167 Action to use an Enum as the type of an argument. In this mode the Enum is
162- reference by value.
168+ referenced by value.
163169
164170 The choices are automatically generated for help.
165171
@@ -194,7 +200,7 @@ def to_enum(self, value):
194200class EnumName (_EnumAction ):
195201 """
196202 Action to use an Enum as the type of an argument. In this mode the Enum is
197- reference by name.
203+ referenced by name.
198204
199205 The choices are automatically generated for help.
200206
@@ -226,10 +232,53 @@ def to_enum(self, value):
226232 return self ._enum [value ]
227233
228234
229- class EnumNameList ( EnumName ):
235+ def _copy_items ( items ):
230236 """
231- Action to use an Enum as the type of an argument. In this mode the Enum is
232- reference by name and appended to a list.
237+ Extracted from argparse
238+ """
239+ if items is None :
240+ return []
241+
242+ # The copy module is used only in the 'append' and 'append_const'
243+ # actions, and it is needed only when the default value isn't a list.
244+ # Delay its import for speeding up the common case.
245+ if isinstance (items , list ):
246+ return items [:]
247+
248+ import copy # pylint: disable=import-outside-toplevel
249+
250+ return copy .copy (items )
251+
252+
253+ class _AppendEnumActionMixin (_EnumAction ):
254+ """
255+ Mixin to support appending enum items
256+ """
257+
258+ def __call__ (self , parser , namespace , values , option_string = None ):
259+ items = getattr (namespace , self .dest , None )
260+ items = _copy_items (items )
261+ enum = self .to_enum (values )
262+ items .append (enum )
263+ setattr (namespace , self .dest , items )
264+
265+ def get_choices (self , choices : Union [Enum , Sequence [Enum ]]):
266+ """
267+ Get choices from the enum
268+ """
269+ raise NotImplementedError # pragma: no cover
270+
271+ def to_enum (self , value ):
272+ """
273+ Get enum from the supplied value.
274+ """
275+ raise NotImplementedError # pragma: no cover
276+
277+
278+ class AppendEnumValue (EnumValue , _AppendEnumActionMixin ):
279+ """
280+ Action to use an Enum as the type of an argument and to accept multiple
281+ enum values. In this mode the Enum is referenced by value.
233282
234283 The choices are automatically generated for help.
235284
@@ -241,28 +290,56 @@ class Colour(Enum):
241290 Blue = "blue"
242291
243292 @app.command
244- @argument("--colour ", type=Colour, action=EnumNameList )
293+ @argument("--colours ", type=Colour, action=AppendEnumValue )
245294 def my_command(args: Namespace):
246295 print(args.colour)
247296
297+ # Or using typing definition
298+
248299 @app.command
249- def my_command(*, colour: Sequence[Colour]):
300+ def my_command(*, colours: Sequence[Colour]):
301+ print(colours)
302+
303+ From CLI::
304+
305+ > my_app m_command --colour red --colour blue
306+ [Colour.Red, Colour.Blue]
307+
308+ .. versionadded:: 4.9
309+
310+ """
311+
312+
313+ class AppendEnumName (EnumName , _AppendEnumActionMixin ):
314+ """
315+ Action to use an Enum as the type of an argument and to accept multiple
316+ enum values. In this mode the Enum is referenced by name.
317+
318+ The choices are automatically generated for help.
319+
320+ Example of use::
321+
322+ class Colour(Enum):
323+ Red = "red"
324+ Green = "green"
325+ Blue = "blue"
326+
327+ @app.command
328+ @argument("--colours", type=Colour, action=AppendEnumName)
329+ def my_command(args: Namespace):
250330 print(args.colour)
251331
252332 From CLI::
253333
254- > my_app m_command --colour Red --colour Green
255- [Colour.Red, Colour.Green ]
334+ > my_app m_command --colour Red --colour Blue
335+ [Colour.Red, Colour.Blue ]
256336
257- .. versionadded:: 4.8.2
337+ .. versionadded:: 4.9
258338
259339 """
260340
261- def __call__ (self , parser , namespace , values , option_string = None ):
262- enum = self .to_enum (values )
263- items = getattr (namespace , self .dest , None ) or []
264- items .append (enum )
265- setattr (namespace , self .dest , items )
341+
342+ EnumNameList = AppendEnumName
266343
267344
268345class _DateTimeAction (Action ):
0 commit comments