-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparser.py
28 lines (21 loc) · 897 Bytes
/
argparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import argparse
import shlex
class DefaultArguments(argparse.ArgumentParser):
def error(self, message):
raise RuntimeError(message)
class Arguments:
def __init__(self, posix: bool = False, allow_abbrev: bool = False, **kwargs):
self.parser = DefaultArguments(allow_abbrev=allow_abbrev, add_help=False, **kwargs)
self.posix = posix
def add_argument(self, *inputs, **kwargs):
""" Shortcut to argparse.add_argument """
self.parser.add_argument(*inputs, **kwargs)
def parse_args(self, text):
""" Shortcut to argparse.parse_args with shlex implemented """
try:
args = self.parser.parse_args(
shlex.split(text if text else "", posix=self.posix)
)
except Exception as e:
return (f"ArgumentError: {e}", False)
return (args, True)