Skip to content

Commit 1c2c0f5

Browse files
committed
Propagate underscores_to_dashes to subparsers
1 parent 5b2eab6 commit 1c2c0f5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

tap/tap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from argparse import ArgumentParser
22
from collections import OrderedDict
33
from copy import deepcopy
4+
from functools import partial
45
import json
56
from pprint import pformat
67
import re
@@ -298,7 +299,7 @@ def _add_subparsers(self) -> None:
298299

299300
# Load each subparser
300301
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)
302303
self._subparsers.add_parser(flag, **kwargs)
303304

304305
def add_subparsers(self, **kwargs) -> None:

tests/test_subparser.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from typing import Union
23
from typing_extensions import Literal
34
import unittest
45
from unittest import TestCase
@@ -195,6 +196,32 @@ def configure(self):
195196
with self.assertRaises(SystemExit):
196197
Args().parse_args('b a'.split())
197198

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+
198225

199226
if __name__ == '__main__':
200227
unittest.main()

0 commit comments

Comments
 (0)