From 92765825358daaed11fa105ca64cf2c7819e5563 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Mon, 27 Jan 2020 20:19:39 +0300 Subject: [PATCH] LibCore: Rewrite CArgsParser This is a complete reimplementation of CArgsParser with a different API. Now, CArgsParser properly supports and distinguishes between: * Positional arguments (required or not) * Options Options can be short and/or long. The API allows you to add custom option and argument types. A few types are pre-implemented for convenience: * Boolean options (take no value) * String and integer options (take a required value) * String and integer arguments * Vector-of-string arguments This commit doesn't include changes for all the users of CArgsParser (see next commit for that). --- Libraries/LibCore/CArgsParser.cpp | 456 +++++++++++++++++------------- Libraries/LibCore/CArgsParser.h | 104 +++---- 2 files changed, 303 insertions(+), 257 deletions(-) diff --git a/Libraries/LibCore/CArgsParser.cpp b/Libraries/LibCore/CArgsParser.cpp index f4f65cca32..84caa72da3 100644 --- a/Libraries/LibCore/CArgsParser.cpp +++ b/Libraries/LibCore/CArgsParser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2020, Sergey Bugaev * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,239 +27,295 @@ #include "CArgsParser.h" #include +#include #include -bool CArgsParserResult::is_present(const String& arg_name) const +CArgsParser::CArgsParser() { - return m_args.contains(arg_name); + add_option(m_show_help, "Display this message", "help", 0); } -String CArgsParserResult::get(const String& arg_name) const +void CArgsParser::parse(int argc, char** argv) { - return m_args.get(arg_name).value_or({}); -} + auto print_usage_and_exit = [this, argv] { + print_usage(stderr, argv[0]); + exit(1); + }; + Vector