Serenity Operating System
at master 31 lines 908 B view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/LexicalPath.h> 8#include <LibCore/ArgsParser.h> 9#include <LibCore/System.h> 10#include <LibMain/Main.h> 11 12ErrorOr<int> serenity_main(Main::Arguments arguments) 13{ 14 TRY(Core::System::pledge("stdio")); 15 16 StringView path; 17 StringView suffix; 18 19 Core::ArgsParser args_parser; 20 args_parser.add_positional_argument(path, "Path to get basename from", "path"); 21 args_parser.add_positional_argument(suffix, "Suffix to strip from name", "suffix", Core::ArgsParser::Required::No); 22 args_parser.parse(arguments); 23 24 auto result = LexicalPath::basename(path); 25 26 if (!suffix.is_null() && result.length() != suffix.length() && result.ends_with(suffix)) 27 result = result.substring_view(0, result.length() - suffix.length()); 28 29 outln("{}", result); 30 return 0; 31}