Rework cmdparse.Script API

The API now requires a command argument from signature. Parsing errors
(no command from input) is thrown as a custom exception, and caught by
the outmost possible invocation to emit a message from click.
This commit is contained in:
Tzu-ping Chung
2018-04-06 17:34:14 +08:00
committed by Dan Ryan
parent d1eeeced45
commit 483bf175cf
4 changed files with 38 additions and 29 deletions
+11 -6
View File
@@ -4,22 +4,27 @@ import shlex
import six
class ScriptEmptyError(ValueError):
pass
class Script(object):
"""Parse a script line (in Pipfile's [scripts] section).
This always works in POSIX mode, even on Windows.
"""
def __init__(self, parts):
if not parts:
raise ValueError('invalid script')
self._parts = parts
def __init__(self, command, args=None):
self._parts = [command]
if args:
self._parts.extend(args)
@classmethod
def parse(cls, value):
if isinstance(value, six.string_types):
value = shlex.split(value)
return cls(value)
if not value:
raise ScriptEmptyError(value)
return cls(value[0], value[1:])
def __repr__(self):
return 'Script({0!r})'.format(self._parts)