1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
|
# coding=utf-8
"""
This module adds capabilities to argparse by patching a few of its functions.
It also defines a parser class called Cmd2ArgumentParser which improves error
and help output over normal argparse. All cmd2 code uses this parser and it is
recommended that developers of cmd2-based apps either use it or write their own
parser that inherits from it. This will give a consistent look-and-feel between
the help/error output of built-in cmd2 commands and the app-specific commands.
If you wish to override the parser used by cmd2's built-in commands, see
override_parser.py example.
Since the new capabilities are added by patching at the argparse API level,
they are available whether or not Cmd2ArgumentParser is used. However, the help
and error output of Cmd2ArgumentParser is customized to notate nargs ranges
whereas any other parser class won't be as explicit in their output.
**Added capabilities**
Extends argparse nargs functionality by allowing tuples which specify a range
(min, max). To specify a max value with no upper bound, use a 1-item tuple
(min,)
Example::
# -f argument expects at least 3 values
parser.add_argument('-f', nargs=(3,))
# -f argument expects 3 to 5 values
parser.add_argument('-f', nargs=(3, 5))
**Tab Completion**
cmd2 uses its ArgparseCompleter class to enable argparse-based tab completion
on all commands that use the @with_argparse wrappers. Out of the box you get
tab completion of commands, subcommands, and flag names, as well as instructive
hints about the current argument that print when tab is pressed. In addition,
you can add tab completion for each argument's values using parameters passed
to add_argument().
Below are the 3 add_argument() parameters for enabling tab completion of an
argument's value. Only one can be used at a time.
``choices`` - pass a list of values to the choices parameter.
Example::
my_list = ['An Option', 'SomeOtherOption']
parser.add_argument('-o', '--options', choices=my_list)
``choices_provider`` - pass a function that returns choices. This is good in
cases where the choice list is dynamically generated when the user hits tab.
Example::
def my_choices_provider(self):
...
return my_generated_list
parser.add_argument("arg", choices_provider=my_choices_provider)
``completer`` - pass a tab completion function that does custom completion.
cmd2 provides a few completer methods for convenience (e.g., path_complete,
delimiter_complete)
Example::
# This adds file-path completion to an argument
parser.add_argument('-o', '--options', completer=cmd2.Cmd.path_complete)
You can use functools.partial() to prepopulate values of the underlying
choices and completer functions/methods.
Example::
# This says to call path_complete with a preset value for its path_filter argument
dir_completer = functools.partial(path_complete,
path_filter=lambda path: os.path.isdir(path))
parser.add_argument('-o', '--options', completer=dir_completer)
For ``choices_provider`` and ``completer``, do not set them to a bound method. This
is because ArgparseCompleter passes the `self` argument explicitly to these
functions. When ArgparseCompleter calls one, it will detect whether it is bound
to a `Cmd` subclass or `CommandSet`. If bound to a `cmd2.Cmd subclass`, it will
pass the app instance as the `self` argument. If bound to a `cmd2.CommandSet`
subclass, it will pass the `CommandSet` instance as the `self` argument.
Therefore instead of passing something like `self.path_complete`, pass
`cmd2.Cmd.path_complete`.
``choices_provider`` and ``completer`` functions can also be implemented as
standalone functions (i.e. not a member of a class). In this case,
ArgparseCompleter will pass its ``cmd2.Cmd`` app instance as the first
positional argument.
Of the 3 tab completion parameters, ``choices`` is the only one where argparse
validates user input against items in the choices list. This is because the
other 2 parameters are meant to tab complete data sets that are viewed as
dynamic. Therefore it is up to the developer to validate if the user has typed
an acceptable value for these arguments.
There are times when what's being tab completed is determined by a previous
argument on the command line. In theses cases, ArgparseCompleter can pass a
dictionary that maps the command line tokens up through the one being completed
to their argparse argument name. To receive this dictionary, your
choices/completer function should have an argument called arg_tokens.
Example::
def my_choices_provider(self, arg_tokens)
def my_completer(self, text, line, begidx, endidx, arg_tokens)
All values of the arg_tokens dictionary are lists, even if a particular
argument expects only 1 token. Since ArgparseCompleter is for tab completion,
it does not convert the tokens to their actual argument types or validate their
values. All tokens are stored in the dictionary as the raw strings provided on
the command line. It is up to the developer to determine if the user entered
the correct argument type (e.g. int) and validate their values.
CompletionItem Class - This class was added to help in cases where
uninformative data is being tab completed. For instance, tab completing ID
numbers isn't very helpful to a user without context. Returning a list of
CompletionItems instead of a regular string for completion results will signal
the ArgparseCompleter to output the completion results in a table of completion
tokens with descriptions instead of just a table of tokens::
Instead of this:
1 2 3
The user sees this:
ITEM_ID Item Name
1 My item
2 Another item
3 Yet another item
The left-most column is the actual value being tab completed and its header is
that value's name. The right column header is defined using the
descriptive_header parameter of add_argument(). The right column values come
from the CompletionItem.description value.
Example::
token = 1
token_description = "My Item"
completion_item = CompletionItem(token, token_description)
Since descriptive_header and CompletionItem.description are just strings, you
can format them in such a way to have multiple columns::
ITEM_ID Item Name Checked Out Due Date
1 My item True 02/02/2022
2 Another item False
3 Yet another item False
To use CompletionItems, just return them from your choices or completer
functions.
To avoid printing a ton of information to the screen at once when a user
presses tab, there is a maximum threshold for the number of CompletionItems
that will be shown. Its value is defined in cmd2.Cmd.max_completion_items. It
defaults to 50, but can be changed. If the number of completion suggestions
exceeds this number, they will be displayed in the typical columnized format
and will not include the description value of the CompletionItems.
**Patched argparse functions**
``argparse._ActionsContainer.add_argument`` - adds arguments related to tab
completion and enables nargs range parsing. See _add_argument_wrapper for
more details on these arguments.
``argparse.ArgumentParser._get_nargs_pattern`` - adds support for nargs ranges.
See _get_nargs_pattern_wrapper for more details.
``argparse.ArgumentParser._match_argument`` - adds support for nargs ranges.
See _match_argument_wrapper for more details.
``argparse._SubParsersAction.remove_parser`` - new function which removes a
sub-parser from a sub-parsers group. See _SubParsersAction_remove_parser for
more details.
**Added accessor methods**
cmd2 has patched ``argparse.Action`` to include the following accessor methods
for cases in which you need to manually access the cmd2-specific attributes.
- ``argparse.Action.get_choices_callable()`` - See
:func:`_action_get_choices_callable` for more details.
- ``argparse.Action.set_choices_provider()`` - See
:func:`_action_set_choices_provider` for more details.
- ``argparse.Action.set_completer()`` - See
:func:`_action_set_completer` for more details.
- ``argparse.Action.get_descriptive_header()`` - See
:func:`_action_get_descriptive_header` for more details.
- ``argparse.Action.set_descriptive_header()`` - See
:func:`_action_set_descriptive_header` for more details.
- ``argparse.Action.get_nargs_range()`` - See
:func:`_action_get_nargs_range` for more details.
- ``argparse.Action.set_nargs_range()`` - See
:func:`_action_set_nargs_range` for more details.
- ``argparse.Action.get_suppress_tab_hint()`` - See
:func:`_action_get_suppress_tab_hint` for more details.
- ``argparse.Action.set_suppress_tab_hint()`` - See
:func:`_action_set_suppress_tab_hint` for more details.
**Subcommand removal**
cmd2 has patched ``argparse._SubParsersAction`` to include a ``remove_parser()``
method which can be used to remove a subcommand.
``argparse._SubParsersAction.remove_parser`` - new function which removes a
sub-parser from a sub-parsers group. See
:func:`_SubParsersAction_remove_parser` for more details.
"""
import argparse
import re
import sys
# noinspection PyUnresolvedReferences,PyProtectedMember
from argparse import (
ONE_OR_MORE,
ZERO_OR_MORE,
ArgumentError,
)
from gettext import (
gettext,
)
from typing import (
IO,
Any,
Callable,
Dict,
Iterable,
List,
NoReturn,
Optional,
Sequence,
Set,
Tuple,
Type,
Union,
cast,
)
from . import (
ansi,
constants,
)
try:
from typing import (
Protocol,
runtime_checkable,
)
except ImportError:
from typing_extensions import ( # type: ignore[misc]
Protocol,
runtime_checkable,
)
def generate_range_error(range_min: int, range_max: Union[int, float]) -> str:
"""Generate an error message when the the number of arguments provided is not within the expected range"""
err_str = "expected "
if range_max == constants.INFINITY:
plural = '' if range_min == 1 else 's'
err_str += f"at least {range_min}"
else:
plural = '' if range_max == 1 else 's'
if range_min == range_max:
err_str += f"{range_min}"
else:
err_str += f"{range_min} to {range_max}"
err_str += f" argument{plural}"
return err_str
class CompletionItem(str):
"""
Completion item with descriptive text attached
See header of this file for more information
"""
def __new__(cls, value: object, *args: Any, **kwargs: Any) -> 'CompletionItem':
return super(CompletionItem, cls).__new__(cls, value)
# noinspection PyUnusedLocal
def __init__(self, value: object, desc: str = '', *args: Any) -> None:
"""
CompletionItem Initializer
:param value: the value being tab completed
:param desc: description text to display
:param args: args for str __init__
:param kwargs: kwargs for str __init__
"""
super().__init__(*args)
self.description = desc
############################################################################################################
# Class and functions related to ChoicesCallable
############################################################################################################
@runtime_checkable
class ChoicesProviderFuncBase(Protocol):
"""
Function that returns a list of choices in support of tab completion
"""
def __call__(self) -> List[str]:
... # pragma: no cover
@runtime_checkable
class ChoicesProviderFuncWithTokens(Protocol):
"""
Function that returns a list of choices in support of tab completion and accepts a dictionary of prior arguments.
"""
def __call__(self, *, arg_tokens: Dict[str, List[str]] = {}) -> List[str]:
... # pragma: no cover
ChoicesProviderFunc = Union[ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens]
@runtime_checkable
class CompleterFuncBase(Protocol):
"""
Function to support tab completion with the provided state of the user prompt
"""
def __call__(
self,
text: str,
line: str,
begidx: int,
endidx: int,
) -> List[str]:
... # pragma: no cover
@runtime_checkable
class CompleterFuncWithTokens(Protocol):
"""
Function to support tab completion with the provided state of the user prompt and accepts a dictionary of prior
arguments.
"""
def __call__(
self,
text: str,
line: str,
begidx: int,
endidx: int,
*,
arg_tokens: Dict[str, List[str]] = {},
) -> List[str]:
... # pragma: no cover
CompleterFunc = Union[CompleterFuncBase, CompleterFuncWithTokens]
class ChoicesCallable:
"""
Enables using a callable as the choices provider for an argparse argument.
While argparse has the built-in choices attribute, it is limited to an iterable.
"""
def __init__(
self,
is_completer: bool,
to_call: Union[CompleterFunc, ChoicesProviderFunc],
) -> None:
"""
Initializer
:param is_completer: True if to_call is a tab completion routine which expects
the args: text, line, begidx, endidx
:param to_call: the callable object that will be called to provide choices for the argument
"""
self.is_completer = is_completer
if is_completer:
if not isinstance(to_call, (CompleterFuncBase, CompleterFuncWithTokens)): # pragma: no cover
# runtime checking of Protocols do not currently check the parameters of a function.
raise ValueError(
'With is_completer set to true, to_call must be either CompleterFunc, CompleterFuncWithTokens'
)
else:
if not isinstance(to_call, (ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens)): # pragma: no cover
# runtime checking of Protocols do not currently check the parameters of a function.
raise ValueError(
'With is_completer set to false, to_call must be either: '
'ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens'
)
self.to_call = to_call
@property
def completer(self) -> CompleterFunc:
if not isinstance(self.to_call, (CompleterFuncBase, CompleterFuncWithTokens)): # pragma: no cover
# this should've been caught in the constructor, just a backup check
raise ValueError('Function is not a CompleterFunc')
return self.to_call
@property
def choices_provider(self) -> ChoicesProviderFunc:
if not isinstance(self.to_call, (ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens)): # pragma: no cover
# this should've been caught in the constructor, just a backup check
raise ValueError('Function is not a ChoicesProviderFunc')
return self.to_call
############################################################################################################
# The following are names of custom argparse Action attributes added by cmd2
############################################################################################################
# ChoicesCallable object that specifies the function to be called which provides choices to the argument
ATTR_CHOICES_CALLABLE = 'choices_callable'
# Descriptive header that prints when using CompletionItems
ATTR_DESCRIPTIVE_HEADER = 'descriptive_header'
# A tuple specifying nargs as a range (min, max)
ATTR_NARGS_RANGE = 'nargs_range'
# Pressing tab normally displays the help text for the argument if no choices are available
# Setting this attribute to True will suppress these hints
ATTR_SUPPRESS_TAB_HINT = 'suppress_tab_hint'
############################################################################################################
# Patch argparse.Action with accessors for choice_callable attribute
############################################################################################################
def _action_get_choices_callable(self: argparse.Action) -> Optional[ChoicesCallable]:
"""
Get the choices_callable attribute of an argparse Action.
This function is added by cmd2 as a method called ``get_choices_callable()`` to ``argparse.Action`` class.
To call: ``action.get_choices_callable()``
:param self: argparse Action being queried
:return: A ChoicesCallable instance or None if attribute does not exist
"""
return cast(Optional[ChoicesCallable], getattr(self, ATTR_CHOICES_CALLABLE, None))
setattr(argparse.Action, 'get_choices_callable', _action_get_choices_callable)
def _action_set_choices_callable(self: argparse.Action, choices_callable: ChoicesCallable) -> None:
"""
Set the choices_callable attribute of an argparse Action.
This function is added by cmd2 as a method called ``_set_choices_callable()`` to ``argparse.Action`` class.
Call this using the convenience wrappers ``set_choices_provider()`` and ``set_completer()`` instead.
:param self: action being edited
:param choices_callable: the ChoicesCallable instance to use
:raises: TypeError if used on incompatible action type
"""
# Verify consistent use of parameters
if self.choices is not None:
err_msg = "None of the following parameters can be used alongside a choices parameter:\n" "choices_provider, completer"
raise (TypeError(err_msg))
elif self.nargs == 0:
err_msg = (
"None of the following parameters can be used on an action that takes no arguments:\n"
"choices_provider, completer"
)
raise (TypeError(err_msg))
setattr(self, ATTR_CHOICES_CALLABLE, choices_callable)
setattr(argparse.Action, '_set_choices_callable', _action_set_choices_callable)
def _action_set_choices_provider(
self: argparse.Action,
choices_provider: ChoicesProviderFunc,
) -> None:
"""
Set choices_provider of an argparse Action.
This function is added by cmd2 as a method called ``set_choices_callable()`` to ``argparse.Action`` class.
To call: ``action.set_choices_provider(choices_provider)``
:param self: action being edited
:param choices_provider: the choices_provider instance to use
:raises: TypeError if used on incompatible action type
"""
self._set_choices_callable(ChoicesCallable(is_completer=False, to_call=choices_provider)) # type: ignore[attr-defined]
setattr(argparse.Action, 'set_choices_provider', _action_set_choices_provider)
def _action_set_completer(
self: argparse.Action,
completer: CompleterFunc,
) -> None:
"""
Set completer of an argparse Action.
This function is added by cmd2 as a method called ``set_completer()`` to ``argparse.Action`` class.
To call: ``action.set_completer(completer)``
:param self: action being edited
:param completer: the completer instance to use
:raises: TypeError if used on incompatible action type
"""
self._set_choices_callable(ChoicesCallable(is_completer=True, to_call=completer)) # type: ignore[attr-defined]
setattr(argparse.Action, 'set_completer', _action_set_completer)
############################################################################################################
# Patch argparse.Action with accessors for descriptive_header attribute
############################################################################################################
def _action_get_descriptive_header(self: argparse.Action) -> Optional[str]:
"""
Get the descriptive_header attribute of an argparse Action.
This function is added by cmd2 as a method called ``get_descriptive_header()`` to ``argparse.Action`` class.
To call: ``action.get_descriptive_header()``
:param self: argparse Action being queried
:return: The value of descriptive_header or None if attribute does not exist
"""
return cast(Optional[str], getattr(self, ATTR_DESCRIPTIVE_HEADER, None))
setattr(argparse.Action, 'get_descriptive_header', _action_get_descriptive_header)
def _action_set_descriptive_header(self: argparse.Action, descriptive_header: Optional[str]) -> None:
"""
Set the descriptive_header attribute of an argparse Action.
This function is added by cmd2 as a method called ``set_descriptive_header()`` to ``argparse.Action`` class.
To call: ``action.set_descriptive_header(descriptive_header)``
:param self: argparse Action being updated
:param descriptive_header: value being assigned
"""
setattr(self, ATTR_DESCRIPTIVE_HEADER, descriptive_header)
setattr(argparse.Action, 'set_descriptive_header', _action_set_descriptive_header)
############################################################################################################
# Patch argparse.Action with accessors for nargs_range attribute
############################################################################################################
def _action_get_nargs_range(self: argparse.Action) -> Optional[Tuple[int, Union[int, float]]]:
"""
Get the nargs_range attribute of an argparse Action.
This function is added by cmd2 as a method called ``get_nargs_range()`` to ``argparse.Action`` class.
To call: ``action.get_nargs_range()``
:param self: argparse Action being queried
:return: The value of nargs_range or None if attribute does not exist
"""
return cast(Optional[Tuple[int, Union[int, float]]], getattr(self, ATTR_NARGS_RANGE, None))
setattr(argparse.Action, 'get_nargs_range', _action_get_nargs_range)
def _action_set_nargs_range(self: argparse.Action, nargs_range: Optional[Tuple[int, Union[int, float]]]) -> None:
"""
Set the nargs_range attribute of an argparse Action.
This function is added by cmd2 as a method called ``set_nargs_range()`` to ``argparse.Action`` class.
To call: ``action.set_nargs_range(nargs_range)``
:param self: argparse Action being updated
:param nargs_range: value being assigned
"""
setattr(self, ATTR_NARGS_RANGE, nargs_range)
setattr(argparse.Action, 'set_nargs_range', _action_set_nargs_range)
############################################################################################################
# Patch argparse.Action with accessors for suppress_tab_hint attribute
############################################################################################################
def _action_get_suppress_tab_hint(self: argparse.Action) -> bool:
"""
Get the suppress_tab_hint attribute of an argparse Action.
This function is added by cmd2 as a method called ``get_suppress_tab_hint()`` to ``argparse.Action`` class.
To call: ``action.get_suppress_tab_hint()``
:param self: argparse Action being queried
:return: The value of suppress_tab_hint or False if attribute does not exist
"""
return cast(bool, getattr(self, ATTR_SUPPRESS_TAB_HINT, False))
setattr(argparse.Action, 'get_suppress_tab_hint', _action_get_suppress_tab_hint)
def _action_set_suppress_tab_hint(self: argparse.Action, suppress_tab_hint: bool) -> None:
"""
Set the suppress_tab_hint attribute of an argparse Action.
This function is added by cmd2 as a method called ``set_suppress_tab_hint()`` to ``argparse.Action`` class.
To call: ``action.set_suppress_tab_hint(suppress_tab_hint)``
:param self: argparse Action being updated
:param suppress_tab_hint: value being assigned
"""
setattr(self, ATTR_SUPPRESS_TAB_HINT, suppress_tab_hint)
setattr(argparse.Action, 'set_suppress_tab_hint', _action_set_suppress_tab_hint)
############################################################################################################
# Allow developers to add custom action attributes
############################################################################################################
CUSTOM_ACTION_ATTRIBS: Set[str] = set()
_CUSTOM_ATTRIB_PFX = '_attr_'
def register_argparse_argument_parameter(param_name: str, param_type: Optional[Type[Any]]) -> None:
"""
Registers a custom argparse argument parameter.
The registered name will then be a recognized keyword parameter to the parser's `add_argument()` function.
An accessor functions will be added to the parameter's Action object in the form of: ``get_{param_name}()``
and ``set_{param_name}(value)``.
:param param_name: Name of the parameter to add.
"""
attr_name = f'{_CUSTOM_ATTRIB_PFX}{param_name}'
if param_name in CUSTOM_ACTION_ATTRIBS or hasattr(argparse.Action, attr_name):
raise KeyError(f'Custom parameter {param_name} already exists')
if not re.search('^[A-Za-z_][A-Za-z0-9_]*$', param_name):
raise KeyError(f'Invalid parameter name {param_name} - cannot be used as a python identifier')
getter_name = f'get_{param_name}'
def _action_get_custom_parameter(self: argparse.Action) -> Any:
f"""
Get the custom {param_name} attribute of an argparse Action.
This function is added by cmd2 as a method called ``{getter_name}()`` to ``argparse.Action`` class.
To call: ``action.{getter_name}()``
:param self: argparse Action being queried
:return: The value of {param_name} or None if attribute does not exist
"""
return getattr(self, attr_name, None)
setattr(argparse.Action, getter_name, _action_get_custom_parameter)
setter_name = f'set_{param_name}'
def _action_set_custom_parameter(self: argparse.Action, value: Any) -> None:
f"""
Set the custom {param_name} attribute of an argparse Action.
This function is added by cmd2 as a method called ``{setter_name}()`` to ``argparse.Action`` class.
To call: ``action.{setter_name}({param_name})``
:param self: argparse Action being updated
:param value: value being assigned
"""
if param_type and not isinstance(value, param_type):
raise TypeError(f'{param_name} must be of type {param_type}, got: {value} ({type(value)})')
setattr(self, attr_name, value)
setattr(argparse.Action, setter_name, _action_set_custom_parameter)
CUSTOM_ACTION_ATTRIBS.add(param_name)
############################################################################################################
# Patch _ActionsContainer.add_argument with our wrapper to support more arguments
############################################################################################################
# Save original _ActionsContainer.add_argument so we can call it in our wrapper
# noinspection PyProtectedMember
orig_actions_container_add_argument = argparse._ActionsContainer.add_argument
def _add_argument_wrapper(
self: argparse._ActionsContainer,
*args: Any,
nargs: Union[int, str, Tuple[int], Tuple[int, int], Tuple[int, float], None] = None,
choices_provider: Optional[ChoicesProviderFunc] = None,
completer: Optional[CompleterFunc] = None,
suppress_tab_hint: bool = False,
descriptive_header: Optional[str] = None,
**kwargs: Any,
) -> argparse.Action:
"""
Wrapper around _ActionsContainer.add_argument() which supports more settings used by cmd2
# Args from original function
:param self: instance of the _ActionsContainer being added to
:param args: arguments expected by argparse._ActionsContainer.add_argument
# Customized arguments from original function
:param nargs: extends argparse nargs functionality by allowing tuples which specify a range (min, max)
to specify a max value with no upper bound, use a 1-item tuple (min,)
# Added args used by ArgparseCompleter
:param choices_provider: function that provides choices for this argument
:param completer: tab completion function that provides choices for this argument
:param suppress_tab_hint: when ArgparseCompleter has no results to show during tab completion, it displays the
current argument's help text as a hint. Set this to True to suppress the hint. If this
argument's help text is set to argparse.SUPPRESS, then tab hints will not display
regardless of the value passed for suppress_tab_hint. Defaults to False.
:param descriptive_header: if the provided choices are CompletionItems, then this header will display
during tab completion. Defaults to None.
# Args from original function
:param kwargs: keyword-arguments recognized by argparse._ActionsContainer.add_argument
Note: You can only use 1 of the following in your argument:
choices, choices_provider, completer
See the header of this file for more information
:return: the created argument action
:raises: ValueError on incorrect parameter usage
"""
# Verify consistent use of arguments
choices_callables = [choices_provider, completer]
num_params_set = len(choices_callables) - choices_callables.count(None)
if num_params_set > 1:
err_msg = "Only one of the following parameters may be used at a time:\n" "choices_provider, completer"
raise (ValueError(err_msg))
# Pre-process special ranged nargs
nargs_range = None
if nargs is not None:
nargs_adjusted: Union[int, str, Tuple[int], Tuple[int, int], Tuple[int, float], None]
# Check if nargs was given as a range
if isinstance(nargs, tuple):
# Handle 1-item tuple by setting max to INFINITY
if len(nargs) == 1:
nargs = (nargs[0], constants.INFINITY)
# Validate nargs tuple
if (
len(nargs) != 2
or not isinstance(nargs[0], int) # type: ignore[unreachable]
or not (isinstance(nargs[1], int) or nargs[1] == constants.INFINITY) # type: ignore[misc]
):
raise ValueError('Ranged values for nargs must be a tuple of 1 or 2 integers')
if nargs[0] >= nargs[1]: # type: ignore[misc]
raise ValueError('Invalid nargs range. The first value must be less than the second')
if nargs[0] < 0:
raise ValueError('Negative numbers are invalid for nargs range')
# Save the nargs tuple as our range setting
nargs_range = nargs
range_min = nargs_range[0]
range_max = nargs_range[1] # type: ignore[misc]
# Convert nargs into a format argparse recognizes
if range_min == 0:
if range_max == 1:
nargs_adjusted = argparse.OPTIONAL
# No range needed since (0, 1) is just argparse.OPTIONAL
nargs_range = None
else:
nargs_adjusted = argparse.ZERO_OR_MORE
if range_max == constants.INFINITY:
# No range needed since (0, INFINITY) is just argparse.ZERO_OR_MORE
nargs_range = None
elif range_min == 1 and range_max == constants.INFINITY:
nargs_adjusted = argparse.ONE_OR_MORE
# No range needed since (1, INFINITY) is just argparse.ONE_OR_MORE
nargs_range = None
else:
nargs_adjusted = argparse.ONE_OR_MORE
else:
nargs_adjusted = nargs
# Add the argparse-recognized version of nargs to kwargs
kwargs['nargs'] = nargs_adjusted
# Extract registered custom keyword arguments
custom_attribs: Dict[str, Any] = {}
for keyword, value in kwargs.items():
if keyword in CUSTOM_ACTION_ATTRIBS:
custom_attribs[keyword] = value
for keyword in custom_attribs:
del kwargs[keyword]
# Create the argument using the original add_argument function
new_arg = orig_actions_container_add_argument(self, *args, **kwargs)
# Set the custom attributes
new_arg.set_nargs_range(nargs_range) # type: ignore[arg-type, attr-defined]
if choices_provider:
new_arg.set_choices_provider(choices_provider) # type: ignore[attr-defined]
elif completer:
new_arg.set_completer(completer) # type: ignore[attr-defined]
new_arg.set_suppress_tab_hint(suppress_tab_hint) # type: ignore[attr-defined]
new_arg.set_descriptive_header(descriptive_header) # type: ignore[attr-defined]
for keyword, value in custom_attribs.items():
attr_setter = getattr(new_arg, f'set_{keyword}', None)
if attr_setter is not None:
attr_setter(value)
return new_arg
# Overwrite _ActionsContainer.add_argument with our wrapper
# noinspection PyProtectedMember
setattr(argparse._ActionsContainer, 'add_argument', _add_argument_wrapper)
############################################################################################################
# Patch ArgumentParser._get_nargs_pattern with our wrapper to nargs ranges
############################################################################################################
# Save original ArgumentParser._get_nargs_pattern so we can call it in our wrapper
# noinspection PyProtectedMember
orig_argument_parser_get_nargs_pattern = argparse.ArgumentParser._get_nargs_pattern
# noinspection PyProtectedMember
def _get_nargs_pattern_wrapper(self: argparse.ArgumentParser, action: argparse.Action) -> str:
# Wrapper around ArgumentParser._get_nargs_pattern behavior to support nargs ranges
nargs_range = action.get_nargs_range() # type: ignore[attr-defined]
if nargs_range is not None:
if nargs_range[1] == constants.INFINITY:
range_max = ''
else:
range_max = nargs_range[1] # type: ignore[assignment]
nargs_pattern = f'(-*A{{{nargs_range[0]},{range_max}}}-*)'
# if this is an optional action, -- is not allowed
if action.option_strings:
nargs_pattern = nargs_pattern.replace('-*', '')
nargs_pattern = nargs_pattern.replace('-', '')
return nargs_pattern
return orig_argument_parser_get_nargs_pattern(self, action)
# Overwrite ArgumentParser._get_nargs_pattern with our wrapper
# noinspection PyProtectedMember
setattr(argparse.ArgumentParser, '_get_nargs_pattern', _get_nargs_pattern_wrapper)
############################################################################################################
# Patch ArgumentParser._match_argument with our wrapper to nargs ranges
############################################################################################################
# noinspection PyProtectedMember
orig_argument_parser_match_argument = argparse.ArgumentParser._match_argument
# noinspection PyProtectedMember
def _match_argument_wrapper(self: argparse.ArgumentParser, action: argparse.Action, arg_strings_pattern: str) -> int:
# Wrapper around ArgumentParser._match_argument behavior to support nargs ranges
nargs_pattern = self._get_nargs_pattern(action)
match = re.match(nargs_pattern, arg_strings_pattern)
# raise an exception if we weren't able to find a match
if match is None:
nargs_range = action.get_nargs_range() # type: ignore[attr-defined]
if nargs_range is not None:
raise ArgumentError(action, generate_range_error(nargs_range[0], nargs_range[1]))
return orig_argument_parser_match_argument(self, action, arg_strings_pattern)
# Overwrite ArgumentParser._match_argument with our wrapper
# noinspection PyProtectedMember
setattr(argparse.ArgumentParser, '_match_argument', _match_argument_wrapper)
############################################################################################################
# Patch argparse._SubParsersAction to add remove_parser function
############################################################################################################
# noinspection PyPep8Naming
def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str) -> None:
"""
Removes a sub-parser from a sub-parsers group. Used to remove subcommands from a parser.
This function is added by cmd2 as a method called ``remove_parser()`` to ``argparse._SubParsersAction`` class.
To call: ``action.remove_parser(name)``
:param self: instance of the _SubParsersAction being edited
:param name: name of the subcommand for the sub-parser to remove
"""
# Remove this subcommand from its base command's help text
for choice_action in self._choices_actions:
if choice_action.dest == name:
self._choices_actions.remove(choice_action)
break
# Remove this subcommand and all its aliases from the base command
subparser = self._name_parser_map.get(name)
if subparser is not None:
to_remove = []
for cur_name, cur_parser in self._name_parser_map.items():
if cur_parser is subparser:
to_remove.append(cur_name)
for cur_name in to_remove:
del self._name_parser_map[cur_name]
# noinspection PyProtectedMember
setattr(argparse._SubParsersAction, 'remove_parser', _SubParsersAction_remove_parser)
############################################################################################################
# Unless otherwise noted, everything below this point are copied from Python's
# argparse implementation with minor tweaks to adjust output.
# Changes are noted if it's buried in a block of copied code. Otherwise the
# function will check for a special case and fall back to the parent function
############################################################################################################
# noinspection PyCompatibility,PyShadowingBuiltins
class Cmd2HelpFormatter(argparse.RawTextHelpFormatter):
"""Custom help formatter to configure ordering of help text"""
def _format_usage(
self,
usage: Optional[str],
actions: Iterable[argparse.Action],
groups: Iterable[argparse._ArgumentGroup],
prefix: Optional[str] = None,
) -> str:
if prefix is None:
prefix = gettext('Usage: ')
# if usage is specified, use that
if usage is not None:
usage %= dict(prog=self._prog)
# if no optionals or positionals are available, usage is just prog
elif not actions:
usage = '%(prog)s' % dict(prog=self._prog)
# if optionals and positionals are available, calculate usage
else:
prog = '%(prog)s' % dict(prog=self._prog)
# split optionals from positionals
optionals = []
positionals = []
# Begin cmd2 customization (separates required and optional, applies to all changes in this function)
required_options = []
for action in actions:
if action.option_strings:
if action.required:
required_options.append(action)
else:
optionals.append(action)
else:
positionals.append(action)
# End cmd2 customization
# build full usage string
format = self._format_actions_usage
action_usage = format(required_options + optionals + positionals, groups)
usage = ' '.join([s for s in [prog, action_usage] if s])
# wrap the usage parts if it's too long
text_width = self._width - self._current_indent
if len(prefix) + len(usage) > text_width:
# Begin cmd2 customization
# break usage into wrappable parts
part_regexp = r'\(.*?\)+|\[.*?\]+|\S+'
req_usage = format(required_options, groups)
opt_usage = format(optionals, groups)
pos_usage = format(positionals, groups)
req_parts = re.findall(part_regexp, req_usage)
opt_parts = re.findall(part_regexp, opt_usage)
pos_parts = re.findall(part_regexp, pos_usage)
assert ' '.join(req_parts) == req_usage
assert ' '.join(opt_parts) == opt_usage
assert ' '.join(pos_parts) == pos_usage
# End cmd2 customization
# helper for wrapping lines
# noinspection PyMissingOrEmptyDocstring,PyShadowingNames
def get_lines(parts: List[str], indent: str, prefix: Optional[str] = None) -> List[str]:
lines: List[str] = []
line: List[str] = []
if prefix is not None:
line_len = len(prefix) - 1
else:
line_len = len(indent) - 1
for part in parts:
if line_len + 1 + len(part) > text_width and line:
lines.append(indent + ' '.join(line))
line = []
line_len = len(indent) - 1
line.append(part)
line_len += len(part) + 1
if line:
lines.append(indent + ' '.join(line))
if prefix is not None:
lines[0] = lines[0][len(indent) :]
return lines
# if prog is short, follow it with optionals or positionals
if len(prefix) + len(prog) <= 0.75 * text_width:
indent = ' ' * (len(prefix) + len(prog) + 1)
# Begin cmd2 customization
if req_parts:
lines = get_lines([prog] + req_parts, indent, prefix)
lines.extend(get_lines(opt_parts, indent))
lines.extend(get_lines(pos_parts, indent))
elif opt_parts:
lines = get_lines([prog] + opt_parts, indent, prefix)
lines.extend(get_lines(pos_parts, indent))
elif pos_parts:
lines = get_lines([prog] + pos_parts, indent, prefix)
else:
lines = [prog]
# End cmd2 customization
# if prog is long, put it on its own line
else:
indent = ' ' * len(prefix)
# Begin cmd2 customization
parts = req_parts + opt_parts + pos_parts
lines = get_lines(parts, indent)
if len(lines) > 1:
lines = []
lines.extend(get_lines(req_parts, indent))
lines.extend(get_lines(opt_parts, indent))
lines.extend(get_lines(pos_parts, indent))
# End cmd2 customization
lines = [prog] + lines
# join lines into usage
usage = '\n'.join(lines)
# prefix with 'Usage:'
return '%s%s\n\n' % (prefix, usage)
def _format_action_invocation(self, action: argparse.Action) -> str:
if not action.option_strings:
default = self._get_default_metavar_for_positional(action)
(metavar,) = self._metavar_formatter(action, default)(1)
return metavar
else:
parts: List[str] = []
# if the Optional doesn't take a value, format is:
# -s, --long
if action.nargs == 0:
parts.extend(action.option_strings)
return ', '.join(parts)
# Begin cmd2 customization (less verbose)
# if the Optional takes a value, format is:
# -s, --long ARGS
else:
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
return ', '.join(action.option_strings) + ' ' + args_string
# End cmd2 customization
# noinspection PyMethodMayBeStatic
def _determine_metavar(
self,
action: argparse.Action,
default_metavar: Union[str, Tuple[str, ...]],
) -> Union[str, Tuple[str, ...]]:
"""Custom method to determine what to use as the metavar value of an action"""
if action.metavar is not None:
result = action.metavar
elif action.choices is not None:
choice_strs = [str(choice) for choice in action.choices]
# Begin cmd2 customization (added space after comma)
result = '{%s}' % ', '.join(choice_strs)
# End cmd2 customization
else:
result = default_metavar
return result
def _metavar_formatter(
self,
action: argparse.Action,
default_metavar: Union[str, Tuple[str, ...]],
) -> Callable[[int], Tuple[str, ...]]:
metavar = self._determine_metavar(action, default_metavar)
# noinspection PyMissingOrEmptyDocstring
def format(tuple_size: int) -> Tuple[str, ...]:
if isinstance(metavar, tuple):
return metavar
else:
return (metavar,) * tuple_size
return format
# noinspection PyProtectedMember
def _format_args(self, action: argparse.Action, default_metavar: Union[str, Tuple[str, ...]]) -> str:
"""Customized to handle ranged nargs and make other output less verbose"""
metavar = self._determine_metavar(action, default_metavar)
metavar_formatter = self._metavar_formatter(action, default_metavar)
# Handle nargs specified as a range
nargs_range = action.get_nargs_range() # type: ignore[attr-defined]
if nargs_range is not None:
if nargs_range[1] == constants.INFINITY:
range_str = f'{nargs_range[0]}+'
else:
range_str = f'{nargs_range[0]}..{nargs_range[1]}'
return '{}{{{}}}'.format('%s' % metavar_formatter(1), range_str)
# Make this output less verbose. Do not customize the output when metavar is a
# tuple of strings. Allow argparse's formatter to handle that instead.
elif isinstance(metavar, str):
if action.nargs == ZERO_OR_MORE:
return '[%s [...]]' % metavar_formatter(1)
elif action.nargs == ONE_OR_MORE:
return '%s [...]' % metavar_formatter(1)
elif isinstance(action.nargs, int) and action.nargs > 1:
return '{}{{{}}}'.format('%s' % metavar_formatter(1), action.nargs)
return super()._format_args(action, default_metavar) # type: ignore[arg-type]
# noinspection PyCompatibility
class Cmd2ArgumentParser(argparse.ArgumentParser):
"""Custom ArgumentParser class that improves error and help output"""
def __init__(
self,
prog: Optional[str] = None,
usage: Optional[str] = None,
description: Optional[str] = None,
epilog: Optional[str] = None,
parents: Sequence[argparse.ArgumentParser] = (),
formatter_class: Type[argparse.HelpFormatter] = Cmd2HelpFormatter,
prefix_chars: str = '-',
fromfile_prefix_chars: Optional[str] = None,
argument_default: Optional[str] = None,
conflict_handler: str = 'error',
add_help: bool = True,
allow_abbrev: bool = True,
) -> None:
super(Cmd2ArgumentParser, self).__init__(
prog=prog,
usage=usage,
description=description,
epilog=epilog,
parents=parents if parents else [],
formatter_class=formatter_class, # type: ignore[arg-type]
prefix_chars=prefix_chars,
fromfile_prefix_chars=fromfile_prefix_chars,
argument_default=argument_default,
conflict_handler=conflict_handler,
add_help=add_help,
allow_abbrev=allow_abbrev,
)
def add_subparsers(self, **kwargs: Any) -> argparse._SubParsersAction:
"""
Custom override. Sets a default title if one was not given.
:param kwargs: additional keyword arguments
:return: argparse Subparser Action
"""
if 'title' not in kwargs:
kwargs['title'] = 'subcommands'
return super().add_subparsers(**kwargs)
def error(self, message: str) -> NoReturn:
"""Custom override that applies custom formatting to the error message"""
lines = message.split('\n')
linum = 0
formatted_message = ''
for line in lines:
if linum == 0:
formatted_message = 'Error: ' + line
else:
formatted_message += '\n ' + line
linum += 1
self.print_usage(sys.stderr)
formatted_message = ansi.style_error(formatted_message)
self.exit(2, f'{formatted_message}\n\n')
# noinspection PyProtectedMember
def format_help(self) -> str:
"""Copy of format_help() from argparse.ArgumentParser with tweaks to separately display required parameters"""
formatter = self._get_formatter()
# usage
formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) # type: ignore[arg-type]
# description
formatter.add_text(self.description)
# Begin cmd2 customization (separate required and optional arguments)
# positionals, optionals and user-defined groups
for action_group in self._action_groups:
if sys.version_info >= (3, 10):
default_options_group = action_group.title == 'options'
else:
default_options_group = action_group.title == 'optional arguments'
if default_options_group:
# check if the arguments are required, group accordingly
req_args = []
opt_args = []
for action in action_group._group_actions:
if action.required:
req_args.append(action)
else:
opt_args.append(action)
# separately display required arguments
formatter.start_section('required arguments')
formatter.add_text(action_group.description)
formatter.add_arguments(req_args)
formatter.end_section()
# now display truly optional arguments
formatter.start_section('optional arguments')
formatter.add_text(action_group.description)
formatter.add_arguments(opt_args)
formatter.end_section()
else:
formatter.start_section(action_group.title)
formatter.add_text(action_group.description)
formatter.add_arguments(action_group._group_actions)
formatter.end_section()
# End cmd2 customization
# epilog
formatter.add_text(self.epilog)
# determine help from format above
return formatter.format_help() + '\n'
def _print_message(self, message: str, file: Optional[IO[str]] = None) -> None:
# Override _print_message to use style_aware_write() since we use ANSI escape characters to support color
if message:
if file is None:
file = sys.stderr
ansi.style_aware_write(file, message)
class Cmd2AttributeWrapper:
"""
Wraps a cmd2-specific attribute added to an argparse Namespace.
This makes it easy to know which attributes in a Namespace are
arguments from a parser and which were added by cmd2.
"""
def __init__(self, attribute: Any) -> None:
self.__attribute = attribute
def get(self) -> Any:
"""Get the value of the attribute"""
return self.__attribute
def set(self, new_val: Any) -> None:
"""Set the value of the attribute"""
self.__attribute = new_val
# The default ArgumentParser class for a cmd2 app
DEFAULT_ARGUMENT_PARSER: Type[argparse.ArgumentParser] = Cmd2ArgumentParser
def set_default_argument_parser(parser: Type[argparse.ArgumentParser]) -> None:
"""
Set the default ArgumentParser class for a cmd2 app. This must be called prior to loading cmd2.py if
you want to override the parser for cmd2's built-in commands. See examples/override_parser.py.
"""
global DEFAULT_ARGUMENT_PARSER
DEFAULT_ARGUMENT_PARSER = parser
|