-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core_commands: split lexical arguments with shlex #2344
base: master
Are you sure you want to change the base?
Conversation
Lexical (shell-like) argument splitting allows support for quoted "user names".
d8bf074
to
5926ef9
Compare
5926ef9
to
4827a84
Compare
598a186
to
8a65b65
Compare
|
8a65b65
to
fe79b41
Compare
I intend to merge this for 3.3.0, just haven't gotten around to reviewing the changes yet. |
The actual The explanatory code comments are only there to help draft code review and will be removed once ratified.. |
Postponing this in order to release 3.3.0 sooner, hopefully we can get it into a 3.3.x point release. |
/msg
and/usearch
commands/say
command/sample
command to test this implementation with 3 argumentsLexical (shell-like) argument splitting allows support for quoted "user names". Quotes are not mandatory if the argument is the first or last one, nor if the username is a single word, i.e; the following inputs all validate into 2 arguments:
/msg "user name" "message text"
<- both arguments quoted, ok./msg user name "message text"
<- only final argument quoted, ok./msg "user name" message text
<- only first argument quoted, ok./msg username message text
<- no quotes required for single-word usernames, ok.Review/testing points:
3 or more
num_required_args
are tested working with the newsplit_args()
function. The/msg
only needs to accept two arguments, but other commands might need more.Only un-quoted arguments will be split. Quoted arguments are always protected from splitting. A helpful error message is shown on the interface in the case of a mismatch, such as "Too many arguments".
Setting
num_required_args=0
returns unlimited splits, and thus will split all un-quoted arguments as separate words, as per the standard split function without maxsplit, or maxsplit=-1, but the quoted phrases will be together as one argument.It is very likely that the
<user>
argument will contain spaces, because many users have such long names. Some commands, such as/usearch
for example) are useless without support for arguments that contain spaces.The proposed implementation is a useful utility that should be available to plugin developers to process any string in the way they want, with any amount of required arguments (such as perhaps from secondary input() prompt or another source).
Closer integration for argument parsing might be considered into the pluginsystem core, but caution is needed to ensure that optional arguments don't throw an error that prevents the plugin command from being triggered.
If there is a combination of required arguments plus two or more optional arguments then arguments can be ambiguous, but having one optional argument (such as in most private chat commands) in the final position would be okay.
Strings containing
'
inline"
quotes"
can be"
protected"
with alternate quotation marks'
, and other features, but unfortunately there is no maxsplit feature, which is why we need to extend the shlex function such as done in this PR.See shlex module Python docs for details.