ladybird/Userland/Utilities/basename.cpp

35 lines
922 B
C++
Raw Normal View History

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h>
2019-04-15 14:57:09 +03:00
#include <stdio.h>
#include <unistd.h>
2019-04-15 14:57:09 +03:00
int main(int argc, char** argv)
{
2020-02-18 15:14:55 +03:00
if (pledge("stdio", nullptr) < 0) {
perror("pledge");
return 1;
}
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);
auto result = LexicalPath::basename(path);
if (!suffix.is_null() && result.length() != suffix.length() && result.ends_with(suffix))
result = result.substring_view(0, result.length() - suffix.length());
outln("{}", result);
2019-04-15 14:57:09 +03:00
return 0;
}