serenity/Userland/Utilities/basename.cpp

32 lines
908 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>
2021-11-25 20:37:57 +00:00
#include <LibCore/System.h>
#include <LibMain/Main.h>
2019-04-15 11:57:09 +00:00
2021-11-25 20:37:57 +00:00
ErrorOr<int> serenity_main(Main::Arguments arguments)
2019-04-15 11:57:09 +00:00
{
TRY(Core::System::pledge("stdio"));
2020-02-18 12:14:55 +00:00
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);
2021-11-25 20:37:57 +00:00
args_parser.parse(arguments);
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 11:57:09 +00:00
return 0;
}