File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 1
1
from argparse import ArgumentParser
2
2
from collections import OrderedDict
3
3
from copy import deepcopy
4
+ from functools import partial
4
5
import json
5
6
from pprint import pformat
6
7
import re
@@ -298,7 +299,7 @@ def _add_subparsers(self) -> None:
298
299
299
300
# Load each subparser
300
301
for flag , subparser_type , kwargs in self ._subparser_buffer :
301
- self ._subparsers ._parser_class = subparser_type
302
+ self ._subparsers ._parser_class = partial ( subparser_type , underscores_to_dashes = self . _underscores_to_dashes )
302
303
self ._subparsers .add_parser (flag , ** kwargs )
303
304
304
305
def add_subparsers (self , ** kwargs ) -> None :
Original file line number Diff line number Diff line change 1
1
import sys
2
+ from typing import Union
2
3
from typing_extensions import Literal
3
4
import unittest
4
5
from unittest import TestCase
@@ -195,6 +196,32 @@ def configure(self):
195
196
with self .assertRaises (SystemExit ):
196
197
Args ().parse_args ('b a' .split ())
197
198
199
+ def test_subparser_underscores_to_dashes (self ):
200
+ class AddProposal (Tap ):
201
+ proposal_id : int
202
+
203
+ class Arguments (Tap ):
204
+ def configure (self ) -> None :
205
+ self .add_subparsers (dest = "subparser_name" )
206
+
207
+ self .add_subparser (
208
+ "add-proposal" ,
209
+ AddProposal ,
210
+ help = "Add a new proposal" ,
211
+ )
212
+
213
+ args_underscores : Union [Arguments , AddProposal ] = Arguments (underscores_to_dashes = False ).parse_args ('add-proposal --proposal_id 1' .split ())
214
+ self .assertEqual (args_underscores .proposal_id , 1 )
215
+
216
+ args_dashes : Union [Arguments , AddProposal ] = Arguments (underscores_to_dashes = True ).parse_args ('add-proposal --proposal-id 1' .split ())
217
+ self .assertEqual (args_dashes .proposal_id , 1 )
218
+
219
+ with self .assertRaises (SystemExit ):
220
+ Arguments (underscores_to_dashes = False ).parse_args ('add-proposal --proposal-id 1' .split ())
221
+
222
+ with self .assertRaises (SystemExit ):
223
+ Arguments (underscores_to_dashes = True ).parse_args ('add-proposal --proposal_id 1' .split ())
224
+
198
225
199
226
if __name__ == '__main__' :
200
227
unittest .main ()
You can’t perform that action at this time.
0 commit comments