Compare commits
13 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ca99df30e | |||
| 9da6c6f421 | |||
| db2402b04a | |||
| bf40fe1a70 | |||
| 03f84ef27e | |||
| ce59a2544b | |||
| 0e86a7ef43 | |||
| 17ce3b77d5 | |||
| 8d2536543b | |||
| d480d5bf5c | |||
| fb0d9c9d22 | |||
| 093819f051 | |||
| b5347fb6cc |
9 changed files with 288 additions and 75 deletions
42
CHANGELOG.md
42
CHANGELOG.md
|
|
@ -1,3 +1,45 @@
|
||||||
|
## 0.7.0a2 (2025-09-29)
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
|
||||||
|
- better support for boolean help
|
||||||
|
|
||||||
|
## 0.7.0a1 (2025-05-12)
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
|
||||||
|
- black
|
||||||
|
|
||||||
|
## 0.7.0a0 (2025-04-30)
|
||||||
|
|
||||||
|
### Feat
|
||||||
|
|
||||||
|
- for boolean always add --xxx and --no-xxx
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
|
||||||
|
- formatter.short_name_max_len for symlink
|
||||||
|
|
||||||
|
## 0.6.2rc2 (2025-04-09)
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
|
||||||
|
- version
|
||||||
|
|
||||||
|
## 0.6.2rc1 (2025-03-19)
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
|
||||||
|
- better leader support
|
||||||
|
|
||||||
|
## 0.6.2rc0 (2025-01-03)
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
|
||||||
|
- python 2.12 support
|
||||||
|
|
||||||
|
## 0.6.1 (2024-11-06)
|
||||||
|
|
||||||
## 0.6.1rc0 (2024-11-06)
|
## 0.6.1rc0 (2024-11-06)
|
||||||
|
|
||||||
### Fix
|
### Fix
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ requires = ["flit_core >=3.8.0,<4"]
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "tiramisu_cmdline_parser"
|
name = "tiramisu_cmdline_parser"
|
||||||
version = "0.6.1"
|
version = "0.7.0a2"
|
||||||
authors = [{name = "Emmanuel Garette", email = "gnunux@gnunux.info"}]
|
authors = [{name = "Emmanuel Garette", email = "gnunux@gnunux.info"}]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
description = "command-line parser using Tiramisu"
|
description = "command-line parser using Tiramisu"
|
||||||
|
|
@ -18,6 +18,8 @@ classifiers = [
|
||||||
"Programming Language :: Python :: 3.10",
|
"Programming Language :: Python :: 3.10",
|
||||||
"Programming Language :: Python :: 3.11",
|
"Programming Language :: Python :: 3.11",
|
||||||
"Programming Language :: Python :: 3.12",
|
"Programming Language :: Python :: 3.12",
|
||||||
|
"Programming Language :: Python :: 3.13",
|
||||||
|
"Programming Language :: Python :: 3.14",
|
||||||
"Programming Language :: Python :: 3",
|
"Programming Language :: Python :: 3",
|
||||||
"Operating System :: OS Independent",
|
"Operating System :: OS Independent",
|
||||||
"Natural Language :: English",
|
"Natural Language :: English",
|
||||||
|
|
@ -37,5 +39,9 @@ name = "cz_conventional_commits"
|
||||||
tag_format = "$version"
|
tag_format = "$version"
|
||||||
version_scheme = "pep440"
|
version_scheme = "pep440"
|
||||||
version_provider = "pep621"
|
version_provider = "pep621"
|
||||||
#update_changelog_on_bump = true
|
version_files = [
|
||||||
|
"tiramisu_cmdline_parser/__version__.py",
|
||||||
|
"pyproject.toml:version"
|
||||||
|
]
|
||||||
|
update_changelog_on_bump = true
|
||||||
changelog_merge_prerelease = true
|
changelog_merge_prerelease = true
|
||||||
|
|
|
||||||
146
tests/test_boolean.py
Normal file
146
tests/test_boolean.py
Normal file
|
|
@ -0,0 +1,146 @@
|
||||||
|
from io import StringIO
|
||||||
|
from contextlib import redirect_stdout, redirect_stderr
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
from tiramisu_cmdline_parser import TiramisuCmdlineParser
|
||||||
|
from tiramisu import BoolOption, OptionDescription, Config
|
||||||
|
from .utils import TestHelpFormatter, to_dict
|
||||||
|
|
||||||
|
|
||||||
|
def get_config(has_tree=False, default_verbosity=False):
|
||||||
|
booloption = BoolOption('disabled',
|
||||||
|
'disabled',
|
||||||
|
properties=('disabled',),
|
||||||
|
)
|
||||||
|
booloption2 = BoolOption('verbosity',
|
||||||
|
'increase output verbosity',
|
||||||
|
default=default_verbosity,
|
||||||
|
)
|
||||||
|
root = OptionDescription('root',
|
||||||
|
'root',
|
||||||
|
[booloption, booloption2],
|
||||||
|
)
|
||||||
|
if has_tree:
|
||||||
|
root = OptionDescription('root',
|
||||||
|
'root',
|
||||||
|
[root],
|
||||||
|
)
|
||||||
|
config = Config(root)
|
||||||
|
config.property.read_write()
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_help_tree():
|
||||||
|
output = """usage: prog.py [-h] [--root.verbosity] [--root.no-verbosity]
|
||||||
|
|
||||||
|
options:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
|
||||||
|
root:
|
||||||
|
--root.verbosity increase output verbosity (default: False)
|
||||||
|
--root.no-verbosity
|
||||||
|
"""
|
||||||
|
parser = TiramisuCmdlineParser(get_config(has_tree=True), 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
f = StringIO()
|
||||||
|
with redirect_stdout(f):
|
||||||
|
parser.print_help()
|
||||||
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_help():
|
||||||
|
output = """usage: prog.py [-h] [--verbosity] [--no-verbosity]
|
||||||
|
|
||||||
|
options:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
--verbosity increase output verbosity (default: False)
|
||||||
|
--no-verbosity
|
||||||
|
"""
|
||||||
|
parser = TiramisuCmdlineParser(get_config(), 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
f = StringIO()
|
||||||
|
with redirect_stdout(f):
|
||||||
|
parser.print_help()
|
||||||
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_help2():
|
||||||
|
output = """usage: prog.py [-h] [--verbosity] [--no-verbosity]
|
||||||
|
|
||||||
|
options:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
--verbosity increase output verbosity (default: True)
|
||||||
|
--no-verbosity
|
||||||
|
"""
|
||||||
|
parser = TiramisuCmdlineParser(get_config(default_verbosity=True), 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
f = StringIO()
|
||||||
|
with redirect_stdout(f):
|
||||||
|
parser.print_help()
|
||||||
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_true():
|
||||||
|
config = get_config(default_verbosity=True)
|
||||||
|
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
assert to_dict(config.value.get()) == {'verbosity': True}
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_false():
|
||||||
|
config = get_config()
|
||||||
|
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
assert to_dict(config.value.get()) == {'verbosity': False}
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_true_to_false():
|
||||||
|
config = get_config(default_verbosity=True)
|
||||||
|
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
parser.parse_args(['--no-verbosity'])
|
||||||
|
assert to_dict(config.value.get()) == {'verbosity': False}
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_true_to_true():
|
||||||
|
config = get_config(default_verbosity=True)
|
||||||
|
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
parser.parse_args(['--verbosity'])
|
||||||
|
assert to_dict(config.value.get()) == {'verbosity': True}
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_false_to_true():
|
||||||
|
config = get_config()
|
||||||
|
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
parser.parse_args(['--verbosity'])
|
||||||
|
assert to_dict(config.value.get()) == {'verbosity': True}
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_false_to_false():
|
||||||
|
config = get_config()
|
||||||
|
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
parser.parse_args(['--verbosity'])
|
||||||
|
assert to_dict(config.value.get()) == {'verbosity': True}
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_disabled():
|
||||||
|
config = get_config(default_verbosity=True)
|
||||||
|
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
f = StringIO()
|
||||||
|
with redirect_stderr(f):
|
||||||
|
try:
|
||||||
|
parser.parse_args(['--disabled'])
|
||||||
|
except SystemExit as err:
|
||||||
|
assert str(err) == "2"
|
||||||
|
assert f.getvalue() == """usage: prog.py [-h] [--verbosity] [--no-verbosity]
|
||||||
|
prog.py: error: unrecognized arguments: --disabled (cannot access to option "disabled" because has property "disabled")
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_no_disabled():
|
||||||
|
config = get_config(default_verbosity=True)
|
||||||
|
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
|
||||||
|
f = StringIO()
|
||||||
|
with redirect_stderr(f):
|
||||||
|
try:
|
||||||
|
parser.parse_args(['--no-disabled'])
|
||||||
|
except SystemExit as err:
|
||||||
|
assert str(err) == "2"
|
||||||
|
assert f.getvalue() == """usage: prog.py [-h] [--verbosity] [--no-verbosity]
|
||||||
|
prog.py: error: unrecognized arguments: --no-disabled (cannot access to option "disabled" because has property "disabled")
|
||||||
|
"""
|
||||||
|
|
@ -52,10 +52,10 @@ def json(request):
|
||||||
|
|
||||||
def test_choice_positional(json):
|
def test_choice_positional(json):
|
||||||
output1 = '''usage: prog.py "str" "1" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
output1 = '''usage: prog.py "str" "1" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
||||||
prog.py: error: argument positional: invalid choice: 'error' (choose from 'str', 'list', 'int', 'none')
|
prog.py: error: argument positional: invalid choice: 'error' (choose from str, list, int, none)
|
||||||
'''
|
'''
|
||||||
output2 = '''usage: prog.py "str" "1" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
output2 = '''usage: prog.py "str" "1" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
||||||
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)
|
||||||
|
|
@ -88,7 +88,7 @@ prog.py: error: argument positional_int: invalid choice: '4' (choose from '1', '
|
||||||
|
|
||||||
def test_choice_str(json):
|
def test_choice_str(json):
|
||||||
output = """usage: prog.py "str" "1" --str "str3" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
output = """usage: prog.py "str" "1" --str "str3" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
||||||
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)
|
||||||
|
|
@ -128,7 +128,7 @@ prog.py: error: argument --str: invalid choice: 'error' (choose from 'str1', 'st
|
||||||
|
|
||||||
def test_choice_int(json):
|
def test_choice_int(json):
|
||||||
output = """usage: prog.py "str" "1" --int "1" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
output = """usage: prog.py "str" "1" --int "1" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
||||||
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)
|
||||||
|
|
@ -156,7 +156,7 @@ prog.py: error: argument --int: invalid choice: '4' (choose from '1', '2', '3')
|
||||||
|
|
||||||
def test_choice_int_multi(json):
|
def test_choice_int_multi(json):
|
||||||
output = """usage: prog.py "str" "1" --int_multi "1" "2" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
output = """usage: prog.py "str" "1" --int_multi "1" "2" [-h] [--str {str1,str2,str3}] [--int {1,2,3}] [--int_multi [{1,2,3} ...]] {str,list,int,none} {1,2,3}
|
||||||
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)
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ options:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
|
|
||||||
leader:
|
leader:
|
||||||
-l [LEADER ...], --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)
|
||||||
|
|
@ -138,13 +138,13 @@ options:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
|
|
||||||
leader:
|
leader:
|
||||||
-l [LEADER ...], --leader.leader [LEADER ...]
|
-l, --leader.leader [LEADER ...]
|
||||||
Leader var
|
Leader var
|
||||||
--leader.follower INDEX [FOLLOWER]
|
--leader.follower INDEX [FOLLOWER]
|
||||||
Follower
|
Follower
|
||||||
--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]
|
--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]
|
||||||
Follower submulti
|
Follower submulti
|
||||||
-i INDEX [FOLLOWER_INTEGER], --leader.follower_integer INDEX [FOLLOWER_INTEGER]
|
-i, --leader.follower_integer INDEX [FOLLOWER_INTEGER]
|
||||||
Follower integer
|
Follower integer
|
||||||
--leader.follower_boolean INDEX
|
--leader.follower_boolean INDEX
|
||||||
Follower boolean
|
Follower boolean
|
||||||
|
|
|
||||||
|
|
@ -466,7 +466,7 @@ prog.py: error: the following arguments are required: --str
|
||||||
|
|
||||||
def test_readme_cross(json):
|
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
|
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)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
|
|
@ -482,7 +482,7 @@ prog.py: error: unrecognized arguments: --int
|
||||||
|
|
||||||
def test_readme_cross_remove(json):
|
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
|
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)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
|
|
@ -498,7 +498,7 @@ prog.py: error: unrecognized arguments: --int
|
||||||
|
|
||||||
def test_readme_cross_tree(json):
|
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
|
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)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
|
|
@ -514,7 +514,7 @@ prog.py: error: unrecognized arguments: --root.int
|
||||||
|
|
||||||
def test_readme_cross_tree_remove(json):
|
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
|
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)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
|
|
@ -530,7 +530,7 @@ prog.py: error: unrecognized arguments: --root.int
|
||||||
|
|
||||||
def test_readme_cross_tree_flatten(json):
|
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
|
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)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
|
|
@ -546,7 +546,7 @@ prog.py: error: unrecognized arguments: --int
|
||||||
|
|
||||||
def test_readme_cross_tree_flatten_remove(json):
|
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
|
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)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
|
|
@ -562,7 +562,7 @@ prog.py: error: unrecognized arguments: --int
|
||||||
|
|
||||||
def test_readme_unknown(json):
|
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)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
|
|
|
||||||
|
|
@ -22,5 +22,6 @@ except ImportError as err:
|
||||||
warnings.warn("cannot not import TiramisuCmdlineParser {err}", ImportWarning)
|
warnings.warn("cannot not import TiramisuCmdlineParser {err}", ImportWarning)
|
||||||
TiramisuCmdlineParser = None
|
TiramisuCmdlineParser = None
|
||||||
|
|
||||||
__version__ = "0.5"
|
from .__version__ import __version__
|
||||||
|
|
||||||
__all__ = ("TiramisuCmdlineParser",)
|
__all__ = ("TiramisuCmdlineParser",)
|
||||||
|
|
|
||||||
1
tiramisu_cmdline_parser/__version__.py
Normal file
1
tiramisu_cmdline_parser/__version__.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
__version__ = "0.7.0a2"
|
||||||
|
|
@ -59,6 +59,30 @@ def get_choice_list(config, properties, display):
|
||||||
return choices
|
return choices
|
||||||
|
|
||||||
|
|
||||||
|
def gen_argument_name(name, is_short_name, force_no, force_del):
|
||||||
|
if force_no:
|
||||||
|
if is_short_name:
|
||||||
|
prefix = "n"
|
||||||
|
else:
|
||||||
|
prefix = "no-"
|
||||||
|
if "." in name:
|
||||||
|
sname = name.rsplit(".", 1)
|
||||||
|
name = sname[0] + "." + prefix + sname[1]
|
||||||
|
else:
|
||||||
|
name = prefix + name
|
||||||
|
if force_del:
|
||||||
|
if is_short_name:
|
||||||
|
prefix = "p"
|
||||||
|
else:
|
||||||
|
prefix = "pop-"
|
||||||
|
if "." in name:
|
||||||
|
sname = name.rsplit(".", 1)
|
||||||
|
name = sname[0] + "." + prefix + sname[1]
|
||||||
|
else:
|
||||||
|
name = prefix + name
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
class TiramisuNamespace(Namespace):
|
class TiramisuNamespace(Namespace):
|
||||||
def __init__(self, config: Config, root: Optional[str]) -> None:
|
def __init__(self, config: Config, root: Optional[str]) -> None:
|
||||||
super().__setattr__("_config", config)
|
super().__setattr__("_config", config)
|
||||||
|
|
@ -155,9 +179,9 @@ class TiramisuNamespace(Namespace):
|
||||||
value = [value]
|
value = [value]
|
||||||
try:
|
try:
|
||||||
option.value.set(value)
|
option.value.set(value)
|
||||||
except PropertiesOptionError:
|
except PropertiesOptionError as err:
|
||||||
raise AttributeError(
|
raise AttributeError(
|
||||||
"unrecognized arguments: {}".format(self.arguments[key])
|
"unrecognized arguments: {} ({})".format(self.arguments[key], err)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _setattr_follower(
|
def _setattr_follower(
|
||||||
|
|
@ -185,7 +209,7 @@ class TiramisuHelpFormatter:
|
||||||
# Remove empty OD
|
# Remove empty OD
|
||||||
if (
|
if (
|
||||||
self.formatter.remove_empty_od
|
self.formatter.remove_empty_od
|
||||||
and len(self.items) == 1
|
and 0 < len(self.items) <= self.formatter.short_name_max_len
|
||||||
and self.items[0][0].__name__ == "_format_text"
|
and self.items[0][0].__name__ == "_format_text"
|
||||||
):
|
):
|
||||||
return ""
|
return ""
|
||||||
|
|
@ -224,34 +248,42 @@ class _BuildKwargs:
|
||||||
(not self.force_no or not add_extra_options)
|
(not self.force_no or not add_extra_options)
|
||||||
or (not_display and not display_modified_value)
|
or (not_display and not display_modified_value)
|
||||||
) and not self.force_del:
|
) and not self.force_del:
|
||||||
if self.force_no:
|
description = option.description()
|
||||||
description = option.information.get("negative_description", None)
|
|
||||||
else:
|
|
||||||
description = None
|
|
||||||
if description is None:
|
|
||||||
description = option.description()
|
|
||||||
self.kwargs["help"] = description
|
self.kwargs["help"] = description
|
||||||
if "positional" not in self.properties:
|
if "positional" not in self.properties:
|
||||||
is_short_name = self.cmdlineparser._is_short_name(
|
is_short_name = self.cmdlineparser._is_short_name(
|
||||||
name, "longargument" in self.properties
|
name, "longargument" in self.properties
|
||||||
)
|
)
|
||||||
if self.force_no:
|
if self.force_no:
|
||||||
ga_name = self.gen_argument_name(name, is_short_name)
|
ga_name = gen_argument_name(
|
||||||
ga_path = self.gen_argument_name(option.path(), is_short_name)
|
name, is_short_name, self.force_no, self.force_del
|
||||||
|
)
|
||||||
|
ga_path = gen_argument_name(
|
||||||
|
option.path(), is_short_name, self.force_no, self.force_del
|
||||||
|
)
|
||||||
self.cmdlineparser.namespace.list_force_no[ga_path] = option.path()
|
self.cmdlineparser.namespace.list_force_no[ga_path] = option.path()
|
||||||
elif self.force_del:
|
elif self.force_del:
|
||||||
ga_name = self.gen_argument_name(name, is_short_name)
|
ga_name = gen_argument_name(
|
||||||
ga_path = self.gen_argument_name(option.path(), is_short_name)
|
name, is_short_name, self.force_no, self.force_del
|
||||||
|
)
|
||||||
|
ga_path = gen_argument_name(
|
||||||
|
option.path(), is_short_name, self.force_no, self.force_del
|
||||||
|
)
|
||||||
self.cmdlineparser.namespace.list_force_del[ga_path] = option.path()
|
self.cmdlineparser.namespace.list_force_del[ga_path] = option.path()
|
||||||
else:
|
else:
|
||||||
ga_name = name
|
ga_name = name
|
||||||
self.kwargs["dest"] = self.gen_argument_name(option.path(), False)
|
ga_path = option.path()
|
||||||
|
self.kwargs["dest"] = gen_argument_name(
|
||||||
|
option.path(), False, self.force_no, self.force_del
|
||||||
|
)
|
||||||
argument = self.cmdlineparser._gen_argument(ga_name, is_short_name)
|
argument = self.cmdlineparser._gen_argument(ga_name, is_short_name)
|
||||||
self.cmdlineparser.namespace.arguments[option.path()] = argument
|
self.cmdlineparser.namespace.arguments[ga_path] = argument
|
||||||
self.args = [argument]
|
self.args = [argument]
|
||||||
|
self.ga_name = ga_name
|
||||||
else:
|
else:
|
||||||
self.cmdlineparser.namespace.arguments[option.path()] = option.path()
|
self.cmdlineparser.namespace.arguments[option.path()] = option.path()
|
||||||
self.args = [option.path()]
|
self.args = [option.path()]
|
||||||
|
self.ga_name = name
|
||||||
|
|
||||||
def __setitem__(self, key: str, value: Any) -> None:
|
def __setitem__(self, key: str, value: Any) -> None:
|
||||||
self.kwargs[key] = value
|
self.kwargs[key] = value
|
||||||
|
|
@ -261,38 +293,19 @@ class _BuildKwargs:
|
||||||
option.name(), "longargument" in self.properties
|
option.name(), "longargument" in self.properties
|
||||||
)
|
)
|
||||||
if self.force_no:
|
if self.force_no:
|
||||||
name = self.gen_argument_name(option.name(), is_short_name)
|
name = gen_argument_name(
|
||||||
|
option.name(), is_short_name, self.force_no, self.force_del
|
||||||
|
)
|
||||||
elif self.force_del:
|
elif self.force_del:
|
||||||
name = self.gen_argument_name(option.name(), is_short_name)
|
name = gen_argument_name(
|
||||||
|
option.name(), is_short_name, self.force_no, self.force_del
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
name = option.name()
|
name = option.name()
|
||||||
argument = self.cmdlineparser._gen_argument(name, is_short_name)
|
argument = self.cmdlineparser._gen_argument(name, is_short_name)
|
||||||
self.cmdlineparser.namespace.arguments[option.path()] = argument
|
self.cmdlineparser.namespace.arguments[option.path()] = argument
|
||||||
self.args.insert(0, argument)
|
self.args.insert(0, argument)
|
||||||
|
|
||||||
def gen_argument_name(self, name, is_short_name):
|
|
||||||
if self.force_no:
|
|
||||||
if is_short_name:
|
|
||||||
prefix = "n"
|
|
||||||
else:
|
|
||||||
prefix = "no-"
|
|
||||||
if "." in name:
|
|
||||||
sname = name.rsplit(".", 1)
|
|
||||||
name = sname[0] + "." + prefix + sname[1]
|
|
||||||
else:
|
|
||||||
name = prefix + name
|
|
||||||
if self.force_del:
|
|
||||||
if is_short_name:
|
|
||||||
prefix = "p"
|
|
||||||
else:
|
|
||||||
prefix = "pop-"
|
|
||||||
if "." in name:
|
|
||||||
sname = name.rsplit(".", 1)
|
|
||||||
name = sname[0] + "." + prefix + sname[1]
|
|
||||||
else:
|
|
||||||
name = prefix + name
|
|
||||||
return name
|
|
||||||
|
|
||||||
def get(self) -> Tuple[Dict]:
|
def get(self) -> Tuple[Dict]:
|
||||||
return self.args, self.kwargs
|
return self.args, self.kwargs
|
||||||
|
|
||||||
|
|
@ -328,6 +341,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
"TiramisuHelpFormatter", (TiramisuHelpFormatter, formatter_class), {}
|
"TiramisuHelpFormatter", (TiramisuHelpFormatter, formatter_class), {}
|
||||||
)
|
)
|
||||||
formatter_class.remove_empty_od = self.remove_empty_od
|
formatter_class.remove_empty_od = self.remove_empty_od
|
||||||
|
formatter_class.short_name_max_len = self.short_name_max_len
|
||||||
kwargs["formatter_class"] = formatter_class
|
kwargs["formatter_class"] = formatter_class
|
||||||
if not _forhelp and self.unrestraint:
|
if not _forhelp and self.unrestraint:
|
||||||
subconfig = self.config.unrestraint
|
subconfig = self.config.unrestraint
|
||||||
|
|
@ -374,9 +388,9 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
return self.prefix_chars + name
|
return self.prefix_chars + name
|
||||||
return self.prefix_chars * 2 + name
|
return self.prefix_chars * 2 + name
|
||||||
|
|
||||||
def _parse_known_args(self, args=None, namespace=None):
|
def _parse_known_args(self, args, namespace, *others):
|
||||||
try:
|
try:
|
||||||
namespace_, args_ = super()._parse_known_args(args, namespace)
|
namespace_, args_ = super()._parse_known_args(args, namespace, *others)
|
||||||
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):
|
||||||
|
|
@ -397,7 +411,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
fullpath=self.fullpath,
|
fullpath=self.fullpath,
|
||||||
)
|
)
|
||||||
namespace_, args_ = new_parser._parse_known_args(
|
namespace_, args_ = new_parser._parse_known_args(
|
||||||
args_, new_parser.namespace
|
args_, new_parser.namespace, *others
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
if self._registries["action"]["help"].needs:
|
if self._registries["action"]["help"].needs:
|
||||||
|
|
@ -454,7 +468,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
elif (
|
elif (
|
||||||
self.add_extra_options
|
self.add_extra_options
|
||||||
and obj.type() == "boolean"
|
and obj.type() == "boolean"
|
||||||
and not obj.issymlinkoption()
|
# and not obj.issymlinkoption()
|
||||||
):
|
):
|
||||||
if not obj.isleader():
|
if not obj.isleader():
|
||||||
yield obj, False, None
|
yield obj, False, None
|
||||||
|
|
@ -472,15 +486,6 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
and obj.type() == "boolean"
|
and obj.type() == "boolean"
|
||||||
and obj.value.get() is True
|
and obj.value.get() is True
|
||||||
):
|
):
|
||||||
negative_description = obj.information.get(
|
|
||||||
"negative_description", None
|
|
||||||
)
|
|
||||||
if _forhelp and not negative_description:
|
|
||||||
raise ValueError(
|
|
||||||
_(
|
|
||||||
f'the boolean "{obj.path()}" cannot have a default value to "True" with option add_extra_options if there is no negative_description'
|
|
||||||
)
|
|
||||||
)
|
|
||||||
yield obj, True, None
|
yield obj, True, None
|
||||||
else:
|
else:
|
||||||
yield obj, None, None
|
yield obj, None, None
|
||||||
|
|
@ -488,7 +493,11 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
# no follower found, search if there is a symlink
|
# no follower found, search if there is a symlink
|
||||||
for sobj in config.list(uncalculated=True):
|
for sobj in config.list(uncalculated=True):
|
||||||
try:
|
try:
|
||||||
if sobj.issymlinkoption() and sobj.option().isleader():
|
if (
|
||||||
|
sobj.issymlinkoption()
|
||||||
|
and sobj.index() is None
|
||||||
|
and sobj.option().isleader()
|
||||||
|
):
|
||||||
yield sobj, None, None
|
yield sobj, None, None
|
||||||
except ConfigError:
|
except ConfigError:
|
||||||
pass
|
pass
|
||||||
|
|
@ -510,11 +519,19 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
_('name cannot startswith "{}"').format(self.prefix_chars)
|
_('name cannot startswith "{}"').format(self.prefix_chars)
|
||||||
)
|
)
|
||||||
if option.issymlinkoption():
|
if option.issymlinkoption():
|
||||||
symlink_name = option.option().name()
|
if self.fullpath:
|
||||||
|
argument_name = option.option().path()
|
||||||
|
else:
|
||||||
|
argument_name = option.option().name()
|
||||||
|
is_short_name = len(option.option().name()) == 1
|
||||||
|
symlink_name = gen_argument_name(
|
||||||
|
argument_name, is_short_name, force_no, force_del
|
||||||
|
)
|
||||||
if symlink_name in options_is_not_default:
|
if symlink_name in options_is_not_default:
|
||||||
options_is_not_default[symlink_name]["name"] = name
|
options_is_not_default[symlink_name]["name"] = name
|
||||||
if symlink_name in actions:
|
if symlink_name in actions:
|
||||||
for action in actions[symlink_name]:
|
for action in actions[symlink_name]:
|
||||||
|
action.force_no = force_no
|
||||||
action.add_argument(option)
|
action.add_argument(option)
|
||||||
continue
|
continue
|
||||||
if force_del:
|
if force_del:
|
||||||
|
|
@ -663,7 +680,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
kwargs["type"] = float
|
kwargs["type"] = float
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
actions.setdefault(option.name(), []).append(kwargs)
|
actions.setdefault(kwargs.ga_name, []).append(kwargs)
|
||||||
|
|
||||||
for option_is_not_default in options_is_not_default.values():
|
for option_is_not_default in options_is_not_default.values():
|
||||||
self._option_is_not_default(**option_is_not_default)
|
self._option_is_not_default(**option_is_not_default)
|
||||||
|
|
@ -691,7 +708,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
if err.proptype == ["mandatory"]:
|
if err.proptype == ["mandatory"]:
|
||||||
self.error("the following arguments are required: {}".format(name))
|
self.error("the following arguments are required: {}".format(name))
|
||||||
else:
|
else:
|
||||||
self.error("unrecognized arguments: {}".format(name))
|
self.error("unrecognized arguments: {} ({})".format(name, err))
|
||||||
if valid_mandatory:
|
if valid_mandatory:
|
||||||
errors = []
|
errors = []
|
||||||
for option in self.config.value.mandatory():
|
for option in self.config.value.mandatory():
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue