new_for_commandline_arg


Description:

public static File new_for_commandline_arg (string arg)

Creates a File with the given argument from the command line.

The value of arg can be either a URI, an absolute path or a relative path resolved relative to the current working directory. This operation never fails, but the returned object might not support any I/O operation if arg points to a malformed path.

Note that on Windows, this function expects its argument to be in UTF-8 -- not the system code page. This means that you should not use this function with string from argv as it is passed to main. get_command_line will return a UTF-8 version of the commandline. Application also uses UTF-8 but create_file_for_arg may be more useful for you there. It is also always possible to use this function with OptionContext arguments of type g_option_arg_filename.

Example: Construct a File for a given commandline-argument:

public static int main (string[] args) {
if (args.length != 2) {
print ("%s [FILE]\n", args[0]);
return 0;
}

// Create a file that can only be accessed by the current user:
File file = File.new_for_commandline_arg (args[1]);
try {
FileOutputStream os = file.create (FileCreateFlags.PRIVATE);
os.write ("My first line\n".data);
print ("Created.\n");
} catch (Error e) {
print ("Error: %s\n", e.message);
}

return 0;
}

valac --pkg gio-2.0 GLib.File.new_for_commandline_arg.vala

Example: Trash a file:

public static int main (string[] args) {
if (args.length != 2) {
print ("%s FILE\n", args[0]);
return 0;
}

try {
File file = File.new_for_commandline_arg (args[1]);
file.trash ();
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}

valac --pkg gio-2.0 GLib.File.trash.vala

Parameters:

arg

a command line string

Returns:

a new File. Free the returned object with unref.




2022 vala-language.org