basename: Support suffix stripping
Allow passing an optional suffix argument to `basename` which is then stripped from the resulting basename (unless the resulting basename is identical to the suffix.) https://pubs.opengroup.org/onlinepubs/9699919799/utilities/basename.html
This commit is contained in:
parent
313e53dca0
commit
9388cf327f
Notes:
sideshowbarker
2024-07-18 17:05:07 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/9388cf327f1
1 changed files with 9 additions and 3 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -16,12 +16,18 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
const char* path = nullptr;
|
||||
StringView path;
|
||||
StringView suffix;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(path, "Path to get basename from", "path");
|
||||
args_parser.add_positional_argument(suffix, "Suffix to strip from name", "suffix", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(argc, argv);
|
||||
|
||||
printf("%s\n", LexicalPath(path).basename().characters());
|
||||
auto basename = LexicalPath(path).basename();
|
||||
if (!suffix.is_null() && basename.length() != suffix.length() && basename.ends_with(suffix))
|
||||
outln("{}", basename.substring_view(0, basename.length() - suffix.length()));
|
||||
else
|
||||
outln("{}", basename);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue