hautils

[hare] Set of POSIX utilities
Log | Files | Refs | README | LICENSE

basename.ha (542B)


      1 use fmt;
      2 use getopt;
      3 use main;
      4 use os;
      5 use path;
      6 use strings;
      7 
      8 export fn utilmain() (main::error | void) = {
      9 	const help: []getopt::help = [
     10 		"return non-directory portion of a pathname",
     11 		"<path> [<suffix>]",
     12 	];
     13 	const cmd = getopt::parse(os::args, help...);
     14 	defer getopt::finish(&cmd);
     15 	if (len(cmd.args) != 1 && len(cmd.args) != 2) {
     16 		main::usage(help);
     17 	};
     18 	let basename = path::basename(cmd.args[0]);
     19 	if (len(cmd.args) == 2) {
     20 		basename = strings::trimsuffix(basename, cmd.args[1]);
     21 	};
     22 	fmt::println(basename)?;
     23 	return void;
     24 };