| 1 | //! Creates a file at the given path, if it doesn't already exist. |
| 2 | //! |
| 3 | //! ``` |
| 4 | //! touch <path> |
| 5 | //! ``` |
| 6 | //! |
| 7 | //! Path must be absolute. |
| 8 | |
| 9 | const std = @import("std"); |
| 10 | const Io = std.Io; |
| 11 | |
| 12 | pub fn main(init: std.process.Init) !void { |
| 13 | const io = init.io; |
| 14 | |
| 15 | var args = try init.minimal.args.iterateAllocator(init.gpa); |
| 16 | defer args.deinit(); |
| 17 | _ = args.next().?; // skip binary name |
| 18 | |
| 19 | const path = args.next() orelse { |
| 20 | std.log.err("missing <path> argument", .{}); |
| 21 | return error.BadUsage; |
| 22 | }; |
| 23 | |
| 24 | const dir_path = Io.Dir.path.dirname(path).?; |
| 25 | const basename = Io.Dir.path.basename(path); |
| 26 | |
| 27 | var dir = try Io.Dir.cwd().openDir(io, dir_path, .{}); |
| 28 | defer dir.close(io); |
| 29 | |
| 30 | var file = try dir.createFile(io, basename, .{ .truncate = false }); |
| 31 | file.close(io); |
| 32 | } |