hautils

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 2f872228bad7363152400fcc1d2eeaeb62d4c83f
parent aaf75eae4d6d31a820a0bf34b2722ec039d52d7d
Author: Evan Johnston <evan@d2evs.net>
Date:   Tue,  5 Oct 2021 17:31:46 -0400

basename, dirname: use cmd.args

os::args yields the wrong behaviour on eg. `basename -- foo/bar`, using
cmd.args fixes this.

Diffstat:
Mbasename.ha | 13+++++++------
Mdirname.ha | 7++++---
2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/basename.ha b/basename.ha @@ -10,17 +10,18 @@ export fn utilmain() (main::error | void) = { "return non-directory portion of a pathname", "<path> [<suffix>]", ]; - getopt::finish(&getopt::parse(os::args, help...)); - if (len(os::args) != 2 && len(os::args) != 3) { + const cmd = getopt::parse(os::args, help...); + defer getopt::finish(&cmd); + if (len(cmd.args) != 1 && len(cmd.args) != 2) { getopt::printusage(os::stderr, os::args[0], help); os::exit(1); }; - let basename = path::basename(os::args[1]); - if (len(os::args) == 3 && strings::has_suffix(basename, os::args[2])) { - if (len(basename) == len(os::args[2])) yield; + let basename = path::basename(cmd.args[0]); + if (len(cmd.args) == 2 && strings::has_suffix(basename, cmd.args[1])) { + if (len(basename) == len(cmd.args[1])) yield; // XXX: This should probably go in strings:: let slice = strings::toutf8(basename); - slice = slice[..len(basename) - len(os::args[2])]; + slice = slice[..len(basename) - len(cmd.args[1])]; basename = strings::fromutf8(slice); }; fmt::println(basename)?; diff --git a/dirname.ha b/dirname.ha @@ -11,11 +11,12 @@ export fn utilmain() (main::error | void) = { "return the directory portion of a pathname", "<path>", ]; - getopt::finish(&getopt::parse(os::args, help...)); - if (len(os::args) != 2) { + const cmd = getopt::parse(os::args, help...); + defer getopt::finish(&cmd); + if (len(cmd.args) != 1) { getopt::printusage(os::stderr, os::args[0], help); os::exit(1); }; - fmt::println(path::dirname(os::args[1]))?; + fmt::println(path::dirname(cmd.args[0]))?; return void; };