parse_argv


Description:

public bool parse_argv (string command_line, out string[] argvp) throws ShellError

Parses a command line into an argument vector, in much the same way the shell would, but without many of the expansions the shell would perform (variable expansion, globs, operators, filename expansion, etc.

are not supported).

The results are defined to be the same as those you would get from a UNIX98 `/bin/sh`, as long as the input contains none of the unsupported shell expansions. If the input does contain such expansions, they are passed through literally.

Possible errors are those from the g_shell_error domain.

In particular, if command_line is an empty string (or a string containing only whitespace), g_shell_error_empty_string will be returned. It’s guaranteed that argvp will be a non-empty array if this function returns successfully.

Free the returned vector with strfreev.

Example: Parse a command line into an argument vector:

public static int main (string[] args) {
// Output: ``'rm', '-R', 'my dir',``
try {
string[]? argvp = null;
Shell.parse_argv ("rm -R \"my dir\"", out argvp);
foreach (unowned string arg in argvp) {
print ("'%s', ", arg);
}
print ("\n");
} catch (ShellError e) {
print ("Error: %s\n", e.message);
}

// Output: ``Error: Text ended before matching quote was found for ". (The text was 'rm -R "my dir')``
try {
string[]? argvp = null;
Shell.parse_argv ("rm -R \"my dir", out argvp);
assert_not_reached ();
} catch (ShellError e) {
print ("Error: %s\n", e.message);
}

return 0;
}

valac --pkg glib-2.0 GLib.Shell.parse_argv.vala

Parameters:

command_line

command line to parse

argvp

return location for array of args

argcp

return location for number of args

Returns:

true on success, false if error set


Namespace: GLib.Shell
Package: glib-2.0