Serenity Operating System
at portability 89 lines 3.3 kB view raw
1/* 2 * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/Function.h> 30#include <AK/String.h> 31#include <AK/Vector.h> 32#include <stdio.h> 33 34namespace Core { 35 36class ArgsParser { 37public: 38 ArgsParser(); 39 40 enum class Required { 41 Yes, 42 No 43 }; 44 45 struct Option { 46 bool requires_argument { true }; 47 const char* help_string { nullptr }; 48 const char* long_name { nullptr }; 49 char short_name { 0 }; 50 const char* value_name { nullptr }; 51 Function<bool(const char*)> accept_value; 52 53 String name_for_display() const 54 { 55 if (long_name) 56 return String::format("--%s", long_name); 57 return String::format("-%c", short_name); 58 } 59 }; 60 61 struct Arg { 62 const char* help_string { nullptr }; 63 const char* name { nullptr }; 64 int min_values { 0 }; 65 int max_values { 1 }; 66 Function<bool(const char*)> accept_value; 67 }; 68 69 void parse(int argc, char** argv); 70 void print_usage(FILE*, const char* argv0); 71 72 void add_option(Option&&); 73 void add_option(bool& value, const char* help_string, const char* long_name, char short_name); 74 void add_option(const char*& value, const char* help_string, const char* long_name, char short_name, const char* value_name); 75 void add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name); 76 77 void add_positional_argument(Arg&&); 78 void add_positional_argument(const char*& value, const char* help_string, const char* name, Required required = Required::Yes); 79 void add_positional_argument(int& value, const char* help_string, const char* name, Required required = Required::Yes); 80 void add_positional_argument(Vector<const char*>& value, const char* help_string, const char* name, Required required = Required::Yes); 81 82private: 83 Vector<Option> m_options; 84 Vector<Arg> m_positional_args; 85 86 bool m_show_help { false }; 87}; 88 89}