Skip to content

Commit 3441a1e

Browse files
committed
now raising an exception by default when encountering unparsed arg(s)
1 parent 962aa03 commit 3441a1e

4 files changed

Lines changed: 26 additions & 8 deletions

File tree

CHANGES.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Changelog
22
=========
33

4+
0.0.7 (2023-11-09)
5+
------------------
6+
7+
- `Plugin.parse_args` now returns any unparsed arguments that were found
8+
- the `args_to_objects` method now raises an Exception by default when
9+
unknown arguments are encountered for a plugin (can be controlled with
10+
the `allow_unknown_args` parameter)
11+
12+
413
0.0.6 (2023-10-11)
514
------------------
615

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def _read(f):
3030
'': 'src'
3131
},
3232
packages=find_namespace_packages(where='src'),
33-
version="0.0.6",
33+
version="0.0.7",
3434
author='Peter Reutemann',
3535
author_email='fracpete@waikato.ac.nz',
3636
)

src/seppl/_args.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def split_args(args: List[str], handlers: List[str]) -> Dict[str, List[str]]:
5252
return result
5353

5454

55-
def args_to_objects(args: Dict[str, List[str]], valid_plugins: Dict[str, Plugin], allow_global_options: bool = False) -> List[Plugin]:
55+
def args_to_objects(args: Dict[str, List[str]], valid_plugins: Dict[str, Plugin], allow_global_options: bool = False, allow_unknown_args: bool = False) -> List[Plugin]:
5656
"""
5757
Instantiates the plugins from the parsed arguments dictionary.
5858
@@ -62,6 +62,8 @@ def args_to_objects(args: Dict[str, List[str]], valid_plugins: Dict[str, Plugin]
6262
:type valid_plugins: dict
6363
:param allow_global_options: whether global options are allowed (ie options that don't follow a plugin name)
6464
:type allow_global_options: bool
65+
:param allow_unknown_args: whether to allow unknown args (eg typos or unknown plugins)
66+
:type allow_unknown_args: bool
6567
:return: the list of instantiated plugins
6668
:rtype: list
6769
"""
@@ -75,7 +77,9 @@ def args_to_objects(args: Dict[str, List[str]], valid_plugins: Dict[str, Plugin]
7577

7678
name = args[key][0]
7779
plugin = copy.deepcopy(valid_plugins[name])
78-
plugin.parse_args(args[key][1:])
80+
unknown = plugin.parse_args(args[key][1:])
81+
if not allow_unknown_args and (len(unknown) > 0):
82+
raise Exception("Found unknown argument(s) for plugin '%s': %s" % (plugin.name(), str(unknown)))
7983
result.append(plugin)
8084
return result
8185

src/seppl/_plugin.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from ._types import classes_to_str
55

66

7+
UNKNOWN_ARGS = "unknown_args"
8+
9+
710
class Plugin:
811
"""
912
Base class for plugins.
@@ -38,6 +41,7 @@ def _create_argparser(self) -> argparse.ArgumentParser:
3841
description=self.description(),
3942
prog=self.name(),
4043
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
44+
parser.add_argument(UNKNOWN_ARGS, nargs=argparse.REMAINDER)
4145
return parser
4246

4347
def _apply_args(self, ns: argparse.Namespace):
@@ -49,18 +53,19 @@ def _apply_args(self, ns: argparse.Namespace):
4953
"""
5054
pass
5155

52-
def parse_args(self, args: List[str]) -> 'Plugin':
56+
def parse_args(self, args: List[str]) -> list:
5357
"""
5458
Parses the command-line arguments.
5559
5660
:param args: the arguments to parse
5761
:type args: list
58-
:return: itself
59-
:rtype: Plugin
62+
:return: any unknown args
63+
:rtype: list
6064
"""
6165
parser = self._create_argparser()
62-
self._apply_args(parser.parse_args(args))
63-
return self
66+
ns = parser.parse_args(args)
67+
self._apply_args(ns)
68+
return ns.unknown_args
6469

6570
def print_help(self):
6671
"""

0 commit comments

Comments
 (0)