feat: allow undefined option

This commit is contained in:
egarette@silique.fr 2026-06-11 22:24:49 +02:00
parent 0d4c822210
commit 3fca16c874
8 changed files with 107 additions and 85 deletions

View file

@ -41,7 +41,7 @@ root:
--root.verbosity increase output verbosity (default: False) --root.verbosity increase output verbosity (default: False)
--root.no-verbosity --root.no-verbosity
""" """
parser = TiramisuCmdlineParser(get_config(has_tree=True), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(has_tree=True), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -56,7 +56,7 @@ options:
--verbosity increase output verbosity (default: False) --verbosity increase output verbosity (default: False)
--no-verbosity --no-verbosity
""" """
parser = TiramisuCmdlineParser(get_config(), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -71,7 +71,7 @@ options:
--verbosity increase output verbosity (default: True) --verbosity increase output verbosity (default: True)
--no-verbosity --no-verbosity
""" """
parser = TiramisuCmdlineParser(get_config(default_verbosity=True), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(default_verbosity=True), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -80,47 +80,47 @@ options:
def test_boolean_true(): def test_boolean_true():
config = get_config(default_verbosity=True) config = get_config(default_verbosity=True)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
assert to_dict(config.value.get()) == {'verbosity': True} assert to_dict(config.value.get()) == {'verbosity': True}
def test_boolean_false(): def test_boolean_false():
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
assert to_dict(config.value.get()) == {'verbosity': False} assert to_dict(config.value.get()) == {'verbosity': False}
def test_boolean_true_to_false(): def test_boolean_true_to_false():
config = get_config(default_verbosity=True) config = get_config(default_verbosity=True)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
parser.parse_args(['--no-verbosity']) parser.parse_args(['--no-verbosity'])
assert to_dict(config.value.get()) == {'verbosity': False} assert to_dict(config.value.get()) == {'verbosity': False}
def test_boolean_true_to_true(): def test_boolean_true_to_true():
config = get_config(default_verbosity=True) config = get_config(default_verbosity=True)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
parser.parse_args(['--verbosity']) parser.parse_args(['--verbosity'])
assert to_dict(config.value.get()) == {'verbosity': True} assert to_dict(config.value.get()) == {'verbosity': True}
def test_boolean_false_to_true(): def test_boolean_false_to_true():
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
parser.parse_args(['--verbosity']) parser.parse_args(['--verbosity'])
assert to_dict(config.value.get()) == {'verbosity': True} assert to_dict(config.value.get()) == {'verbosity': True}
def test_boolean_false_to_false(): def test_boolean_false_to_false():
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
parser.parse_args(['--verbosity']) parser.parse_args(['--verbosity'])
assert to_dict(config.value.get()) == {'verbosity': True} assert to_dict(config.value.get()) == {'verbosity': True}
def test_boolean_disabled(): def test_boolean_disabled():
config = get_config(default_verbosity=True) config = get_config(default_verbosity=True)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -134,7 +134,7 @@ prog.py: error: unrecognized arguments: --disabled (cannot access to option "dis
def test_boolean_no_disabled(): def test_boolean_no_disabled():
config = get_config(default_verbosity=True) config = get_config(default_verbosity=True)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:

View file

@ -58,7 +58,7 @@ prog.py: error: argument positional: invalid choice: 'error' (choose from str, l
prog.py: error: argument positional_int: invalid choice: '4' (choose from 1, 2, 3) prog.py: error: argument positional_int: invalid choice: '4' (choose from 1, 2, 3)
''' '''
config = get_config(json) config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
parser.parse_args(['str', '1']) parser.parse_args(['str', '1'])
assert to_dict(config.value.get()) == {'positional': 'str', assert to_dict(config.value.get()) == {'positional': 'str',
'positional_int': 1, 'positional_int': 1,
@ -91,7 +91,7 @@ def test_choice_str(json):
prog.py: error: argument --str: invalid choice: 'error' (choose from str1, str2, str3) prog.py: error: argument --str: invalid choice: 'error' (choose from str1, str2, str3)
""" """
config = get_config(json) config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
parser.parse_args(['str', '1', '--str', 'str1']) parser.parse_args(['str', '1', '--str', 'str1'])
assert to_dict(config.value.get()) == {'positional': 'str', assert to_dict(config.value.get()) == {'positional': 'str',
'positional_int': 1, 'positional_int': 1,
@ -131,7 +131,7 @@ def test_choice_int(json):
prog.py: error: argument --int: invalid choice: '4' (choose from 1, 2, 3) prog.py: error: argument --int: invalid choice: '4' (choose from 1, 2, 3)
""" """
config = get_config(json) config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
parser.parse_args(['str', '1', '--int', '1']) parser.parse_args(['str', '1', '--int', '1'])
assert to_dict(config.value.get()) == {'positional': 'str', assert to_dict(config.value.get()) == {'positional': 'str',
'positional_int': 1, 'positional_int': 1,
@ -159,7 +159,7 @@ def test_choice_int_multi(json):
prog.py: error: argument --int_multi: invalid choice: '4' (choose from 1, 2, 3) prog.py: error: argument --int_multi: invalid choice: '4' (choose from 1, 2, 3)
""" """
config = get_config(json) config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
parser.parse_args(['str', '1', '--int_multi', '1', '2']) parser.parse_args(['str', '1', '--int_multi', '1', '2'])
assert to_dict(config.value.get()) == {'positional': 'str', assert to_dict(config.value.get()) == {'positional': 'str',
'positional_int': 1, 'positional_int': 1,

View file

@ -51,7 +51,7 @@ options:
od: od:
{str,list,int,none} choice the sub argument {str,list,int,none} choice the sub argument
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -69,7 +69,7 @@ od:
two line two line
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline", formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline", formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()

View file

@ -78,7 +78,7 @@ leader:
--leader.follower_mandatory INDEX FOLLOWER_MANDATORY --leader.follower_mandatory INDEX FOLLOWER_MANDATORY
Follower mandatory Follower mandatory
""" """
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -107,7 +107,7 @@ leader:
--leader.follower_mandatory INDEX FOLLOWER_MANDATORY --leader.follower_mandatory INDEX FOLLOWER_MANDATORY
Follower mandatory Follower mandatory
""" """
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -124,7 +124,7 @@ leader:
-l, --leader.leader [LEADER ...] -l, --leader.leader [LEADER ...]
Leader var Leader var
""" """
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True, with_default_value=False), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True, with_default_value=False), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -153,7 +153,7 @@ leader:
--leader.follower_mandatory INDEX FOLLOWER_MANDATORY --leader.follower_mandatory INDEX FOLLOWER_MANDATORY
Follower mandatory Follower mandatory
""" """
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -194,7 +194,7 @@ prog.py: error: unrecognized arguments: 255.255.255.0
""" """
config = get_config(json) config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -309,7 +309,7 @@ def test_leadership_modif_follower_choice_unknown(json):
prog.py: error: argument --leader.follower_choice: invalid choice: 'opt_unknown' (choose from opt1, opt2) prog.py: error: argument --leader.follower_choice: invalid choice: 'opt_unknown' (choose from opt1, opt2)
""" """
config = get_config(json) config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -327,7 +327,7 @@ prog.py: error: index must be a number, not a
""" """
config = get_config(json) config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -384,7 +384,7 @@ def test_leadership_modif_mandatory(json):
prog.py: error: the following arguments are required: --leader.follower_submulti""" prog.py: error: the following arguments are required: --leader.follower_submulti"""
config = get_config(json, with_mandatory=True) config = get_config(json, with_mandatory=True)
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -422,7 +422,7 @@ def test_leadership_modif_mandatory_remove(json):
prog.py: error: the following arguments are required: --leader.follower_submulti""" prog.py: error: the following arguments are required: --leader.follower_submulti"""
config = get_config(json, with_mandatory=True) config = get_config(json, with_mandatory=True)
parser = TiramisuCmdlineParser(config, 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(config, 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:

View file

@ -99,7 +99,7 @@ options:
-v, --verbosity increase output verbosity (default: False) -v, --verbosity increase output verbosity (default: False)
-nv, --no-verbosity -nv, --no-verbosity
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -117,7 +117,7 @@ root:
-v, --root.verbosity increase output verbosity (default: False) -v, --root.verbosity increase output verbosity (default: False)
-nv, --root.no-verbosity -nv, --root.no-verbosity
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -135,7 +135,7 @@ root:
-v, --verbosity increase output verbosity (default: False) -v, --verbosity increase output verbosity (default: False)
-nv, --no-verbosity -nv, --no-verbosity
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
@ -154,7 +154,7 @@ options:
-nv, --no-verbosity -nv, --no-verbosity
--str STR string option --str STR string option
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
try: try:
@ -175,7 +175,7 @@ options:
-nv, --no-verbosity -nv, --no-verbosity
--str STR string option --str STR string option
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
try: try:
@ -199,7 +199,7 @@ options:
-nv, --no-verbosity -nv, --no-verbosity
--str STR string option --str STR string option
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
try: try:
@ -219,7 +219,7 @@ options:
-v, --verbosity increase output verbosity (default: False) -v, --verbosity increase output verbosity (default: False)
-nv, --no-verbosity -nv, --no-verbosity
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
try: try:
@ -243,7 +243,7 @@ options:
-nv, --no-verbosity -nv, --no-verbosity
--str STR string option --str STR string option
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
try: try:
@ -264,7 +264,7 @@ options:
-nv, --no-verbosity increase output verbosity (default: False) -nv, --no-verbosity increase output verbosity (default: False)
--str STR string option --str STR string option
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
try: try:
@ -288,7 +288,7 @@ options:
-nv, --no-verbosity -nv, --no-verbosity
--str STR string option --str STR string option
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
try: try:
@ -308,7 +308,7 @@ options:
-v, --verbosity increase output verbosity (default: False) -v, --verbosity increase output verbosity (default: False)
--str STR string option --str STR string option
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
try: try:
@ -324,7 +324,7 @@ def test_readme_positional_mandatory(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none} output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: the following arguments are required: cmd prog.py: error: the following arguments are required: cmd
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -340,7 +340,7 @@ def test_readme_positional_mandatory_tree(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none} output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: the following arguments are required: root.cmd prog.py: error: the following arguments are required: root.cmd
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -356,7 +356,7 @@ def test_readme_positional_mandatory_tree_flatten(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none} output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: the following arguments are required: cmd prog.py: error: the following arguments are required: cmd
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -372,7 +372,7 @@ def test_readme_mandatory(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none} output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
prog.py: error: the following arguments are required: --str prog.py: error: the following arguments are required: --str
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -388,7 +388,7 @@ def test_readme_mandatory_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR
prog.py: error: the following arguments are required: --str prog.py: error: the following arguments are required: --str
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -404,7 +404,7 @@ def test_readme_mandatory_tree(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR {str,list,int,none} output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR {str,list,int,none}
prog.py: error: the following arguments are required: --root.str prog.py: error: the following arguments are required: --root.str
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -420,7 +420,7 @@ def test_readme_mandatory_tree_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR
prog.py: error: the following arguments are required: --root.str prog.py: error: the following arguments are required: --root.str
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -436,7 +436,7 @@ def test_readme_mandatory_tree_flatten(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none} output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
prog.py: error: the following arguments are required: --str prog.py: error: the following arguments are required: --str
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -452,7 +452,7 @@ def test_readme_mandatory_tree_flatten_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR
prog.py: error: the following arguments are required: --str prog.py: error: the following arguments are required: --str
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -468,7 +468,7 @@ def test_readme_cross(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none} output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int (cannot access to option "int option" because has property "disabled") prog.py: error: unrecognized arguments: --int (cannot access to option "int option" because has property "disabled")
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -484,7 +484,7 @@ def test_readme_cross_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --int (cannot access to option "int option" because has property "disabled") prog.py: error: unrecognized arguments: --int (cannot access to option "int option" because has property "disabled")
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -500,7 +500,7 @@ def test_readme_cross_tree(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none} output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --root.int (cannot access to option "int option" because has property "disabled") prog.py: error: unrecognized arguments: --root.int (cannot access to option "int option" because has property "disabled")
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -516,7 +516,7 @@ def test_readme_cross_tree_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --root.int (cannot access to option "int option" because has property "disabled") prog.py: error: unrecognized arguments: --root.int (cannot access to option "int option" because has property "disabled")
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -532,7 +532,7 @@ def test_readme_cross_tree_flatten(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none} output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int (cannot access to option "int option" because has property "disabled") prog.py: error: unrecognized arguments: --int (cannot access to option "int option" because has property "disabled")
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -548,7 +548,7 @@ def test_readme_cross_tree_flatten_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --int (cannot access to option "int option" because has property "disabled") prog.py: error: unrecognized arguments: --int (cannot access to option "int option" because has property "disabled")
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -564,7 +564,7 @@ def test_readme_unknown(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none} output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: argument root.cmd: invalid choice: 'unknown' (choose from str, list, int, none) prog.py: error: argument root.cmd: invalid choice: 'unknown' (choose from str, list, int, none)
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -879,7 +879,7 @@ prog.py: error: unrecognized arguments: --unknown
output2 = """usage: prog.py "list" -v --list "a" [-h] [-v] [-nv] --list LIST [LIST ...] {str,list,int,none} output2 = """usage: prog.py "list" -v --list "a" [-h] [-v] [-nv] --list LIST [LIST ...] {str,list,int,none}
prog.py: error: unrecognized arguments: --root.unknown prog.py: error: unrecognized arguments: --root.unknown
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter, color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:

View file

@ -39,19 +39,19 @@ def test_short(json):
# #
output = {'list': None, 'l': None} output = {'list': None, 'l': None}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args([]) parser.parse_args([])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': 'a', 'l': 'a'} output = {'list': 'a', 'l': 'a'}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['--list', 'a']) parser.parse_args(['--list', 'a'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': 'a', 'l': 'a'} output = {'list': 'a', 'l': 'a'}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['-l', 'a']) parser.parse_args(['-l', 'a'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
@ -81,7 +81,7 @@ def test_short_mandatory(json):
prog.py: error: the following arguments are required: --list prog.py: error: the following arguments are required: --list
""" """
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -94,13 +94,13 @@ prog.py: error: the following arguments are required: --list
# #
output = {'list': 'a', 'l': 'a'} output = {'list': 'a', 'l': 'a'}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['--list', 'a']) parser.parse_args(['--list', 'a'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': 'a', 'l': 'a'} output = {'list': 'a', 'l': 'a'}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['-l', 'a']) parser.parse_args(['-l', 'a'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
@ -124,31 +124,31 @@ def test_short_multi(json):
# #
output = {'list': [], 'l': []} output = {'list': [], 'l': []}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args([]) parser.parse_args([])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': ['a'], 'l': ['a']} output = {'list': ['a'], 'l': ['a']}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['--list', 'a']) parser.parse_args(['--list', 'a'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': ['a', 'b'], 'l': ['a', 'b']} output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['--list', 'a', 'b']) parser.parse_args(['--list', 'a', 'b'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': ['a'], 'l': ['a']} output = {'list': ['a'], 'l': ['a']}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['-l', 'a']) parser.parse_args(['-l', 'a'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': ['a', 'b'], 'l': ['a', 'b']} output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['-l', 'a', 'b']) parser.parse_args(['-l', 'a', 'b'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
@ -175,7 +175,7 @@ def test_short_multi_mandatory(json):
prog.py: error: the following arguments are required: --list prog.py: error: the following arguments are required: --list
""" """
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
@ -188,24 +188,24 @@ prog.py: error: the following arguments are required: --list
# #
output = {'list': ['a'], 'l': ['a']} output = {'list': ['a'], 'l': ['a']}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['--list', 'a']) parser.parse_args(['--list', 'a'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': ['a', 'b'], 'l': ['a', 'b']} output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['--list', 'a', 'b']) parser.parse_args(['--list', 'a', 'b'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': ['a'], 'l': ['a']} output = {'list': ['a'], 'l': ['a']}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['-l', 'a']) parser.parse_args(['-l', 'a'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output
# #
output = {'list': ['a', 'b'], 'l': ['a', 'b']} output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config() config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py', color=False)
parser.parse_args(['-l', 'a', 'b']) parser.parse_args(['-l', 'a', 'b'])
assert to_dict(config.value.get()) == output assert to_dict(config.value.get()) == output

View file

@ -1,4 +1,4 @@
# Copyright (C) 2018-2019 Team tiramisu (see AUTHORS for all contributors) # Copyright (C) 2018-2026 Team tiramisu (see AUTHORS for all contributors)
# #
# This program is free software: you can redistribute it and/or modify it # This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the # under the terms of the GNU Lesser General Public License as published by the

View file

@ -27,7 +27,7 @@ from gettext import gettext as _
# try: # try:
from tiramisu import Config from tiramisu import Config
from tiramisu.error import PropertiesOptionError, LeadershipError, ConfigError from tiramisu.error import PropertiesOptionError, LeadershipError, ConfigError, AttributeOptionError
def get_choice_list(config, properties, display): def get_choice_list(config, properties, display):
@ -110,6 +110,12 @@ class TiramisuNamespace(Namespace):
else: else:
true_key = key true_key = key
option = self._config.option(true_key) option = self._config.option(true_key)
try:
option.get()
except AttributeOptionError:
# We are in this case when we call a parent ArgumentParser not present in current config
# it's useful to dispatch attribute in differents configs
return
if option.isfollower(): if option.isfollower():
_setattr = self._setattr_follower _setattr = self._setattr_follower
index = value[0] index = value[0]
@ -337,6 +343,7 @@ class TiramisuCmdlineParser(ArgumentParser):
self.display_modified_value = display_modified_value self.display_modified_value = display_modified_value
self.short_name_max_len = short_name_max_len self.short_name_max_len = short_name_max_len
self.add_help = add_help self.add_help = add_help
self.kwargs = kwargs.copy()
if TiramisuHelpFormatter not in formatter_class.__mro__: if TiramisuHelpFormatter not in formatter_class.__mro__:
formatter_class = type( formatter_class = type(
"TiramisuHelpFormatter", (TiramisuHelpFormatter, formatter_class), {} "TiramisuHelpFormatter", (TiramisuHelpFormatter, formatter_class), {}
@ -395,6 +402,10 @@ class TiramisuCmdlineParser(ArgumentParser):
except (ValueError, LeadershipError, AttributeError) as err: except (ValueError, LeadershipError, AttributeError) as err:
self.error(err) self.error(err)
if args != args_ and args_ and args_[0].startswith(self.prefix_chars): if args != args_ and args_ and args_[0].startswith(self.prefix_chars):
kwargs = self.kwargs
if "epilog" in kwargs:
kwargs = kwargs.copy()
del kwargs["epilog"]
# option that was disabled are no more disable # option that was disabled are no more disable
# so create a new parser # so create a new parser
new_parser = TiramisuCmdlineParser( new_parser = TiramisuCmdlineParser(
@ -412,6 +423,7 @@ class TiramisuCmdlineParser(ArgumentParser):
short_name_max_len=self.short_name_max_len, short_name_max_len=self.short_name_max_len,
fullpath=self.fullpath, fullpath=self.fullpath,
add_help=self.add_help, add_help=self.add_help,
**kwargs,
) )
namespace_, args_ = new_parser._parse_known_args( namespace_, args_ = new_parser._parse_known_args(
args_, new_parser.namespace, *others args_, new_parser.namespace, *others
@ -694,15 +706,22 @@ class TiramisuCmdlineParser(ArgumentParser):
args, kwargs = value.get() args, kwargs = value.get()
group.add_argument(*args, **kwargs) group.add_argument(*args, **kwargs)
# def _valid_mandatory(self):
# pass
#
def parse_args(self, *args, valid_mandatory=True, **kwargs): def parse_args(self, *args, valid_mandatory=True, **kwargs):
namespaces, unknown = self.parse_known_args(*args, valid_mandatory=valid_mandatory, **kwargs)
if unknown:
msg_unknown = 'unrecognized arguments: %s' % ' '.join(unknown)
if self.exit_on_error:
self.error(msg_unknown)
else:
err = ArgumentError(None, msg_unknown)
err.unknown = unknown
raise err
return namespaces
def parse_known_args(self, *args, valid_mandatory=True, **kwargs):
kwargs["namespace"] = self.namespace kwargs["namespace"] = self.namespace
try: try:
namespaces, unknown = super().parse_known_args(*args, **kwargs) namespaces, unknown = super().parse_known_args(*args, **kwargs)
if unknown:
msg_unknown = 'unrecognized arguments: %s' % ' '.join(unknown)
except PropertiesOptionError as err: except PropertiesOptionError as err:
name = err._subconfig.path name = err._subconfig.path
properties = self.config.option(name).property.get() properties = self.config.option(name).property.get()
@ -745,16 +764,13 @@ class TiramisuCmdlineParser(ArgumentParser):
self.error( self.error(
"the following arguments are required: {}".format(", ".join(errors)) "the following arguments are required: {}".format(", ".join(errors))
) )
if unknown: return namespaces, unknown
if self.exit_on_error:
self.error(msg_unknown)
else:
err = ArgumentError(None, msg_unknown)
err.unknown = unknown
raise err
return namespaces
def format_usage(self, *args, **kwargs): def format_usage(self, *args, **kwargs):
kwargs_ = self.kwargs
if "epilog" in kwargs_:
kwargs_ = kwargs_.copy()
del kwargs_["epilog"]
help_formatter = TiramisuCmdlineParser( help_formatter = TiramisuCmdlineParser(
self.config, self.config,
self.prog, self.prog,
@ -768,12 +784,17 @@ class TiramisuCmdlineParser(ArgumentParser):
epilog=self.epilog, epilog=self.epilog,
description=self.description, description=self.description,
_forhelp=True, _forhelp=True,
**kwargs_,
) )
return super(TiramisuCmdlineParser, help_formatter).format_usage( return super(TiramisuCmdlineParser, help_formatter).format_usage(
*args, **kwargs *args, **kwargs
) )
def format_help(self): def format_help(self):
kwargs = self.kwargs
if "epilog" in kwargs:
kwargs = kwargs.copy()
del kwargs["epilog"]
help_formatter = TiramisuCmdlineParser( help_formatter = TiramisuCmdlineParser(
self.config, self.config,
self.prog, self.prog,
@ -787,6 +808,7 @@ class TiramisuCmdlineParser(ArgumentParser):
epilog=self.epilog, epilog=self.epilog,
description=self.description, description=self.description,
_forhelp=True, _forhelp=True,
**kwargs,
) )
return super(TiramisuCmdlineParser, help_formatter).format_help() return super(TiramisuCmdlineParser, help_formatter).format_help()