def test_integration_invalid_default_value(self): argparser = ArgumentParser(prog='prog', add_help=False) with self.assertRaises(ValueError): argparser.add_argument('-p', '--params', action=StoreCommaDelimitedList, default='123')
def test_integration_choices_in_list(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', choices=['1', '2', '3'], action=StoreDelimitedListBase) parsed, extras = argparser.parse_known_args(['-p', '3']) self.assertEqual(parsed.params, ['3']) parsed, extras = argparser.parse_known_args(['-p', '3,2']) self.assertEqual(parsed.params, ['3', '2']) parsed, extras = argparser.parse_known_args(['-p', '3,2,1']) self.assertEqual(parsed.params, ['3', '2', '1']) parsed, extras = argparser.parse_known_args(['-p', '3,3,3']) self.assertEqual(parsed.params, ['3', '3', '3']) stub_stdouts(self) with self.assertRaises(SystemExit): argparser.parse_known_args(['-p', '3,2,1,0']) self.assertIn("(choose from '1', '2', '3')", sys.stderr.getvalue()) with self.assertRaises(SystemExit): argparser.parse_known_args(['-p', '0']) argparser.add_argument( '--dot', choices=['a', 'b', 'c'], action=StoreDelimitedListBase, sep='.') parsed, extras = argparser.parse_known_args(['--dot', 'a.b.c']) self.assertEqual(parsed.dot, ['a', 'b', 'c']) with self.assertRaises(SystemExit): argparser.parse_known_args(['--dot', 'a,b,c'])
def test_integration_set_limit(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreDelimitedListBase, maxlen=1) parsed, extras = argparser.parse_known_args(['-p', '3,4,5']) self.assertEqual(parsed.params, ['3'])
def test_integration_optional_kwargs(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreDelimitedListBase, sep='.') parsed, extras = argparser.parse_known_args(['-p', '3,4.5']) self.assertEqual(parsed.params, ['3,4', '5'])
def test_integration_set_limit(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreDelimitedListBase, maxlen=1) parsed, extras = argparser.parse_known_args(['-p', '3,4,5']) self.assertEqual(parsed.params, ['3'])
def test_integration_optional_kwargs(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreDelimitedListBase, sep='.') parsed, extras = argparser.parse_known_args(['-p', '3,4.5']) self.assertEqual(parsed.params, ['3,4', '5'])
def test_integration_optional(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreCommaDelimitedList, required=False) parsed, extras = argparser.parse_known_args(['-p', '3,4']) self.assertEqual(parsed.params, ['3', '4']) parsed, extras = argparser.parse_known_args() self.assertEqual(parsed.params, None)
def test_integration_optional(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreCommaDelimitedList, required=False) parsed, extras = argparser.parse_known_args(['-p', '3,4']) self.assertEqual(parsed.params, ['3', '4']) parsed, extras = argparser.parse_known_args() self.assertEqual(parsed.params, None)
def test_integration(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreCommaDelimitedList, default=('1', '2',)) parsed, extras = argparser.parse_known_args(['-p', '3,4']) self.assertEqual(parsed.params, ['3', '4']) parsed, extras = argparser.parse_known_args(['-p', '34']) self.assertEqual(parsed.params, ['34']) parsed, extras = argparser.parse_known_args([]) self.assertEqual(parsed.params, ('1', '2'))
def test_integration(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreCommaDelimitedList, default=( '1', '2', )) parsed, extras = argparser.parse_known_args(['-p', '3,4']) self.assertEqual(parsed.params, ['3', '4']) parsed, extras = argparser.parse_known_args(['-p', '34']) self.assertEqual(parsed.params, ['34']) parsed, extras = argparser.parse_known_args([]) self.assertEqual(parsed.params, ('1', '2'))
def test_deprecation(self): import warnings argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-n', '--normal', action='store') argparser.add_argument( '-d', '--deprecated', action='store', deprecation=True) argparser.add_argument( '--bye', action=StoreDelimitedListBase, deprecation='bye') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') # test that they store stuff args = argparser.parse_args(['-n', 'hello']) self.assertEqual(args.normal, 'hello') args = argparser.parse_args(['-d', 'hello']) self.assertEqual(args.deprecated, 'hello') args = argparser.parse_args(['--deprecated', 'hello']) self.assertEqual(args.deprecated, 'hello') args = argparser.parse_args(['--bye', 'hello,goodbye']) self.assertEqual(args.bye, ['hello', 'goodbye']) # and the warnings are triggered self.assertEqual( "option '-d' is deprecated", str(w[0].message)) self.assertEqual( "option '--deprecated' is deprecated", str(w[1].message)) self.assertEqual( "option '--bye' is deprecated: bye", str(w[2].message)) stream = StringIO() argparser.print_help(file=stream) # deprecated options are not visible on help self.assertNotIn("--deprecated", stream.getvalue()) self.assertNotIn("--bye", stream.getvalue()) self.assertIn("--normal", stream.getvalue())
def test_integration_edge_cases(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreCommaDelimitedList, default=['1', '2']) parsed, extras = argparser.parse_known_args(['--params=1,2']) self.assertEqual(parsed.params, ['1', '2']) parsed, extras = argparser.parse_known_args(['--params=8', '-p', '3']) self.assertEqual(parsed.params, ['8', '3']) parsed, extras = argparser.parse_known_args(['--params', '']) self.assertEqual(parsed.params, []) parsed, extras = argparser.parse_known_args(['--params=']) self.assertEqual(parsed.params, []) parsed, extras = argparser.parse_known_args(['--params=,']) self.assertEqual(parsed.params, ['']) parsed, extras = argparser.parse_known_args(['--params=1,']) self.assertEqual(parsed.params, ['1'])
def test_integration_choices_in_list(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', choices=['1', '2', '3'], action=StoreDelimitedListBase) parsed, extras = argparser.parse_known_args(['-p', '3']) self.assertEqual(parsed.params, ['3']) parsed, extras = argparser.parse_known_args(['-p', '3,2']) self.assertEqual(parsed.params, ['3', '2']) parsed, extras = argparser.parse_known_args(['-p', '3,2,1']) self.assertEqual(parsed.params, ['3', '2', '1']) parsed, extras = argparser.parse_known_args(['-p', '3,3,3']) self.assertEqual(parsed.params, ['3', '3', '3']) stub_stdouts(self) with self.assertRaises(SystemExit): argparser.parse_known_args(['-p', '3,2,1,0']) self.assertIn("(choose from '1', '2', '3')", sys.stderr.getvalue()) with self.assertRaises(SystemExit): argparser.parse_known_args(['-p', '0']) argparser.add_argument('--dot', choices=['a', 'b', 'c'], action=StoreDelimitedListBase, sep='.') parsed, extras = argparser.parse_known_args(['--dot', 'a.b.c']) self.assertEqual(parsed.dot, ['a', 'b', 'c']) with self.assertRaises(SystemExit): argparser.parse_known_args(['--dot', 'a,b,c'])
def test_integration_edge_cases(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreCommaDelimitedList, default=['1', '2']) parsed, extras = argparser.parse_known_args(['--params=1,2']) self.assertEqual(parsed.params, ['1', '2']) parsed, extras = argparser.parse_known_args(['--params=8', '-p', '3']) self.assertEqual(parsed.params, ['8', '3']) parsed, extras = argparser.parse_known_args(['--params', '']) self.assertEqual(parsed.params, []) parsed, extras = argparser.parse_known_args(['--params=']) self.assertEqual(parsed.params, []) parsed, extras = argparser.parse_known_args(['--params=,']) self.assertEqual(parsed.params, ['']) parsed, extras = argparser.parse_known_args(['--params=1,']) self.assertEqual(parsed.params, ['1'])
def test_deprecation(self): import warnings argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-n', '--normal', action='store') argparser.add_argument('-d', '--deprecated', action='store', deprecation=True) argparser.add_argument('--bye', action=StoreDelimitedListBase, deprecation='bye') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') # test that they store stuff args = argparser.parse_args(['-n', 'hello']) self.assertEqual(args.normal, 'hello') args = argparser.parse_args(['-d', 'hello']) self.assertEqual(args.deprecated, 'hello') args = argparser.parse_args(['--deprecated', 'hello']) self.assertEqual(args.deprecated, 'hello') args = argparser.parse_args(['--bye', 'hello,goodbye']) self.assertEqual(args.bye, ['hello', 'goodbye']) # and the warnings are triggered self.assertEqual("option '-d' is deprecated", str(w[0].message)) self.assertEqual("option '--deprecated' is deprecated", str(w[1].message)) self.assertEqual("option '--bye' is deprecated: bye", str(w[2].message)) stream = StringIO() argparser.print_help(file=stream) # deprecated options are not visible on help self.assertNotIn("--deprecated", stream.getvalue()) self.assertNotIn("--bye", stream.getvalue()) self.assertIn("--normal", stream.getvalue())
def test_integration_invalid_default_value(self): argparser = ArgumentParser(prog='prog', add_help=False) with self.assertRaises(ValueError): argparser.add_argument( '-p', '--params', action=StoreCommaDelimitedList, default='123')