summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorsteven.bethard <devnull@localhost>2010-01-07 17:30:28 +0000
committersteven.bethard <devnull@localhost>2010-01-07 17:30:28 +0000
commit6f0470065b4aa7c8673ba375b4d99b8809999fb5 (patch)
treed45ab818ba62dcc8239546401304537d1f70c5ab /test
parent595bce353fc65c44168df31a9cd3e29b9059b486 (diff)
downloadargparse-6f0470065b4aa7c8673ba375b4d99b8809999fb5.tar.gz
Add convert_arg_line_to_args to allow subclasses to easily override file parsing behavior.
Diffstat (limited to 'test')
-rw-r--r--test/test_argparse.py34
1 files changed, 33 insertions, 1 deletions
diff --git a/test/test_argparse.py b/test/test_argparse.py
index a7eb74a..2e78c88 100644
--- a/test/test_argparse.py
+++ b/test/test_argparse.py
@@ -175,6 +175,8 @@ class ParserTesterMetaclass(type):
# default parser signature is empty
if not hasattr(cls, 'parser_signature'):
cls.parser_signature = Sig()
+ if not hasattr(cls, 'parser_class'):
+ cls.parser_class = ErrorRaisingArgumentParser
# ---------------------------------------
# functions for adding optional arguments
@@ -238,7 +240,7 @@ class ParserTesterMetaclass(type):
def _get_parser(self, tester):
args = tester.parser_signature.args
kwargs = tester.parser_signature.kwargs
- parser = ErrorRaisingArgumentParser(*args, **kwargs)
+ parser = tester.parser_class(*args, **kwargs)
self._add_arguments(parser, tester.argument_signatures)
return parser
@@ -1325,6 +1327,36 @@ class TestArgumentsFromFile(TempDirMixin, ParserTestCase):
]
+class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
+ """Test reading arguments from a file"""
+
+ def setUp(self):
+ super(TestArgumentsFromFileConverter, self).setUp()
+ file_texts = [
+ ('hello', 'hello world!\n'),
+ ]
+ for path, text in file_texts:
+ file = open(path, 'w')
+ file.write(text)
+ file.close()
+
+ class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):
+ def convert_arg_line_to_args(self, arg_line):
+ for arg in arg_line.split():
+ if not arg.strip():
+ continue
+ yield arg
+ parser_class = FromFileConverterArgumentParser
+ parser_signature = Sig(fromfile_prefix_chars='@')
+ argument_signatures = [
+ Sig('y', nargs='+'),
+ ]
+ failures = []
+ successes = [
+ ('@hello X', NS(y=['hello', 'world!', 'X'])),
+ ]
+
+
# =====================
# Type conversion tests
# =====================